#!/usr/local/bin/perl
#
# Get input information from system enviroment
#
$string = $ENV{'QUERY_STRING'};
($graph,$partitioner,$partitions) = split(/[&]/,$string);
($name,$graph) = split(/[=]/,$graph);
($name,$partitioner) = split(/[=]/,$partitioner);
($name,$partitions) = split(/[=]/,$partitions);
#
#
#
$method = "0" if $partitioner eq "rcb";
$method = "1" if $partitioner eq "rib";
$method = "2" if $partitioner eq "ibph";
$method = "3" if $partitioner eq "ibpz";
#
#
#
$http = "http://dante.npac.syr.edu:1996";
$bindir = "/home/B7A/pcrc_pod/cgi-bin/SPRINT/ALPHA";
$sprintdir = "/home/B7A/pcrc_pod/htdocs/SPRINT";
$graphdir = "$sprintdir/GRAPHS";
$history = "$sprintdir/RESULT/.history-alpha";
$javadir = "$http/JAVA/";
$resultdir = "$sprintdir/RESULT";
$reduce = "/home/B7A/pcrc_pod/cgi-bin/SPRINT/ALPHA/reduce";
$gen_result = "/home/B7A/pcrc_pod/cgi-bin/SPRINT/ALPHA/gen_result";
$file_r = "$graph.$partitioner.$partitions.r";
$file_g = "$graph.$partitioner.$partitions.g";
#
# print out the HTML header
#
print qq|Content-type: text/html\n\n|;
print qq|<HTML><HEAD><TITLE>SPRINT Demonstration</TITLE></HEAD>\n|;
print qq|<a HREF="http://dante.npac.syr.edu:1996/SPRINT/ALPHA/sprint-alpha.html" TARGET ="_parent">Back to previous page</a>|;
print qq|<HR>\n|;
#
#
#
$query_string = "$graph+$file_r+$file_g";
$file_test = "$resultdir/RESULT/ALPHA/$file_r.html";
$file_exist = 1;
$file_exist = 0 if -f $file_test;
if ($file_exist) {
    open(HISTORY,">>$history");
    print HISTORY "$query_string\n";
    close(HISTORY);
}
#
#
#
$lock_file = "$sprintdir/.lock-alpha";
$lock = 0;
$lock = 1 if -f $lock_file;
if ($lock) {
    print qq|<FONT SIZE=4>The SPRINT software is currently running for other user.</FONT><BR>\n|;
    print qq|<H1>Please wait</H1>..........<BR>\n<HR>\n|;
    print qq|<a HREF="$http/SPRINT/ALPHA/sprint-alpha.html" TARGET ="_parent">Back to previous page</a>|;
    print qq|</HTML>\n|;
    exit;
}
else {
    open(LOCK,">$lock_file");
    print LOCK "$ENV{'REMOTE_HOST'}";
    close(LOCK);
}
print qq|<H2>Results</H2>\n|;
#
# Generate initial graph
#
$file_i = "$sprintdir/RESULT/GRAPHS/$graph.i";
$file_exist = 1;
$file_exist = 0 if -f $file_i;
if ($file_exist) {
    $command = "rsh kestrel1 $reduce $graphdir/node.$graph $graphdir/edge.$graph -o $file_i";
    open (TMP, "-|") || exec ($command);
    open(INIT,">$resultdir/$graph.i.html");
    print INIT "<HTML>\n";
    print INIT "<CENTER>\n";
    print INIT "<H2>Initial Graph</H2>\n";
    print INIT "In order to rotate the following graph, please push a mouse botton inside the frame and drag it around.\n";
    print INIT "<P>\n";
    print INIT "<TABLE BORDER>\n";
    print INIT "<TR><TD>\n";
    print INIT "<applet codebase=$javadir code=ThreeD.class width=400 height=400>\n";
    print INIT "<param name=model value=$http/SPRINT/RESULT/GRAPHS/$graph.i>\n";
    print INIT "<param name=part value=1>\n";
    print INIT "</applet>\n";
    print INIT "</TD></TR>\n";
    print INIT "</TABLE>\n";
    print INIT "<TABLE BORDER><TR>\n";
    print INIT "<TD>Graph</TD>\n";
    print INIT "<TD>$graph</TD>";
    print INIT "</TR></TABLE>\n";
    print INIT "</CENTER>\n";
    print INIT "</HTML>\n";
    close(INIT);
}
#
# Partition the graph
#
open(EPG,">$bindir/sprint.pg");
print EPG "local 0\n";
$i = 1;
while ($i < $partitions) {
    $i = $i + 1;
    print EPG "kestrel$i 1 $bindir/sprint\n";
}
close(EPG);

open(SPRINT_IN,">$bindir/SPRINT.in");
print SPRINT_IN "$graphdir/node.$graph\n";
print SPRINT_IN "$bindir/.done\n";
print SPRINT_IN "$method\n";
print SPRINT_IN "$bindir/map.$graph\n";
print SPRINT_IN "$bindir/time.$graph\n";
close(SPRINT_IN);
#
#
#
$command = "rsh kestrel1 \"$bindir/sprint -p4pg $bindir/sprint.pg\"";
open(TMP, "-|") || exec ($command);
#
#
#
print qq|<a HREF="$http/SPRINT/RESULT/GRAPHS/$graph.i.html" TARGET="sprint_display2"><FONT SIZE=4>Initial Graph</FONT>: JAVA display of the initial graph.</a><BR>\n|;
#
#
#
$chk_file = "$bindir/.done";
$exist = 1;
$exist = 0 if -e $chk_file;
$exist = 1 if -z $chk_file;
while ($exist) {
    $exist = 0 if -e $chk_file;
    $exist = 1 if -z $chk_file;
}
#
#
#
print qq|<a HREF="$http/SPRINT/RESULT/ALPHA/$file_r.html" TARGET="sprint_display2"><FONT SIZE=4>Result</FONT>: Results on $partitions-node Alpha-Farm.</a><BR>\n|;
#
#
#
$command = "rsh kestrel1 \"$gen_result $graphdir/node.$graph $graphdir/edge.$graph $bindir/map.$graph -m $method -t $bindir/time.$graph -o $resultdir/$file_g -r $resultdir/ALPHA/$file_r.html\"";
open(TMP, "-|") || exec ($command);
#
#
#
open(PARTITIONED,">$resultdir/$file_g.html");
print PARTITIONED "<HTML>\n";
print PARTITIONED "In order to rotate the following graph, please push a mouse botton inside the frame and drag it around.\n";
print PARTITIONED "<P>\n";
print PARTITIONED "<CENTER>\n";
print PARTITIONED "<TABLE BORDER>\n";
print PARTITIONED "<TR><TD>\n";
print PARTITIONED "<applet codebase=$javadir code=ThreeD.class width=400 height=400>\n";
print PARTITIONED "<param name=model value=$http/SPRINT/RESULT/$file_g>\n";
print PARTITIONED "<param name=part value=$partitions>\n";
print PARTITIONED "</applet>\n";
print PARTITIONED "</TD></TR>\n";
print PARTITIONED "</TABLE>\n";
print PARTITIONED "<TABLE BORDER><TR>\n";
print PARTITIONED "<TD>Graph</TD><TD>Partitioner</TD><TD>Partitions</TD>\n";
print PARTITIONED "</TR><TR>\n";
print PARTITIONED "<TD ALIGN=RIGHT>$graph</TD><TD ALIGN=RIGHT>$partitioner</TD><TD ALIGN=RIGHT>$partitions</TD>\n";
print PARTITIONED "</TR></TABLE>\n";
print PARTITIONED "</CENTER>\n";
print PARTITIONED "</HTML>\n";
close(PARTITIONED);
print qq|<a HREF="$http/SPRINT/RESULT/$file_g.html" TARGET="sprint_display2"><FONT SIZE=4>Partitioned Graph</FONT>: JAVA display of the partitioned graph.</a><BR>\n|;
#
# Clean working space
#
$command = "rm $bindir/map.$graph $bindir/time.$graph $bindir/sprint.pg $bindir/SPRINT.in $bindir/.done $lock_file";
open(TMP, "-|") || exec ($command);
#while (<TMP>) {
#    print qq|$_<br>|;
#}
#close(TMP);
#
# Back to the previous page
#
print qq|<HR>\n|;
print qq|<a HREF="$http/SPRINT/ALPHA/sprint-alpha.html" TARGET="_parent">Back to previous page</a>\n|;
print qq|</HTML>|;
