#!/usr/local/bin/perl
##########################################################################
##########################################################################
	open (Config_file, '/home/T10D/vself/Project/Config')
		|| die "Cannot open Configuration File";


	&PrintHeader;
	&ReadEntry;
	if (!($path eq $cpath)) {
		&FIND_FIRST_FILE;
	}
	else {     
		&FIND_WHICH_FILE;
	}
	close(Config_file);
	&PRINT_FORM;
	$current = '/home/T10D/vself/Project/' . $current;
	if (-r $current)  {
		&PRINT_FILE_CONTENTS;
	}
    elsif (($current eq '/home/T10D/vself/Project/View') 
			|| ($current eq '/home/T10D/vself/Project/Path')
			|| ($current eq '/home/T10D/vself/Project/')){
		print "<FONT SIZE=\"+3\"><CENTER>\n";
		print "You have reached the end of the path<BR>\n";
		print "Please select another path.<BR>\n";
		print "</CENTER></FONT>\n";
	}
	else  {
		print "Could not read filename: ", $current,"\n";
	}
	&PRINT_ADDRESS;


###########################################################################
##########################################################################

sub ReadEntry {
	&ReadParse;				#Read arguments passed from form
	$route = $in{'route'};
	($view, $path) = split(': ',$in {'route'},2);
	$direction = $in{'direction'};
	$current_file = $in {'current'};
	$cpath = $in{'cpath'};
	return 1;
}



##########################################################################
##########################################################################
# Returns the magic line which tells WWW that this is an HTML document

sub PrintHeader {
  print "Content-type: text/html\n\n";
  print "\n";
}

##########################################################################
#						Subroutine ReadParse                             #
#						                                                 #
# reads the arguments passed from the form                               #
##########################################################################
sub ReadParse {
    local ($i, $loc, $key, $val);

    if ($ENV{'REQUEST_METHOD'} eq "GET") {                      # Read in text
       $in = $ENV{'QUERY_STRING'};
    } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
      for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
        $in .=getc;
      }
    }

    @in = split(/&/,$in);

    foreach $i (0 .. $#in) {
      $in[$i] =~ s/\+/ /g;                          # Convert plus's to spaces
      $in[$i] =~ s/%(..)/pack("c",hex($1))/ge; 
                      # Convert %XX from hex numbers to alphanumeric
      $loc = index($in[$i], "=");                  # Split onto key and value.
      $key = substr($in[$i],0,$loc);
      $val = substr($in[$i],$loc+1);
      $in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
      $in{$key} .= $val;
   }
   return 1;
}



##########################################################################
##########################################################################

sub GET_APPLICATION {
	$name = "";
	read (Config_file,$char,1);
	while (!($char eq "\n") && !($char eq "|")){
		$name = $name . $char;
		read (Config_file,$char,1);
   }
}

##########################################################################
##########################################################################

sub GET_NAME {
	$name = "";
	read (Config_file,$char,1);
	while (!($char eq "|") && !($char eq ",") && !($char eq ":")){
		if (! ($char eq " ")  && !($char eq "\n")){
			$name = $name . $char;
		}
		read (Config_file,$char,1);
   }
}


##########################################################################
##########################################################################

sub readln {
	do {
		read (Config_file,$char,1);
	} until ($char eq "\n");
}

##########################################################################
##########################################################################

sub PRINT_FORM{

	print "<HEAD>\n\n";
	print "<FONT SIZE =\"+4\">\n";
	if (!($current eq "View") && !($current eq "Path") 
			&& !($current eq "")){
		print "<TITLE>",$current, "</TITLE>\n";
		print "<CENTER>" ,$current, "</CENTER>\n";
	}
	else {
		print "<TITLE>End of Path</TITLE>\n";
	}
	print "</FONT>\n";
	print "</HEAD>\n\n";

	print "<BODY>\n\n";

	print "<HR>\n\n";


	print "<FORM METHOD=\"POST\" 
		ACTION=\"http://deneb.npac.syr.edu:3768/cgi-bin/old-viewer.pl\">\n\n\n";

	print "<INPUT TYPE=\"RADIO\" NAME=\"direction\" 
						VALUE=\"PREVIOUS\">Previous\n";
	print "<INPUT TYPE=\"RADIO\" NAME=\"direction\" 
						VALUE=\"NEXT\"checked>Next\n"; 

	print "<INPUT TYPE=HIDDEN NAME=current value=", $current ,">";
	print "<INPUT TYPE=HIDDEN NAME=cpath value=", $path , ">";

#	print "<SELECT NAME=\"current\">\n";
#	print "    <OPTION>New Path \n\n";
#	print "	   <OPTION SELECTED VALUE=$current>Current file: ", $current;
#	print "</SELECT>\n";
	print "<SELECT NAME=\"route\">\n";

	if ($route eq "Applications: Transformations") {
		print "    <OPTION SELECTED>Applications: Transformations\n";}
	else{
		print "    <OPTION>Applications: Transformations\n";}
	if ($route eq "Applications: ParkBench") {
		print "    <OPTION SELECTED>Applications: ParkBench\n";}
	else{
		print "    <OPTION>Applications: ParkBench\n";}
	if ($route eq "Applications: ParkBench") {
		print "    <OPTION SELECTED>Applications: Integrate\n";}
	else {
		print "    <OPTION>Applications: Integrate\n";}
	if ($route eq "Languages: F90") {
		print "    <OPTION SELECTED>Languages: F90\n"; }
	else{
		print "    <OPTION>Languages: F90\n";}

	print "</SELECT>\n\n";

	print "<INPUT TYPE=\"SUBMIT\" VALUE=\"View File\">\n";


	print "</FORM>\n\n";


	print "<P>\n";
	print "<HR>\n";

}


##########################################################################
##########################################################################

sub PRINT_FILE_CONTENTS{

	$title = $header = $current;

    open(INPUT_FILE,$current) || die "Can not open filename: ", $current;

    print "<FONT SIZE =\"+1\">\n";
    print "<PRE>\n";


    while (! eof(INPUT_FILE)) {
        read(INPUT_FILE,$char,1);
        print $char;
    }
    print "</PRE>\n";
    print "</FONT>\n";
    print "</BODY>\n";
}


##########################################################################
##########################################################################

sub PRINT_ADDRESS {


	print "<HR>\n\n";

	print "<A HREF=\"http://www.cs.rice.edu/CRPC/retool/\" > Return to 
				Retooling Home Page </A>\n";

	print "<HR>\n\n";
	print "<ADDRESS>\n\n";

	print "For more information about or to comment on this page, contact\n\n";
	print "Vareck R. Self, <A HREF=\"mailto: vself\@npac.syr.edu\">\n";
	print "vself\@npac.syr.edu</A>, or Nancy McCracken, <A HREF=\"mailto:\n";
	print "njm\@npac.syr.edu\">njm\@npac.syr.edu</A>\n\n";

	print "</ADDRESS>\n";

}

##########################################################################
##########################################################################

sub FIND_FIRST_FILE{ 
   $previous = "NIL";
    if ($view eq "Applications"){
        if ($path eq "Transformations"){
            $current = "laplace.f90";
		}
        elsif ($path eq "Integrate"){
            $current = "integrateSimp.f90"; 
		}
        else{
            $current = "aa.f90"; 
		}
    }
    elsif ($view eq "Languages"){
        if ($path eq "F90"){
            $current = "aa.f90";
		}
	}
}
##########################################################################
##########################################################################

#searching the file for the view that matches the one that we are looking for

sub FIND_WHICH_FILE {

	read (Config_file,$tag,6);

    &GET_APPLICATION($name); 
	$current = "";
	while (!($name eq $view)){
		&readln;
		read (Config_file, $tag, 6);
    	&GET_APPLICATION($name); 
	}
	
#searching the file for the path that matches the one that we are looking for

	read (Config_file,$tag,6);
	&GET_NAME($name); 
	while (!($name eq $path)){
		&readln;
		read (Config_file, $tag, 6);
 		&GET_NAME($name); 
	}

#An attempt to match the current file name with wanted one


	&GET_NAME($name);
	while (!($current eq $current_file)){
		$previous = $current;
        $current = $next;
		$next = $name;
		&GET_NAME($name);
	}

	if ($direction eq "PREVIOUS"){
		$current = $previous;
	}
	else  {
		$current = $next;
	}
}
