#!/usr/sww/bin/perl

$config{"dotplan"} = "/private/remailer-list/.plan";
$config{"remailer-list-file"} = "/usr/local/remailer-list3/.remailers";
$config{"motd"} = "/usr/local/remailer-list3/motd";
$config{"mmotd"} = "/usr/local/remailer-list3/mmotd";
$config{"webfile"} = "/home/orodruin/c/grad/raph/public_html/remailer-list.html";
$config{"webtemplate"} = "/usr/local/remailer-list3/remailer-list-templ.html";
$config{"mwebfile"} = "/usr/local/raph/ipc/mixmaster-list.html";
$config{"mwebtemplate"} = "/usr/local/remailer-list3/mixmaster-templ.html";
$config{"nums"} = "/usr/local/remailer-list3/nums";
$config{"type2"} = "/usr/local/raph/ipc/type2.list";
$config{"pubring"} = "/usr/local/raph/ipc/pubring.mix";

sub compare {
    if ($a =~ /^(\S+)\s/) {
	$remailera = $1;
	if ($b =~ /^(\S+)\s/) {
	    $remailerb = $1;
	    return $rel{$remailerb} <=> $rel{$remailera};
	}
    }
}

sub compare_mix {
    if ($a =~ /^(\S+)\s/) {
	$remailera = $1;
	if ($b =~ /^(\S+)\s/) {
	    $remailerb = $1;
	    return $rel_mix{$remailerb} <=> $rel_mix{$remailera};
	}
    }
}

{
    open (DOTPLAN, ">".$config{"dotplan"});
    open (RFILE, $config{"remailer-list-file"});
    while (<RFILE>) {
	if (/^\$remailer\{\"([a-zA-Z0-9\-]+)\"\} \= /) {
	    print DOTPLAN $_;
	    $remailer = $1;
	    if (/\<([^\>]*)\>(.*)$/) {
		$email{$remailer} = $1;
		if ($2 =~ /filter/) {
		    $filter{$remailer} = 1;
		}
	    }
	}
    }
    open (MOTD, $config{"motd"});
    while (<MOTD>) {
	print DOTPLAN;
    }
    close (MOTD);
    print DOTPLAN "\n";
    close (RFILE);
    print DOTPLAN 'Last update: '.&time (localtime (time))."\n";
    print DOTPLAN "remailer  email address                        history  latency  uptime\n";
    print DOTPLAN "-----------------------------------------------------------------------\n";
    while (<>) {
	if (/^\s*([a-zA-Z0-9\-]+)\s/) {
	    $remailer = $1;
	    $email = sprintf ("%-32.32s", $email{$remailer});
	    s/\<\-+\>/$email/;
	    if (/(\d+\.\d+)\%/) {
		$rel = $1;
		if ($filter{$remailer}) {
		    $rel -= 10;
		}
		$rel{$remailer} = $rel;
	    }
	    if ($remailer =~ /^\d+$/) {
		push (@nums, $_);
	    } else {
		push (@mailers, $_);
	    }
	} elsif (/^\s*\(([\w\-]+)\)\s/) {
	    $remailer = $1;
	    if (/(\d+\.\d+)\%/) {
		$rel = $1;
		$rel_mix{$remailer} = $rel;
	    }
	    s/\(([\w\-]+)\)/$1/;
	    push (@mixmasters, $_);
	}
    }
    @mailers = sort compare @mailers;
    print DOTPLAN @mailers;
    close (DOTPLAN);
    @mixmasters = sort compare_mix @mixmasters;
    if ($config{"webfile"}) {
	open (WEBFILE, ">".$config{"webfile"});
	open (WEBTEMPL, $config{"webtemplate"});
	while (<WEBTEMPL>) {
	    if ($_ eq "<remailer-list here>\n") {
		open (DOTPLAN, $config{"dotplan"});
		while (<DOTPLAN>) {
		    $_ =~ s/\</\&lt\;/g;
		    $_ =~ s/\>/\&gt\;/g;
		    print WEBFILE $_;
		}
		close (DOTPLAN);
	    } else {
		print WEBFILE $_;
	    }
	}
	close (WEBTEMPL);
	close (WEBFILE);
    }
    if ($config{"mwebfile"}) {
	open (WEBFILE, ">".$config{"mwebfile"});
	open (WEBTEMPL, $config{"mwebtemplate"});
	while (<WEBTEMPL>) {
	    if ($_ eq "<remailer-list here>\n") {
		open (MOTD, $config{"mmotd"});
		while (<MOTD>) {
		    $_ =~ s/\</\&lt\;/g;
		    $_ =~ s/\>/\&gt\;/g;
		    print WEBFILE;
		}
		close (MOTD);
		print WEBFILE "\n";
		print WEBFILE 'Last update: '.&time (localtime (time))."\n";
		print WEBFILE "mixmaster           history  latency  uptime\n";
		print WEBFILE "--------------------------------------------\n";
		print WEBFILE @mixmasters;
	    } elsif ($_ eq "<timing>\n") {
		&timing ();
	    } else {
		print WEBFILE $_;
	    }
	}
	close (WEBTEMPL);
	close (WEBFILE);
    }
    if ($config{"nums"}) {
	open (NUMS, '>'.$config{"nums"});
	print NUMS @nums;
	close (NUMS);
    }
    print "\n";
    print @mailers;
    print @mixmasters;
    print @nums;
}

sub time {
# $time = &time (localtime (time))
# Format an (already expanded time) nicely.
# Not consistent with HTTP standard.
    local (@time) = @_;
    local ($time);

    $time = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")[$time[6]];
    $time = "$time $time[3] ";
    $time = $time . ("Jan", "Feb", "Mar", "Apr", "May", "Jun",
		     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")[$time[4]];
    $time = "$time $time[5]";
    $time = sprintf ("%s %d:%02d:%02d ",
		     $time, $time[2], $time[1], $time[0]);
    $time = $time . ("PST", "PDT")[$time[8]];
    return $time;
}

sub timing {
# Print time since last modification of pubring.mix and type2.list
    local ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
	$atime,$mtime,$ctime,$blksize,$blocks);
    local ($now, $last_change, $staleness, $staledesc);

    $now = time;
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
     $atime,$mtime,$ctime,$blksize,$blocks)
	= stat($config{"type2"});
    $last_change = $mtime;
    ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
     $atime,$mtime,$ctime,$blksize,$blocks)
	= stat($config{"type2"});
    if ($mtime > $last_change) { $last_change = $mtime; }
    $staleness = $now - $mtime;
    print WEBFILE "<!--- $staleness seconds ago --->\n";
    $staledesc = '';
    if ($staleness >= 86400) {
	$staledesc .= sprintf ('%d days ', $staleness / 86400);
    }
    if ($staleness >= 3600) {
	$staledesc .= sprintf ('%d hours ', ($staleness % 86400) / 3600);
    }
    if ($staleness >= 60 && $staleness < 86400) {
	$staledesc .= sprintf ('%d minutes ', ($staleness % 3600) / 60);
    }
    if ($staleness >= 1 && $staleness < 3600) {
	$staledesc .= sprintf ('%d seconds ', $staleness % 60);
    }
    print WEBFILE $staledesc."ago.\n";
}
