#!/usr/bin/perl

use strict;
use warnings;

my $link;
my $got_data;
my $file = $ARGV[0];
my $strip = (scalar @ARGV gt 1 and $ARGV[1] eq '-strip');
open my $lines, $file or die "Could not open $file: $!";

while(my $line = <$lines>) {
	chomp($line);
	#if($line =~ /(\?s=6&p=\d+)/) {
    if($line =~ /(\d+\/\d+\/\d+).*?<a href="(\?s=6&p=[^"]+)">([^<]+)/) {
		if($got_data) {
			if($strip) { print "$link\n"; }
			$got_data = 0;
		}
		$link = "$2\t$3\t$1";
		if(!$strip) { print "$link\n"; }
		next;
	}
	$line =~ s/<br><br><br>//g;
	if($line eq '<br />') { next; }
	elsif($line =~ /^--.*?--$/) { next; }
	elsif($line =~ /^\|.*?\|$/) { next; }
	elsif($line =~ /<a href/ and $strip) { next; }
	elsif($line =~ /<\.img/ and $strip) { next; }
	elsif($line =~ /\[..\].*?began/) { next; }
	elsif($line =~ /\[..\].*?ceased/) { next; }
	elsif($line =~ /\[..\].*?blocked/ and $strip) { next; }
	elsif($line =~ /\[...?\].*?memo/ and $strip) { next; }
	if($strip) {
		$line =~ s/<span[^>]*>//g;
		$line =~ s/<\/span>//g;
	}
	if($line eq '') { next; }
	print "$line\n";
	$got_data = 1;
}

close $lines;
