#!/usr/local/bin/perl
#
# 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://leibniz.npac.syr.edu:1996/SPRINT/CM5/sprint-cm5.html" TARGET ="_parent">Back to previous page</a>|;
print qq|<HR>\n|;
#
#
#
print qq|<H2>Results - CM5</H2>\n|;
#
# 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://leibniz.npac.syr.edu:1996";
$bindir = "/home/B7A/pcrc_pod/cgi-bin/SPRINT/CM5";
$sprintdir = "/home/B7A/pcrc_pod/htdocs/SPRINT";
$graphdir = "$sprintdir/GRAPHS";
$history = "$sprintdir/RESULT/.history-cm5";
$javadir = "http://leibniz.npac.syr.edu:1996/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";
#
#
#
$query_string = "$graph+$file_r+$file_g";
$file_test = "$resultdir/RESULT/CM5/$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-cm5";
$lock = 0;
$lock = 1 if -f $lock_file;
if ($lock) {
    print qq|<H4>The SPRINT software is currently running for other user.</H4><BR>\n|;
    print qq|<H1><BLINK>Please wait</BLINK>..........</H1>\n<HR>\n|;
    print qq|<a HREF="$http/SPRINT/CM5/sprint-cm5.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);
}
#
# 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,">$file_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=part value=1>\n";
    print INIT "<param name=model value=$http/SPRINT/RESULT/GRAPHS/$graph.i>\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);
}
print qq|<a HREF="$http/SPRINT/RESULT/GRAPHS/$graph.i.html" TARGET="sprint_display2"><FONT SIZE=4>Initial Graph</FONT></a>: JAVA display of the initial graph.<BR>\n|;

#
# Partition the graph
#
$sprintcommand = "rsh swift $bindir/sprint $graphdir/node.$graph -d $bindir/.done -n $partitions -p $method -m $bindir/map.$graph -o $bindir/time.$graph";
open (TMP, "-|") || exec ($sprintcommand);
#
#
#
$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;
}
#
#
#
$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/CM5/$file_r.html\"";
open(TMP,"-|") || exec($command);
#while (<TMP>) {
#    print qq|$_<br>|;
#}
#close(TMP);
#
#
#
print qq|<a HREF="$http/SPRINT/RESULT/CM5/$file_r.html" TARGET="sprint_display2"><FONT SIZE=4>Result</FONT></a>: Results for $partitions partitions on the CM-5.<BR>\n|;

open(PARTITIONED,">$resultdir/$file_g.html");
print PARTITIONED "<HTML>\n";
print PARTITIONED "<CENTER>\n";
print PARTITIONED "<H2>Partitioned Graph</H2>\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 "<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></a>: JAVA display of the partitioned graph.<BR>\n|;
#
# Clean working space
#
$command = "rm $bindir/map.$graph $bindir/time.$graph $lock_file $bindir/.done";
open(TMP, "-|") || exec ($command);
#
# VRML output
#
print qq|VRML format will be available in the near future.<BR>\n|;
#
# Back to the previous page
#
print qq|<HR>\n|;
print qq|<a HREF="$http/SPRINT/CM5/sprint-cm5.html" TARGET="_parent">Back to previous page</a>\n|;
print qq|</HTML>|;
