#!/bin/sh -f
#### I use sh instead of csh because
#### hesinfo seems only returns $? signal when in sh, not in csh...

#### copy right statement                                                ####
####                                                                     ####
#### Sept 2, 2006 copyright by Takehiko Nagakura. All rights reserved.   ####
#### Written by takehiko nagakura, Massachusetts Institue of Technology. ####
#### Do not use, copy, or distribute without permission by Takehiko      ####
#### Nagakura. Nagakura will not be responsible for any consequence      ####
#### of running this program.                                            ####
####                                                                     ####
#### For information, send e-mail to takehiko@mit.edu                    ####
####                                                                     ####

#### This CGI gets the frame contents from user submsion at
#### http://cat.mit.edu/4.560/register/form1.html
#### I tried to be as security concious as possible...
#### User response is returned in the environmental variable, $QUERY_STRING

outputdir=/arch3/people/arc/4.560/register/current

echo "Content-type: text/html"
echo ""

#echo "$QUERY_STRING"
#echo "<br>" 

#### Take out wild card just in case for security...
QUERY_STRING=`echo "$QUERY_STRING" | sed 's/\*/ /g'`
QUERY_STRING=`echo "$QUERY_STRING" | sed 's/\?/ /g'`

#### Not a very fancy program but just a quick trial...
account=`echo "$QUERY_STRING"  | sed 's/^account=\(.*\)&fullname.*$/\1/g'`
fullname=`echo "$QUERY_STRING"  | sed 's/^.*fullname=\(.*\)&program.*$/\1/g'` 
program=`echo "$QUERY_STRING"  | sed 's/^.*program=\(.*\)&building.*$/\1/g'`
building=`echo "$QUERY_STRING"  | sed 's/^.*building=\(.*\)&architect.*$/\1/g'` 
architect=`echo "$QUERY_STRING"  | sed 's/^.*architect=\(.*\)&location.*$/\1/g'`
location=`echo "$QUERY_STRING"  | sed 's/^.*location=\(.*\)&year.*$/\1/g'` 
year=`echo "$QUERY_STRING"  | sed 's/^.*year=\(.*\)$/\1/g'`
####
fullname=`echo "$fullname"  | sed 's/+/ /g'`
program=`echo "$program"   | sed 's/+/ /g'`
building=`echo "$building"  | sed 's/+/ /g'`
architect=`echo "$architect" | sed 's/+/ /g'`
location=`echo "$location"  | sed 's/+/ /g'`
year=`echo "$year"      | sed 's/+/ /g'`

#### check the accountname quickly
#### take out the space first
account=`echo "$account"   | sed 's/+//g'`

#### This does not work without full path...
####hesinfo $account passwd  > /dev/null

/bin/athena/hesinfo $account passwd  > /dev/null
result="$?"

#### Alternative method...
#### result2=`/bin/athena/hesinfo $account passwd | sed 'g/:.*/s///g'`
#### echo "<br>" $result2 rrr 

if [ "$result" = "0" ]
  then
    echo account exist > /dev/null
  else  ## if the hesinfo fails, $result should be 1 or 2
    echo "<h3>$account: Account name does not exist.</h3>"
	echo "Please try again. <br>"
	echo "It should be the same as your MIT email address<br>"
	echo "but do NOT include @mit.edu."
    exit 0
fi

##### writing to a file
fname="$outputdir/$account"
#echo $fname 

echo "<br>account name: $account" > $fname # bourn sh overwrite exisiting one if any 
echo "<br>full name: $fullname" >> $fname
echo "<br>degree program: $program" >> $fname
echo "<br>building name: $building" >> $fname
echo "<br>architect: $architect" >> $fname 
echo "<br>location: $location" >> $fname
echo "<br>year: $year" >> $fname
echo "<br>---------------------------------" >> $fname

##### displaying the result
echo "<h3> 4.560 Project Registration </h3>"
echo "Your entry has been successful and should be now included below.<br>"
echo "(The list is alphabetically ordered via your account names.)<p>"
cd $outputdir
people=`ls -1`
for var in $people
do
 cat $var
done



#echo $#argv
# echo $argv[1] | sed "s/\\//g" | sed 's/\^/\"/g' | sed 's/|/ /g' 
#while ($#argv)
#  echo $argv[1] | sed "s/\\//g" | sed 's/\^/\"/g' | sed 's/|/ /g' 
#  shift argv
#end
