#!/usr/bin/perl

#------------------------------------------------------------------------
# Author: C. Edward Chow
# Date:   16 Oct. 1998
# Draft:  1.0
# Simple Perl script to grade midterm 
#------------------------------------------------------------------------

use CGI qw(:standard);

$examine=param('examine'); # consist of class, semester, midterm/final
$studentLogin=param('studentLogin');
$studentPasswd=param('studentPasswd');
print header();
# extract class from $examine
$class=substr($examine, 0, 5);
$semester=substr($examine, 5, 1);
$year=substr($examine, 6, 4);
$term=substr($examine, 10, length($examine)-10);

print "<html>examine=$examine, studentLogin=$studentLogin<br>";

open(IN, "stulist.txt");
$found = 0;
while (<IN>) {
    if (/$studentLogin/) {
        @fields = split(/:/);
        $passwd = $fields[1];
        $fullname = $fields[5];
        $found = 1;
        last;
    }
}
if ($found == 0) {
    print "student not exist in password file!";
    die;
}
$try = 1;
print "studentPasswd=$studentPasswd passwd= $passwd\n";
if ($studentPasswd == $passwd) {
     print "Student password correct<BR>\n";
} else {
        print "student password not correct!";
      die;
}
close(IN);

$found = 0;
while(<$class\/$examine\/*.htm>){
   if (/$studentLogin/) {
      chomp($filename=$_);
      print "studentLogin=$studentLogin, filename=$filename\n";
      $found = 1;
      last;
   }
}
if ($found == 0) {
   print start_html();
   print "$login $examine file not found";
   print end_html();
   die;
} else {
  print "student answer filename=$filename\n";
}
open(EXAM, "$filename");
while (<EXAM>) {
  print;
}
close(EXAM);

print "<hr><center><h2>$examine Grade Distribution<h2><pre>";
open(DIST, "$class\/$examine\/distribution.txt");
while (<DIST>) {
   print;
}
close(DIST);
print "</pre></center>\n";
