#!/usr/bin/perl
# -v = verbose, -a = args only, -x = just index, -n = don't do anything
umask 002;

require "ctime.pl";
use Getopt::Std;

$home = "/var/lib/lists-archives";
$timestamp = "$home/timestamp";
$mhonarc = "/usr/bin/mhonarc -expireage 2419200000";
$glimpsedir = "/usr/bin/glimpse";
$rcfile = "/etc/lists-archives/standard.rc";
$archivedir = "$home/lists";
$webdir = "$home/archives";
$glwork = $archivedir;
$indexorig = "/etc/lists-archives/index.html";
$buildindex = "/usr/lib/lists-archives/buildindex";

@months   = ('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug',
             'Sep', 'Oct', 'Nov', 'Dec');

require "/etc/lists-archives/conf.pl";

getopts('vaxn') || die;
$quiet = $opt_v ? "" : "-quiet";

# N=name, B=blurb, MN=month name, M=month, Y=year, YY=long year
$browseb = '<ul>
';
$browsee = '</ul>
';
$listb = '<li><strong>$N$</strong>: $B$<ul>
';
$liste = '</ul></li>
';
$yearb = '<li>$YY$
';
$yeare = '</li>
';
$listmonth = '[<a href="$N$-$Y$$M$/threads.html">$MN$</a>]
';
$listyear  = '<li><a href="$N$-$Y$/threads.html">$YY$</a></li>
';

for $a (@ARGV) { &update($a); }
&makeindex if $opt_x;

if (!$opt_a && !$opt_x) {
    $lastupdate = &timeupdate(0);

    opendir (D, $archivedir) || die "$!";
    while ($_ = readdir D) {
	next if !/\d\d$/;
	next if ((stat "$archivedir/$_")[9] < $lastupdate);
	print "----> Updating $_\n" if $opt_v;
	$updateidx |= &update($_);
    }
    &makeindex() if $updateidx;
    &timeupdate(1);
}

sub timeupdate {
    my $upd = shift;
    my $l = 0;
    if (-f $timestamp) {
	open (F, "$timestamp") || die;
	chop ($l = <F>);
    }
    print "----> Last update: ",&ctime($l) if $opt_v;
    if ($upd && !$opt_n) {
	open (F, "> $timestamp") || die;
	print F "$^T\n";
	close F || die;
	print "----> Now: ",&ctime($^T) if $opt_v;
    }
    $l;
}


sub update {
    my($file) = @_;

    $dir = $file;
    if (! -d "$webdir/$dir") {
	mkdir "$webdir/$dir",0755 || die "$!";
	symlink "threads.html", "$webdir/$dir/index.html" || die "$!";
	$idx = 1;
    }

    ($title = $dir) =~ s/-(\d\d)(\d\d)?$//;
    $title = $2 ? "\"$title $months[$2-1] $1\"" : "\"$title $1\"";

    systemcall("$mhonarc $quiet -rcfile $rcfile -outdir $webdir/$dir "
	       ."-title $title -ttitle $title -add $archivedir/$file");

    systemcall("$buildindex $file" . ($opt_v ? "" : "> /dev/null"));

    $idx;
}


sub makeindex {
    print "----> Writing index\n" if $opt_v;
    return if $opt_n;
    open (O, "$indexorig") || die "$!";
    open (F,"> $webdir/index.html") || die "$!";
    while ($l=<O>) {
	if ($l =~ /^BROWSE$/) {
	    print F $browseb;
	    $lastl = "";
	    $lastlevel = -1;
	    for $currentlevel (0..$maxgrouplevel) {
		opendir (D, $archivedir) || die "$!";
		for $_ (y2kDirSort(sort readdir D)) {
		    next if !/\d\d$/;
		    ($name, $year, $month) = /^(\S*)-(\d\d)(\d\d)?$/;
		    $monthlist{"$year$month"} = 1 if ($month % 3 == 1);
		    ($level, $blurb) = split(':',$blurb{$name});
		    ($level, $blurb) = ($maxgrouplevel, "")
			if (!$blurb{$name});
		    next if ($level != $currentlevel);
		    $mn = $months[$month-1];
		    print F &vexpand($yeare)
			if ($lasty && $month && $lasty ne $year);
		    print F &vexpand($liste) if ($last && $last ne $name);
		    print F <<EOF
</ul><p>
	<table width="100%" border="0" cellspacing="0" cellpadding="5">
	<tr><td width="100%" bgcolor="#fff0d0"><font color="#000000">
        <b>$grouptitle[$currentlevel]</b>
	</font></td></tr> </table>
<p><ul>
EOF
			if ($currentlevel != $lastlevel);
		    if ($last ne $name) {
			print F &vexpand($listb);
			$lasty = "";
		    }
		    print F &vexpand($yearb) if ($month && $lasty ne $year);
		    $last = $name; $lasty = $year; $lastlevel = $level;
		    print F ($month ? &vexpand($listmonth) :
			     &vexpand($listyear));
		}
	    }
	    print F $yeare if ($month);
	    print F $liste, $browsee;
	}
	elsif ($l =~ /^DATELIST$/) {
	    for $m (sort y2ksort keys %monthlist) {
		next if ($m !~ /\d{4}/);
		print F "<option>", $months[$m%100-1], " to ",
		$months[$m%100+1], " ", int($m / 100) < 10 ? "0".int($m / 100) : int($m / 100), "\n";
	    }
	}
	elsif ($l =~ /^GROUPLIST$/) {
	    opendir (D, $archivedir) || die "$!";
	    for $_ (readdir D) {
		next if !/\d\d$/;
		($name, $year, $month) = /^(\S*)-(\d\d)(\d\d)?$/;
		$name =~ s/^debian\-//;
		$listlist{$name} = 1;
	    }
	    for $_ (sort keys %listlist) { print F "<option> $_\n"; }
	}
	else {
	    print F $l;
	}
    }
    close F || die;
}

sub systemcall {
    my $cmd = shift;
    print "$cmd\n" if $opt_v;
    if (!$opt_n) { system($cmd) && die "$cmd: $?" }
}



sub vexpand {
    my($s) = @_;
    my $century = $year < 90 ? 20 : 19;
    $s =~ s/\$N\$/$name/g;
    $s =~ s/\$B\$/$blurb/g;
    $s =~ s/\$MN\$/$mn/g;
    $s =~ s/\$M\$/$month/g;
    $s =~ s/\$Y\$/$year/g;
    $s =~ s/\$YY\$/$century$year/g;
    $s;
}
sub y2kDirSort
{
        my @dir = @_;
        my @sortedDir;
        my $i = 0;
        for $_ (@dir)
        {
                next if !/\d\d$/;
                $_ =~ /^\S*-(\d\d)\d\d?$/;
                my $year = $1;
                if($year >= 50)
                {
                        $sortedDir[$i] = $_;
                        $i++;
                }
        }
        for $_ (@dir)
        {
                next if !/\d\d$/;
                $_ =~ /^\S*-(\d\d)\d\d?$/;
                my $year = $1;
                if($year < 50)
                {
                        $sortedDir[$i] = $_;
                        $i++;
                }
        }
        return @sortedDir;
}

sub y2ksort
{
	my $year1 = int($a /100);
	my $month1 = $a % 100;
	my $year2 = int($b /100);
	my $month2 = $b % 100;

	return 1 if(($year1 >= 50) && ($year2 < 50));
        return -1 if(($year1 < 50) && ($year2 >= 50));
	return $b <=> $a;
}
