#!/afs/athena/contrib/perl/perl

$pageopt = shift(@ARGV);
$pageopt =~ s/all/links-images-length-cool-nifty/;
while(<>){
#    ($page, $url, $got, $it, $nlinks, $links, $nimages, $images,
#     $len, $length, $done) = split;
    ($url, $url, $nlinks, $nimages, $len, $uimgs, $date) = split;
    chop($url);
    next if $nlinks =~ /\D/;
    next if $nimages =~ /\D/;
    next if $len =~ /\D/;
    $linklist{$url} = $nlinks;
    $imagelist{$url} = $nimages;
    $lenlist{$url} = $len;
    $coollist{$url} = ($nlinks*$nlinks - $nimages*$nimages)/$len if $len;
    $niftylist{$url} = $nlinks*$nimages + $len;
}

if($pageopt =~ /links/){
    print("<h1>Pages Sorted by Number of links in them</h1>\n<ul>\n");
    @linkstop = sort {$linklist{$b} <=> $linklist{$a};} keys %linklist;
    for $l (@linkstop){
	$ol = $l;
	$l = "http://www.mit.edu:8001".$l if substr($l, 0, 1) eq "/";
	print("<li>($linklist{$ol})<a href=$l>$l</a>\n");
    }
    print("</ul>\n");
}

if($pageopt =~ /images/){
    print("<h1>Pages Sorted by Number of images in them</h1>\n<ul>\n");
    @imagestop = sort {$imagelist{$b} <=> $imagelist{$a};} keys %imagelist;
    for $l (@imagestop){
	$ol = $l;
	$l = "http://www.mit.edu:8001".$l if substr($l, 0, 1) eq "/";
	print("<li>($imagelist{$ol})<a href=$l>$l</a>\n");
    }
    print("</ul>\n");
}

if($pageopt =~ /len/){
    print("<h1>Pages Sorted by Length</h1>\n<ul>\n");
    @lenstop = sort {$lenlist{$b} <=> $lenlist{$a};} keys %lenlist;
    for $l (@lenstop){
	$ol = $l;
	$l = "http://www.mit.edu:8001".$l if substr($l, 0, 1) eq "/";
	print("<li>($lenlist{$ol})<a href=$l>$l</a>\n");
    }
    print("</ul>\n");
}


if($pageopt =~ /cool/){
    print("<h1>Pages Sorted by \"Cool\"ness formula</h1>\n<ul>\n");
    @coolstop = sort {$coollist{$b} <=> $coollist{$a};} keys %coollist;
    for $l (@coolstop){
	$ol = $l;
	$l = "http://www.mit.edu:8001".$l if substr($l, 0, 1) eq "/";
	print("<li>($coollist{$ol})<a href=$l>$l</a>\n");
    }
    print("</ul>\n");
}

if($pageopt =~ /nifty/){
    print("<h1>Pages Sorted by \"Nifty\"ness formula</h1>\n<ul>\n");
    @niftystop = sort {$niftylist{$b} <=> $niftylist{$a};} keys %niftylist;
    for $l (@niftystop){
	$ol = $l;
	$l = "http://www.mit.edu:8001".$l if substr($l, 0, 1) eq "/";
	print("<li>($niftylist{$ol})<a href=$l>$l</a>\n");
    }
    print("</ul>\n");
}
