#!/usr/bin/perl
chdir "/html/kent";

# first, figure out which file to replace:

#   get a list of image files, sorted in inverse age order 
@flist = `ls -tr qc?.jpg`;

#   find out which run this is
$iter = `cat qc.iter`;
$iter++;
if( $iter > $#flist ) {
	$iter = 0;
}
open IFL, ">qc.iter" || die("couldn't open qc.iter file");
print IFL $iter;

#   overwrite the file with the appropriate relative age
system("cp /g/kent/qcam/t.jpg $flist[$iter]");

# second, dump out a web page with the appropriate stuff

#   standard header
open HFL, ">qcx.html" || die("couldn't create new html file");
print HFL "<html>\n<head>\n";
print HFL "<title>quickcam history</title>\n";
print HFL "<meta http-equiv=\"refresh\" content=\"600\">\n";
print HFL "</head>\n";
print HFL "<body bgcolor=\"#ffffff\">\n";
print HFL "<center>\n";
print HFL "<h2>songbird view history, with random numbers</h2>\n";
print HFL "<a href=\"qclive.html\">Live View</a><br>\n";
print HFL "(hex and decimal digits derived from md5 checksum of the image file)<br>\n";
print HFL "</center>\n";
print HFL " \n<hr>";

#   now the variable stuff
print HFL "<table>\n";

@flist = `ls -lt --full-tim qc?.jpg`;
foreach $i (0..$#flist) {
    ($date,$name) = $flist[$i] =~
    /\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+(\S+\s+\S+\s+\S+\s+\S+\s+\S+)\s+(\S+)/;
    $n0 = `md5 <$name`;
    @n = $n0 =~
        /^(....)(....)(....)(....)(....)(....)(....)(....)/;
    print HFL "<tr><td>\n";
    print HFL 
    "<img src=\"http://songbird.com/kent/$name\" width=320 height=240><br>";
    print HFL "</td>\n<td>\n";
    print HFL "$date<br>\n";
    print HFL "<pre>\n";
    foreach $j (0,2,4,6) {
        printf HFL "$n[$j] $n[$j+1] || %05d %05d\n",
            hex($n[$j]),hex($n[$j+1]);
    }
    print HFL "\n";
    print HFL "</pre></td></tr>\n";
}
print HFL "</table>\n";

# finally, print the trailing html
print HFL "<IMG SRC=\"http://songbird.com/pix/icons/llogo.gif\" ";
print HFL " ALIGN=CENTER>\n";
print HFL "<A HREF=\"http://songbird.com/index.html\">";
print HFL "[Songbird]</A></DL>\n</BODY>\n</HTML>";
close HFL;

# OK all is done -- overwrite the live html
system("cp qcx.html qc.html");
