#!/usr/athena/bin/perl

# given tab separated values of kerberos id, name, and code name,
# generate index.shtml and code.shtml so that each student
#  can see only their own name and code name.

open(I, ">index.shtml") || die "Couldn't open index.shtml for writing: $!";
open(C, ">code.shtml") || die "Couldn't open code.shtml for writing: $!";


print I "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<HTML>
<HEAD>
<TITLE>Random name test</TITLE>
</HEAD>
<!--#set var=\"uname\" value=\"ERROR\" -->
<!--#set var=\"ucode\" value=\"ERROR\" -->


<!--#if expr=\"\$SSL_CLIENT_EMAIL = /^\@/\" -->
 <!--#set var=\"uname\" value=\"ERROR 0\" -->
 <!--#set var=\"ucode\" value=\"ERROR 0\" -->
";

print C "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">
<HTML>
<HEAD>
<TITLE>Random name test</TITLE>
</HEAD>
<!--#set var=\"uname\" value=\"ERROR\" -->
<!--#set var=\"ucode\" value=\"ERROR\" -->


<!--#if expr=\"\$SSL_CLIENT_EMAIL = /^\@/\" -->
 <!--#set var=\"uname\" value=\"Error 0\" -->
 <!--#set var=\"ucode\" value=\"Error 0\" -->
";



while (<>) {
      chop;
      ($kerberos, $name, $codename) = split('\t', $_);
      print STDERR "Error for user $kerberos\n"
	  if (($codename eq "") || ($name eq ""));
      print I "
<!--#elif expr=\"\$SSL_CLIENT_EMAIL = /^$kerberos\@/\" -->
 <!--#set var=\"uname\" value=\"$name\" -->
 <!--#set var=\"ucode\" value=\"$codename\" -->
";
      print C "
<!--#elif expr=\"\$SSL_CLIENT_EMAIL = /^$kerberos\@/\" -->
 <!--#set var=\"uname\" value=\"$name\" -->
 <!--#set var=\"ucode\" value=\"$codename\" -->
";
}

print I "<!--#else -->
 <!--#set var=\"uname\" value=\"ERROR 2\" -->
 <!--#set var=\"ucode\" value=\"ERROR 2\" -->
<!--#endif -->

<BODY TEXT=\"#993300\" BGCOLOR=\"#fffff7\" LINK=\"#336600\" VLINK=\"#663300\" ALINK=\"#336600\">
<H1 align=center>Random name test</H1>
<!--#if expr=\"\$uname = /ERROR/\" -->
<p>You are not registered for this course, please see the instructors.
<!--#else -->
<p>If you are <b><!--#echo var=\"uname\" --></b>
<a href=\"code.shtml\">follow this link</a> for your code name.

<p>Otherwise contact your class administrator.
<!--#endif -->
</BODY> </HTML>
";

print C "<!--#else -->
 <!--#set var=\"uname\" value=\"ERROR 2\" -->
 <!--#set var=\"ucode\" value=\"ERROR 2\" -->
<!--#endif -->

<BODY TEXT=\"#993300\" BGCOLOR=\"#fffff7\" LINK=\"#336600\" VLINK=\"#663300\" ALINK=\"#336600\">
<H1 align=center>Random name test</H1>

<p>Your code name is <b><!--#echo var=\"ucode\" --></b>.
</BODY> </HTML>
";
