#!/usr/local/bin/perl
#
# print out the HTML header
#
print qq|Content-type: text/html\n\n|;
print qq|<HTML><HEAD><TITLE>SPRINT (Partitioning On Demand)</TITLE></HEAD>\n|;
print qq|<H1>Partitioning On Demand (DEC Alpha)</H1>\n|;
print qq|<H2>Results - Sequential</H2>\n|;
#
# Get input information from system enviroment
#
$string = $ENV{'QUERY_STRING'};
($graph,$node,$edge,$random_dir,$partitioner,$partitions) = split(/[&]/,$string);
($name,$graph) = split(/[=]/,$graph);
($name,$node) = split(/[=]/,$node);
($name,$edge) = split(/[=]/,$edge);
($name,$random_dir) = split(/[=]/,$random_dir);
($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";
$ftpdir = "/incoming/pod/$random_dir";
$bindir = "/home/B7A/pcrc_pod/cgi-bin/SPRINT/SEQ";
$sprintdir = "/home/B7A/pcrc_pod/htdocs/SPRINT";
$graphdir = "$sprintdir/CLIENT";
$javadir = "$http/JAVA/";
$resultdir = $graphdir;
$mappingdir = $graphdir;
$reduce = "/home/B7A/pcrc_pod/cgi-bin/SPRINT/ALPHA/reduce";
$gen_result = "/home/B7A/pcrc_pod/cgi-bin/SPRINT/SEQ/gen_result";
$file_r = "$graph.$partitioner.$partitions.r";
$file_g = "$graph.$partitioner.$partitions.g";
#
# Lock the system for one user at a time
#
$lock_file = "$sprintdir/.lock-seq";
$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><BLINK>Please wait</BLINK></H1>..........\n<HR>\n|;
    print qq|</HTML>\n|;
    exit;
}
else {
    open(LOCK,">$lock_file");
    print LOCK "$ENV{'REMOTE_HOST'}";
    close(LOCK);
}
#
# Generate initial graph
#
$file_i = "$resultdir/$graph.i";
$file_exist = 1;
$file_exist = 0 if -f $file_i;
if ($file_exist) {
    $command = "rsh kestrel7 $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=model value=$http/SPRINT/CLIENT/$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>$node</TD>";
    print INIT "</TR></TABLE>\n";
    print INIT "</CENTER>\n";
    print INIT "</HTML>\n";
    close(INIT);
}
print qq|<a HREF="$http/SPRINT/CLIENT/$graph.i.html"><FONT SIZE=4>Initial Graph</FONT></a>: JAVA display of the initial graph.<BR>\n|;
#
# Partition the graph
#
$sprintcommand = "rsh kestrel7 $bindir/sprint $graphdir/node.$graph -d $bindir/.done -n $partitions -p $method -m $bindir/map.$graph -t $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 kestrel7 \"$gen_result $graphdir/node.$graph $graphdir/edge.$graph $bindir/map.$graph -m $method -n $partitions -t $bindir/time.$graph -o $resultdir/$file_g -r $resultdir/$file_r.html\"";
open(TMP,"-|") || exec($command);
#while (<TMP>) {
#    print qq|$_<br>|;
#}
#close(TMP);
#
#
#
print qq|<a HREF="$http/SPRINT/CLIENT/$file_r.html"><FONT SIZE=4>Result</FONT></a>: Results for $partitions partitions on a DEC Alpha workstation.<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/CLIENT/$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>$node</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/CLIENT/$file_g.html"><FONT SIZE=4>Partitioned Graph</FONT></a>: JAVA display of the partitioned graph.<BR>\n|;
#
# Generate FTP link
#
$command = "mv $bindir/map.$graph $mappingdir/map.$graph";
open(TMP, "-|") || exec ($command);
print qq|<a href="ftp://ftp.npac.syr.edu$ftpdir/map.$graph"><FONT SIZE=4>FTP mapping</FONT></a>: The result mapping for $partitions partitions on a DEC-Alpha
workstation<BR>\n|;
#
# Clean working space
#
$command = "rm $bindir/time.$graph $lock_file $bindir/.done";
open(TMP, "-|") || exec ($command);
#
# VRML
#
print qq|VRML format will be available in the near future.\n|;
print qq|</HTML>|;
