#! /usr/local/bin/perl
############################################################################
#
# File  : MM.pl
# Author: Jason Krug
# Input : <none>
# Output: A http link to 1 or 4 Physic Servers
# 
############################################################################

############################################################################
# Main Function
#
&ReadEntry &&
&GenerateHttp;

############################################################################
# Generates the http address of a Physics Server
#
sub GenerateHttp
{ @sites = ('http://altair.syr.edu:2024/MM', 'http://laura.syr.edu:2024/MM', 
  'http://diphda.npac.syr.edu:2024/MM', 'http://shaman.syr.edu:2024/MM');

######### This is a little randomizer fromula that I have developed....
######### It spreads the distribution pretty well
#
  srand(time * getppid());
  $presite = int(rand($#sites+1));
  $site=$presite;
  for ($x = -1; $x <= $presite; $x++)
    { srand (time * getppid());
      $site= int(rand($#sites+1));
    }

######### Now I have to set up the Web Page.
#
  print "Content-type: text/html\n\n";
  print "\n <HTML> <HEAD>\n";
  print "<! (C) 1995 Simon Catterall> <LINK HREF=\"mailto:smcnpac.syr.edu\">\n";
  print "<TITLE>Mind and Machine Module Transporter</TITLE>\n";
  print "<BASE HREF=\"www.npac.syr.edu/REU/reu95/home/jckrug/MM/index.html\">\n";
  print "</HEAD>\n <BODY BACKGROUND=\"icons/bckgrnd4.gif\"> \n<CENTER>\n";
  print "<H1>Mind and Machine Module</H1>\n <IMG HEIGHT=212 WIDTH=350 SRC=\"pics/mindlogo.gif\">\n";
  print "<ADDRESS>Simon Catterall, Assisstant Professor of Physics (smcnpac.syr.edu) </address>\n";
  print "<BR> <HR> <BR> </CENTER> \n";
  print "The <STRONG> Mind and Machine Module </STRONG> is designed to
compliment the <IMG SRC=\"icons/su_logo.gif\"> Syracuse University Physics
Department's PHY106: <I> Science for the 21st Century. </I>
students are asked to complete the Web related <A
HREF=\"http://www.npac.syr.edu/REU/reu95/home/jckrug/MM/ASS/index.html\">
homework assignments </A> when instructed during class lectures. The
<STRONG> Mind and Machine Module </STRONG> will explain how Neural
Networks pertain to both Biology and Computer Science. We have
included demonstrations to show various applications of Neural
Networks in Computer Science. <P>\n";

print "For those students who are new to the World Wide Web (WWW), have no
fear!  First, a major rule on the WWW is if something is <STRONG>
\"blue\" </STRONG> then <EM> &lt click-on-it &gt </EM>.  For a little
more official introduction, we have supplied a couple of small
tutorials.  You can try a <A
HREF=\"http://www.npac.syr.edu/REU/reu95/home/jckrug/MM/PHY106/intronet.html\">
simple introduction </A> to SU Campus Web resources or the <A
HREF=\"http://home.mcom.com/home/online-manual.html\"> On-line Netscape
Handbook </A>.  If this still hasn't satisfied you, we have a <A
HREF=\"http://www.npac.syr.edu/REU/reu95/home/jckrug/MM/PHY106/newbie.html\">
few more links </A> that we can suggest. <P>\n";

  print "We hope that you enjoy the <STRONG> Mind and Machine Module </STRONG>,
if you have any questions feel free to ask your TA. <P>\n";

########## Here is where the URL is actually inserted.
#
  print "<HR> <BR>\n";
  print "<CENTER> <A HREF= \n\"$sites[$site]\"> Take me to the Mind and Machine Module!</A> <P> </CENTER>\n";
  print "<HR> <ADDRESS>\n The Mind and Machine Module is brought to you by: <br> Marco Falcioni, Rob Salgado, and Jason Krug.\n </BODY> </HTML>\n";
  return 1;
}

############################################################################
# Read in the Input....
#
sub ReadEntry 
{ &ReadParse;                     # Read arguments passed from fill-out form
  $tmp = $in{'go'};
  return 1;
}

############################################################################
# Reads in all Input Passed from a fillout form into an associative array
# Where the <KEYS> are the indexes and their values are the respective 
# values.
# 
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 multi separator
    $in{$key} .= $val;
  }
  return 1; 
}
