#!/usr/athena/bin/perl

($whoami = $0) =~ s,.*/,,;

$usage = "Usage: $0 [ -debug ] [ -out file ] [ -- ] [ filename [ ... ] ]";

while (($_ = $ARGV[0]) && /^-/) {
    shift;
    if (/^-debug$/) {
	$debug++;
    }
    elsif (/^-out$/) {
	($outfile = shift @ARGV) ||
	    die "Missing argument to $_ option.\n$usage\n";
    }
    elsif (/^--$/) {
	last;
    }
    else {
	die "Unknown option \"$_\".\n$usage\n";
    }
}

if ($outfile) {
    open(STDOUT, ">$outfile") || die "Opening $outfile for write: $!.\n";
}

require 'timelocal.pl';

$now = time;
$one_year = 60 * 60 * 24 * 365;
$dead_line = $one_year;

($garbage, $garbage, $garbage, $garbage, $garbage, $cur_year) = 
    localtime($now);

$/ = "\n\n";
$rec = 0;
while (<>) {
    $rec++;
    $* = 0;
    s/\n\n+$/\n/;
    $* = 1;
    ($record = $_) =~ s/^/\t/g;
    $comment = "";
    warn "Converted +Comment: to +C: in record $rec.\n"
	if (s/^\+Comment:/\+C:/g && $debug);
    while (s/^\+C:(.*\n([ \t]+.*\n)*)//) {
	$newcomment = &trim_comment($1);
	$comment .= "+C:" . $newcomment
	    if ($newcomment ne "");
    }
    print $_, $comment, "\n";
}

sub trim_comment {
    local($_) = @_;
    local($newcomment) = "";
    local($mon, $mday, $year);
    local($comment);
    local(@comments);
    local($diff);

    $* = 1;
    if (! s,(^[ \t]*[0-9]+/[0-9]+/[0-9]+:),--trim-comments--\n$1,g) {
	warn "Incorrect comment format in record $rec:\n$record";
	return $_;
    }
    @comments = split(/^--trim-comments--\n/, $_);
    if ($comments[0] ne "") {
	warn "Incorrect comment format in record $rec:\n$record";
	return(join('', @comments));
    }
    else {
	shift @comments; # get rid of the blank one at the top.
    }

    $* = 0;

    while ($comment = shift @comments) {
	if ($comment !~ m,^[ \t]*([0-9]+)/([0-9]+)/([0-9]+):,) {
	    warn "Internal error in trim_comment!  Date match failed.\n";
	    return(join('', $newcomment, $comment, @comments));
	}
	$mon = $1 - 1;
	$mday = $2;
	$year = $3;

	if (($mon < 0) || ($mon > 11)) {
	    warn "Invalid month $mon in record $rec:\n$record";
	    return(join('', $newcomment, $comment, @comments));
	}
	if (($mday < 1) || ($mday > 31)) {
	    warn "Invalid month day $mday in record $rec:\n$record";
	    return(join('', $newcomment, $comment, @comments));
	}
	if ($year > 1900) {
	    warn "Four-digit year $year in record $rec.\n";
	    $year -= 1900;
	}
	if (($year < 90) || ($year > $cur_year)) {
	    warn "Invalid year $year in record $rec:\n$record";
	    return(join('', $newcomment, $comment, @comments));
	}
	
	$diff = $now - &timelocal(0, 0, 0, $mday, $mon, $year);

	if ($diff < 0) {
	    warn "Negative time difference for $mon/$mday/$year " .
		"in record $rec:\n$record";
	    return(join('', $newcomment, $comment, @comments));
	}
	elsif ($diff > 2 * $dead_line) {
	    warn "Abnormally large difference for $mon/$mday/$year " .
		"in record $rec:\n$record";
	    return(join('', $newcomment, $comment, @comments));
	}
	elsif ($diff <= $dead_line) {
	    $newcomment .= $comment;
	}
	else {
	    warn "Removed comment in $rec.\n"
		if ($debug);
	}
    }

    $newcomment;
}
