#!/usr/bin/perl

my ($standard,$current)=@ARGV;

$standard=~s/[^\/\w]//g;
$current=~s/[^\/\w]//g;
my %svhash;
my %sbhash;

my @oldrpms;
my @badversionrpms;
my @newrpms;
my @externalrpms;

open IN, $standard;
while(<IN>) {
    chop;
    my ($name,$version,$buildtime)=split(/\s+/);
    $svhash{$name}=$version;
    $sbhash{$name}=$buildtime;
}
close IN;

open IN, $current;
while(<IN>) {
    chop;
    my ($name,$version,$buildtime)=split(/\s+/);
    if ($svhash{$name}) {
	unless ($svhash{$name} eq $version) {
	    push @badversionrpms,"$name is version $version, should be $svhash{$name}\n";
	}
	if ($sbhash{$name}<$buildtime) {
	    push @newrpms,"$name was built ".localtime($buildtime)." when the expected build time was ".localtime($sbhash{$name})."\n";
	}
	if ($sbhash{$name}>$buildtime) {
	    push @newrpms,"$name was built ".localtime($sbhash{$name})." when the expected build time was ".localtime($buildtime)."\n";
	}
    }
    else {
	push @externalrpms,"$name (built ".localtime($buildtime).") is an RPM not expected to ever be on a LON-CAPA system\n";
    }
}
close IN;

my $date=`date`; chop $date;
my $hostname=`hostname`;
print <<END;
<HTML>
<HEAD>
<TITLE>LON-CAPA Software RPM Status Page</TITLE>
</HEAD>
<BODY>
<FONT SIZE=+2>LON-CAPA Software RPM Status Page (done on $date for $hostname)</FONT>
<BR>Michigan State University
<BR>Learning Online with CAPA
<BR>Contact korte@lon-capa.org
<UL>
<LI>About this file</LI>
<LI>Bad RPM Versions</LI>
<LI>Out-of-date RPMS</LI>
<LI>Newer than expected RPMS</LI>
<LI>RPMS external to LON-CAPA</LI>
</UL>
<FONT SIZE=+2>About this file</FONT>
<P>
This file is generated dynamically by <tt>make rpmstatuspost</tt>
when this command is entered in the CVS:loncom/build directory.
</P>
END
    print <<END;
<FONT SIZE=+2>Bad RPM Versions</FONT>
<PRE>
END
foreach my $rpminfo (@badversionrpms) {
    print $rpminfo;
}
print <<END;
</PRE>
END
print <<END;
<FONT SIZE=+2>Out-of-date RPMS</FONT>
<PRE>
END
foreach my $rpminfo (@oldrpms) {
    print $rpminfo;
}
print <<END;
</PRE>
END
print <<END;
<FONT SIZE=+2>Newer than expected RPMS</FONT>
<PRE>
END
foreach my $rpminfo (@newrpms) {
    print $rpminfo;
}
print <<END;
</PRE>
END
print <<END;
<FONT SIZE=+2>RPMS external to LON-CAPA</FONT>
<PRE>
END
foreach my $rpminfo (@externalrpms) {
    print $rpminfo;
}
print <<END;
</PRE>
END
