#!/usr/bin/perl
#------------------------------------------------------------------------
# Author: C. Edward Chow
# Date:   22 December 1995
# Draft:  1.0
# Simple Perl script to answer grade query
#------------------------------------------------------------------------

print "Content-type: text/html\n\n";

if ($ENV{'REQUEST_METHOD'} eq "get") { $buffer = $ENV{'QUERY_STRING'}; }
else { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }

print "<HTML><HEAD>\n";
print "<TITLE>CS522 F2002 Grade Query Results</TITLE>\n";
print "</HEAD><BODY BGColor=\"#93eedb\" TEXT=\"#2222222\">\n";
print "<H1>Query Results</H1>\n";
print "<PRE>\n";

@nvpairs = split(/&/, $buffer);
foreach $pair (@nvpairs)
{
    ($name, $value) = split(/=/, $pair);

    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;


	 $course = "CS522F2002";
    if ($name ne "passwd") {
		print "$name = $value\n";
	 }
	 if ($name eq "login") {
		$userlogin = $value;
	 } elsif ($name eq "passwd") {
		$userpasswd = $value;
	 } elsif ($name eq "comments") {
		$comments = $value;
	 } else {
		# do nothing;
	 }
}
if (! $userlogin) {
		print "You need to enter login for access this private information.\n";
} elsif (! $course) {
	print "You forgot to select the course name.\n";
} else {
	open(FILEREAD, "list.txt");
	$search = 1;
	while(($passwdline=<FILEREAD>) && $search) { # with () $passwdline will be 1
		@fields = split(/:/, $passwdline); # split $line using : as delimiter
		#print "line with login=$login, $passwd, $fields[0], $fields[1]\n";
		if ($userlogin eq $fields[0]) {
			if (crypt($userpasswd, $fields[1]) eq $fields[1]) {
				print "login correct!\n";
				$fullname = $fields[4];
	   		$fullname =~ s/,.*//; # throw away everything after comma,
	   		print "\n\n$fullname, \n";
				print "I hope you learn a lot. Thank for your participation in class.\n";
				print "I welcome any feedback or comment you have.\n";
				print "<BR> <BR>Edward\n";
				print "<BR> <BR>Here is your current grade status. <BR>Note that there may be a posting delay between this on-line grade data<BR> and the actual data record on the class grade spreadseet.\n";
				$filename = "$course\Grade.txt";
				open(GRADEFILE, $filename);
				$notfound = 1;
				#<GRADEFILE>; # skip title line
				$gradeline = <GRADEFILE>;
				@g = split(/\t/, $gradeline);
				$l = @g;
				print "<TABLE BORDER=2 CELLSPACING=3 CELLPADDING=3>\n";
				print "\t<TR BGCOLOR=\"#EDDF99\"><TH COLSPAN=\"$l\"> $course grade</TH></TR>";
				print "\t<TR BGCOLOR=\"#EDDF99\">";
				foreach $title (@g) {
					print "<TH>$title</TH>";
				}
				print "</TR>\n";
				while (($gradeline = <GRADEFILE>) && $notfound) {
					if ($gradeline =~ /$userlogin/) {
						@g = split(/\t/, $gradeline);
						print "\t<TR BGCOLOR=\"#EDDFC9\">";
						foreach $title (@g) {
							print "<TD Align=\"center\">$title</TH>";
						}
						print "</TR>\n";
						$notfound = 0
					}
				}
				print "</TABLE>\n";
				if ($notfound) {
					print "grade not found in $course file\n";
				}
			} else {
				print "password not correct!\n";
			}
			$search = 0;
		}
	}
	if ($search) {
		print "login incorrect!\n";
	}
	close(FILEREAD);
}
if ($comments) {
	open(MAIL, "| mail chow\@cs.uccs.edu");
	print MAIL "Subject: grade query: comment from $fullname\n";
	print MAIL "user login=$userlogin\@sanluis\ncomments:\n\n$comments\n";
	close(MAIL);
}
print "If you have problems accessing your grade, send me email.\n";
print "</PRE>\n";
print "</BODY></HTML>\n"

