#!/usr/bin/perl
# author: C. Edward Chow
# date:   11/9/2000
# copyright © 2000
# free distribution for research and education
# but please retain the above author, date and copyright lines.
#
use CGI qw(:standard);

print header('text/html');
print "<html><head><title>CS691c Lab Reservation Result</title></head><body>";
print "<h1>CS691c Lab Reservation Result</h1>";
print hr;
@names = param;
foreach $name (@names) {
	$value = param($name);
        if ($name ne "passwd") { print "$name=$value<br>";}
        if ($value eq "Reserve") { 
           $reserveSlot=$name; 
           $command = $value;
        }
        $_ = $value;
        if (/Cancel reservation by (\w+)/) {
            $command = "Cancel";
            $reservedBy = $1;
            $reserveSlot=$name; 
            print "cancel the reservation by $1\n<BR>";
        }
}
print "reserveSlot=$reserveSlot\n<BR>";
$login = param('login');
$passwd =param('passwd');
print "verify login=$login\n<BR>";
if ($login eq "") {
      print "You need to enter login for reservation.\n<hr>\n";
      open(WEBPAGE, "reserve/cs691creserve.htm");
      while(<WEBPAGE>) {
         print $_;
      }
      close(WEBPAGE); 
      exit(1);
} else {
   open(FILEREAD, "password.txt");
   $search = 1;
   while(($passwdline=<FILEREAD>) && $search) { # with () $passwdline will be 1
      @fields = split(/:/, $passwdline); # split $line using : as delimiter
      if ($login eq $fields[1]) {
         print "login entry exist!\n";
         if ($passwd eq $fields[2]) {
            $fullname = $fields[0];
            $fullname =~ s/,.*//; # throw away everything after comma,
            print "passwd correct! fullname=$fullname\n<BR>";
         } else {
            print "password not correct! encpasswd=$fields[2]; passwd=$passwd\n<hr>\n";
      open(WEBPAGE, "reserve/cs691creserve.htm");
      while(<WEBPAGE>) {
         print $_;
      }
      close(WEBPAGE); 
            exit(1);
         }
         $search = 0;
      }
   }
   if ($search) {
      print "login/passwd incorrect!\n<hr>\n";
      open(WEBPAGE, "reserve/cs691creserve.htm");
      while(<WEBPAGE>) {
         print $_;
      }
      close(WEBPAGE); 
      exit(1);
   }
   close(FILEREAD);
}
dbmopen(%reserve, "reserve/cs691chw1", 0666);
$slot = $reserve{$reserveSlot};
#print "slot=$slot<br>/n";
if ($command eq "Reserve") {
   if ($slot eq "") {
      print "$reserveSlot not reserved; reserving...";
      $reserve{$reserveSlot} = $login;
      print "<BR>$fullname: the slot $reserveSlot is reserved for you!\n<BR>";
      # move up the above print line for better web page look.
      # logically it should be after the web page is updated.
      # update the web page
      open(WEBPAGE, "reserve/cs691creserve.htm");
      open(UPDATE, ">reserve/updatecs691creserve.htm");
      while(<WEBPAGE>) {
         if (/$reserveSlot/) {
            s/Reserve/Cancel reservation by $login/;
            
         } 
         print UPDATE $_;
         print $_;
      }
      close(WEBPAGE); close(UPDATE);
      system("mv reserve/updatecs691creserve.htm reserve/cs691creserve.htm");
   } else {
      print "$reserveSlot reserved by $slot! Try different time slot\n<BR>";
   }
} else { # Cancel
   if ($login eq $slot) {
      print "prepare to cancel...";
      $reserve{$reserveSlot} = ""; # reset
      print "<BR>$fullname: the reservation on slot $reserveSlot is canceled!\n<BR>";
      # move up the above print line for better web page look.
      # logically it should be after the web page is updated.
      # update web page
      open(WEBPAGE, "reserve/cs691creserve.htm");
      open(UPDATE, ">reserve/updatecs691creserve.htm");
      while(<WEBPAGE>) {
         if (/$reserveSlot/) {
            s/Cancel reservation by $login/Reserve/;
         } 
         print UPDATE $_;
         print $_;
      }
      close(WEBPAGE); close(UPDATE);
      system("mv reserve/updatecs691creserve.htm reserve/cs691creserve.htm");
   } else { # login and reserveby not matched
      print "The slot is reserved by others.  Please select the timeslot
reserved by you.<BR>";
   }
}
print "</body></html>";
