#!/usr/local/bin/perl

$thesisometer_url = "http://lava.sp.cs.cmu.edu/prog/thesisometer.pl";
$thesiswc = "/afs/cs/user/jab/p/thesis/thesis.wc";
$TZ = -5;  # time zone relative to London
$totwords = 40000; # estimate of how many words in the completed thesis!!
$barcolor = '9999FF';
$hours = 168.0; # default time interval on plot
&gethours;      # or override default from cgi environment

$| = 1;		# make output piping hot
&set_html_wrapper;

$donetime = 894560000; # the time when I finished!  set this to 0 if you're not done yet.

&getdata;
&buildpage;

exit(0);


sub set_html_wrapper {

    $num_experiments = `cat /afs/cs/project/learn/group/boss/scratch/ID`;

    $html_top = "
<HEAD>
  <TITLE>Justin's thesis page</TITLE>
</HEAD>
<BODY BGCOLOR=white>

<H1>Justin's thesis stats:</H1>

<TABLE>
<TR>
<TD align=right>Working Title:
<TH align=left> Learning Evaluation Functions for Global Optimization
<!--Efficient Value Function Approximation for Optimization-->

<TR>
<TD align=right>Date of proposal:
<TH align=left> June 17, 1996
   (<a href=\"http://www.cs.cmu.edu/~jab/pubs/propo/\">online proposal</a>)


<TR>
<TD align=right>Date of defense:
<TH align=left> May 13, 1998


<TR>
<TD align=right># experiments run:
<TH align=left> $num_experiments
</TABLE>

<br>
<br>
";

    $html_bottom = "
<hr>
Thanks for being visitor #
<a href=\"http://lava.sp.cs.cmu.edu/prog/stats.pl?thesis\">
<img src=\"http://lava.sp.cs.cmu.edu/prog/count-thesis.xbm\"></a>!
<br>
<a href=\"http://www.cs.cmu.edu/~jab/tom/\">So you want your own thesis-o-meter?</a>
<br>
<a href=\"http://sulfuric.graphics.cs.cmu.edu/~zoran/thesis-meter.cgi\">(Here's Zoran's)</a>
</BODY>
";
    
}


sub gethours {
    $query = $ENV{'QUERY_STRING'};
    for (split('&', $query)) {
	($tag,$value) = split('=', $_, 2);
	$value =~ s/\+/ /g;
	$value =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;

	if ($tag eq 'hours') {
	    $hours = 1;
	    while ($value =~ /(\d+)/g) {
		$hours *= $1;
	    }
	}
    }
}


sub getdata {

    open(WC, $thesiswc) || &error("couldn't open datafile");
    while(<WC>) {
	next unless /^(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s*(\d+)?$/;
	$date = $1;
	$lines{$date} = $2;
	$words{$date} = $3;
	$bytes{$date} = $4;
	$pages{$date} = $5;

	$totwords = $words{$date} if $words{$date} > $totwords;
    }
    @dates = sort {$a <=> $b} keys %words;
    $totwords = $words{$dates[$#dates]} if $donetime; # if we're done, we know the real total!

    $dtime = time - $dates[0];

    $spw = $dtime / ($words{$dates[$#dates]} - $words{$dates[0]});

    $tdone = $dates[0] + $spw * ($totwords - $words{$dates[0]});

    $tpages = $pages{$dates[0]} + (($tdone - $dates[0]) *
				   ($pages{$dates[$#dates]}-$pages{$dates[0]})/$dtime);
}

sub buildpage {
    
    $interval = $hours * 3600;  # interval, in seconds, between updates

    print "Content-type: text/html

<HTML>

$html_top

<a name=\"meter\"><H2>Thesis-o-meter!</H2></a>

<form action=\"$thesisometer_url\">
Interval: <input name=\"hours\" maxlength=5 size=5 value=$hours> hours
</form>


<TABLE border=0 cellspacing=0 cellpadding=1 width=100%>
<tr>
<th colspan=2> Date  <th> Pages <th colspan=2 align=left> Word count 
";

    # always start at 11:59:59pm on day before first date entry
    $row = int($dates[0]/(24*3600)) * (24*3600) -1 - $TZ*3600;
    $show_predict=1;

    for ($i=-1; $i < $#dates || $show_predict; $row += $interval) {

	if ($i < $#dates) {
	    # find most recent wordcount at time = $row
	    $advanced=0;
	    while (($i < 0) || ($i < $#dates && $dates[$i+1] <= $row)) {
		$i++;
		$advanced=1;
	    }
	    # now:  $dates[$i] <= $row; $dates[$i+1] > $row.
	    
	    $date = $advanced ? $dates[$i] : $row;
	    $words = $words{$dates[$i]};
	    $pages = $pages{$dates[$i]};
	    $message = '&nbsp;';
	}
	elsif ($donetime && time > $donetime) {
	    $date = $donetime;
	    $words = $totwords;
	    $pages = $pages{$dates[$#dates]};
	    $advanced = 1;
	    $show_predict = 0;
	    $troptions = 'bgcolor=99FF99';
	    $message = "&nbsp; <b>Rough draft completed!</b>";
	}
	else {  # show predicted finishing date bar
	    $date = $tdone;
	    $words = $totwords;
	    $pages = int($tpages);
	    
	    $advanced=1;
	    $show_predict = 0;
	    $troptions = 'bgcolor=FFFF00';
	    $message = "&nbsp; <b>Predicted rough draft completion</b>";
	}
	
	($sec,$min,$hour,$mday,$mon,$year) = localtime($date);
	$mon++;
	$am = ($hour >= 12) ? "pm" : "am";
	$hr = ($hour == 0) ? 12 : ($hour > 12) ? $hour-12 : $hour;
	print "<tr $troptions>\n";
	printf ("<td align=right> $mon/%02d, ", $mday);
	print "<td align=right>";
	printf("$hr:%02d$am:\n", $min) if $advanced;
	print "<td align=right> $pages\n";
	print "<td align=right> $words\n";

	print "<td width=100\%>  ";
	$pctg = 100 * $words / $totwords;
	print "<table width=$pctg\% border=0 cellspacing=0 cellpadding=0 bgcolor=$barcolor><tr><td width=100\%> $message </table>\n";
    }

    print "
</TABLE>

$html_bottom

</HTML>";


}


sub error {
    local($msg) = @_;
       print <<"EOM";
Status: 400 Bad Request
Content-type: text/plain

$msg
EOM
    exit(1);
}

