#!/usr/local/bin/perl

%desc = ("official", "Official Info",
	"sipb", "SIPB Home Page",
	"linkmithp", "Link to the MIT Home Page",
	"studhp", "Student Home Pages",
	"linksipbhp", "Link to the SIPB Home Page",
	"link", "Some other link",
	"service", "Service on www.mit.edu/",
	"bookmark", "Bookmark",
	"other", "Other"
	);
print "Content-type: text/html\n\n";

opendir(D, "/var/local/www/data/survey/");
@r = readdir(D);
closedir(D);

foreach $r (@r){
	next if (($r eq ".") || ($r eq ".."));
	open(F, "/var/local/www/data/survey/$r");
	@v = <F>;
	close(F);
	foreach $v (@v){
		($v, @blah) = split(" ", $v);
		$asum++;
		$acount{$r}++;
		if($seen{$v}){
		    next if $canceled{$v};
		    $count{$seen{$v}}--;
		    $sum--;
		    $canceled{$v}=1;
		    next;
		}
		$count{$r}++ unless $seen{$v};
		$sum++ unless $seen{$v};
		$seen{$v}=$r;
		if($r eq "other"){
			push(@others, join(" ", @blah));
		}
	}
}

print("<h1>Survey Results</h1>\nTotal of $sum responses from unique hosts, $asum including duplicates\n");

print("<table>\n");
print "<tr><th>Answer</th><th>Count, unique hosts(%)</th><th>Count, total (%)</th></tr>\n";
foreach $r (sort {$count{$b} <=> $count{$a}; } (keys %count)){
	$perc = int(100*$count{$r}/$sum);
	$aperc = int(100*$acount{$r}/$asum);
	print("<tr><td>$desc{$r}</td><td align=center>$count{$r} ($perc%)</td><td align=center>$acount{$r} ($aperc%)</td></tr>\n");
}

print("</table>\n");

$sipb = $count{"studhp"}+$count{"service"}+$count{"bookmark"}+$count{"sipb"}+$count{"linksipbhp"};
$off = $count{"official"}+$count{"linkmithp"};

$asipb = $acount{"studhp"}+$acount{"service"}+$acount{"bookmark"}+$acount{"sipb"}+$acount{"linksipbhp"};
$aoff = $acount{"official"}+$acount{"linkmithp"};

$psipb = int(100*$sipb/$sum);
$poff = int(100*$off/$sum);

$pasipb = int(100*$asipb/$asum);
$paoff = int(100*$aoff/$asum);

print("<hr>Roughly: $psipb% looking for SIPB stuff, $poff% looking for web.mit.edu stuff (unique hosts)<br>\n");
print("Roughly: $pasipb% looking for SIPB stuff, $paoff% looking for web.mit.edu stuff (all answers)\n");
print("<hr>Of the \"Other\" category, these were the answers:<br>\n");
print join("<br>\n", @others);
