#!/afs/athena/contrib/perl5/p

require '/var/local/www/data/recent-hp-changes/chat2.pl';
require '/var/local/www/data/recent-hp-changes/dateparse.pl';
#require '/afs/athena/user/m/k/mkgray/perl/chat2.pl';
#require '/afs/athena/user/m/k/mkgray/perl/dateparse.pl';
require LWP;
require HTTP::Response;
require HTTP::Request;

#$debug = 1;
$|=1;
$homedata = "/afs/sipb/project/www/data/home.DATA";

open(H, $homedata);
while(<H>){
    ($username, $url, $name, $alpha, $path, $date) = split("", $_);
    $url = "http://www.mit.edu".$url unless $url=~/^http/;
    push(@urls, $url);
    $name{$url}=$name;
}
$|=1;
foreach $u (@urls){
#    $ua = new LWP::UserAgent;
#    $req = new HTTP::Request 'GET', $u;
#    $r = $ua->request($req);
#    $age = $r->current_age;
#    print STDERR "$u ";
    ($headers, $body) = &head_url($u);
    $*=1;
    if($headers =~ /last-modified: (.+)/i){
	$lastmod = $1;
	$lastmod =~ s/[\n\r]*//g;
	$d = time()-($ds = &pgetdate($lastmod));
	if($ds == -1){
	    $d = -1;
	}
    }
    else{
	$lastmod = 0;
	$d = -1;
    }
    if($d > 604800){
	$e = int($d/604800)."weeks";
    }
    elsif($d > 86400){
	$e = int($d/86400)."days";
    }
    elsif($d > 3600){
	$e = int($d/3600)."hours";
    }
    elsif($d > 0){
	$e = int($d/60)."minutes";
    }
    else{
	$e = "unknown";
    }
    $mod{$u} = $d;
    print "$d $u $name{$u}\n";
    $ct++;
}

exit;
print "<h1 align=center></h1>\n";
print "<h1 aling=center>Recently modified Home Pages</h1>\n";
print "<ul>\n";
foreach $x (sort {$mod{$a} <=> $mod{$b}} keys %mod){
    $d = $mod{$x};
    if($d > 1209600){
	$e = int($d/604800)." weeks";
    }
    elsif($d > 172800){
	$e = int($d/86400)." days";
    }
    elsif($d > 7200){
	$e = int($d/3600)." hours";
    }
    elsif($d > 0){
	$e = int($d/60)." minutes";
    }
    if($d < 604800){
	print "<li><a href=$x>$name{$x}</a> ($e)\n" unless $mod{$x} < 0;
    }
}
print "</ul>\n";

sub head_url {
    # Returns headers and document
    local($url) = @_;
    $method = "HEAD";
    $method = "HEAD" if $url=~/\.gif$/i;
    print("######Get $url\n") if $main'debug;
    local($mysite, $mypath, $myport);
    local($doc, $handle, $headers, $document);
    $SIG{'ALRM'} = 'alarmhandle';
    alarm(15);
    ($mysite, $mypath, $myport) = &ParseURL($url);
    print("fetchin' $mysite $mypath $myport\n") if $main'debug;
    $mypath =~ s,//,/,g;
    $doc = ''; $headers = ''; $document = '';
    $handle =&chat'open_port($mysite, $myport) || do {print(STDERR "Failed following $mysite:$myport $!\n");return;
};
    &chat'print("GET $mypath HTTP/1.0\nUser-Agent: mkgray\@mit.edu>\n\n");
    $doc=&listen(12);
    &chat'close($handle);

    if($doc=~/\n\r?\n\r?/){
        $headers = $`;
        $document = $';
    }
    else{
        $doc =~ s/ /\./g;
        $document = $doc;
        $headers = "Weird 600 Idunno";
    }
    ($headers, $document);
}
sub ParseURL {
    # Returns site, path, port
    local($url) = @_;
    local(@path)=();    
    local($path) = "";
    ($proto, $garbage, $siteport, @path) = split('/', $url);
        print("Parsed $url\n") if $main'debug;
        print("into parts: @path\n") if $main'debug;
    next if ($proto ne 'http:');
    $port = 0;
    ($site, $port)=split(':', $siteport);
    if(!$port){
        $site = $siteport; $port = 80;
    }
    $pn = 0;
    for (@path){
        if($_ eq '..'){
            splice(@path, $pn-1, 2);
            $pn -=2;
        }
        if($_ eq '.'){
            splice(@path, $pn, 1);
            $pn -=1;
        }
        $pn++;
    }
    $path = '/'.join('/', @path);
        print("Path joined: $path\n") if $main'debug;
    $path .= '/' if substr($url, length($url)-1,1) eq '/';
        print("Path adjusted: $path\n") if $main'debug;
    return($site, $path, $port);
}
sub listen {
    local($secs) = @_;
    local($return,$tmp) = "";
    while (length($tmp = &chat'expect($secs, '(.|\n)+', '$&'))) {
        print $tmp if $trace;
        $return .= $tmp;
        (return $return) if (length($return) > 100000);
    }
    $return;
}
sub alarmhandle { print STDERR "Alarm!\n"; return; }
