#!/usr/local/bin/perl

# input:   a tab-delimited "phonelist"
# outputs:  an PRE(-formatted) table "phonepre.html"
#           an HTML table "phonetable.html"
#
### Rob Salgado (salgado@npac.syr.edu) 6 Jul 1995


open(OUT,">phonepre.html");
open(OUT2,">phonetable.html");
chmod 0664,  'phonepre.html',  'phonetable.html';

open(IN, "cat phonelist |") || die "phonelist missing\n";

print OUT "<HTML>\n";
print OUT "<HEAD>\n";
print OUT "<TITLE>NPAC Phone List (PRE version)</TITLE>\n";
print OUT "</HEAD>\n";
print OUT "<BODY>\n";
print OUT "<H1>NPAC Phone List</H1>\n<HR>";

print OUT2 "<HTML>\n";
print OUT2 "<HEAD>\n";
print OUT2 "<TITLE>NPAC Phone List (TABLE version)</TITLE>\n";
print OUT2 "</HEAD>\n";
print OUT2 "<BODY>\n";
print OUT2 "<H1>NPAC Phone List</H1>\n<HR>";


#### Not elegant...but it works.
#
# make a first pass to determine the column widths
#

FILE0:
while (<IN>) {
	chop $_;
	($name_last, $name_first, $phone, $login, $office) = split(/\t/,$_); 
  $l_name_last  = length($name_last)  if length($name_last)  > $l_name_last;
  $l_name_first = length($name_first) if length($name_first) > $l_name_first;
  $l_phone      = length($phone)      if length($phone)      > $l_phone;
  $l_login      = length($login)      if length($login)      > $l_login;
  $l_office     = length($office)     if length($office)     > $l_office;
	next FILE0;
}

# start over at the beginning and make the preformatted table
#
close(IN);

open(IN, "cat phonelist |") || die "phonelist missing\n";
    print OUT "<PRE>\n";
    print OUT "Last Name", " " x ($l_name_last  - length("Last Name")),   " ";
    print OUT "First Name", " " x ($l_name_first - length("First Name")), "  ";
    print OUT "Ext",        " " x ($l_phone    - length("Ext")),  "  ";
    print OUT "Email Address"," " x ($l_login - length("Email Address")), "  ";
    print OUT "Office",       " " x ($l_office   - length("Office")),     "\n";
    print OUT "<HR>\n";


print OUT2 "If your browser does not support tables, please use the\n"; 
print OUT2 "<a href=\"phonepre.html\">";
print OUT2 "preformatted version of this phone list.</a><hr>\n";
print OUT2 "<TABLE BORDER>\n";
print OUT2 "<TR><TD COLSPAN=5 ALIGN=CENTER>
        <H1>NPAC Phone List</H1> </TD></TR>\n";
print OUT2 "<TR> <TH>Last Name</TH> <TH>First Name</TH> <TH>Extension</TH>\n";
print OUT2 "<TH>Email Address</TH> <TH>Office</TH> </TR>\n";



FILE:
while (<IN>) {
	chop $_;
	($name_last, $name_first, $phone, $login, $office) = split(/\t/,$_); 

    print OUT "$name_last", " " x ($l_name_last  - length($name_last)), " ";
    print OUT "$name_first"," " x ($l_name_first - length($name_first)),"  ";
    print OUT "$phone",     " " x ($l_phone      - length($phone)),     "  ";
    print OUT "$login",     " " x ($l_login      - length($login)),     "  ";
    print OUT "$office",    " " x ($l_office     - length($office)),    "\n";

        print OUT2 "<TR>\n";
        print OUT2 "<TD>&#160; <B>$name_last</B>  </TD>\n" ;
        print OUT2 "<TD>&#160; $name_first        </TD>\n" ;
        print OUT2 "<TD ALIGN=RIGHT> $phone </TD>\n" ;
        print OUT2 "<TD>&#160; <tt>$login</tt>    </TD>\n" ;
        print OUT2 "<TD>&#160;  $office            </TD>\n" ;
        print OUT2 "</TR>\n";

	next FILE;
}
close(IN);

print OUT "</PRE>\n";
print OUT "<HR>\n";
print OUT "<DT> All phone extensions are <b><tt>(315)-443-xxxx</tt></b>.\n";
print OUT "<DT> All email addresses are <b><tt>@npac.syr.edu</tt></b> unless otherwise noted.<HR>\n";


print OUT2 "<TR><TD COLSPAN=5 ALIGN=CENTER>
   All phone extensions are <b><tt>(315)-443-xxxx</tt></b>. </TD></TR>\n";
print OUT2 "<TR><TD COLSPAN=5 ALIGN=CENTER>
   All email addresses are <b><tt>@npac.syr.edu</tt></b>";
print OUT2 " unless otherwise noted.</TD></TR>\n";
print OUT2 "</TABLE>\n<HR>\n";




#
# generate timestamps
#

open(UPDATE, "ls -l phonelist |") || die "phonelist missing\n";
while(<UPDATE>){
    chop $_;
    ($mod, $num, $owner, $size, $month, $date, $time, $name)=split(/\s+/,$_);
    print OUT "<I>Data last updated: $date $month <BR>\n";
    print OUT2 "<I>Data last updated: $date $month <BR>\n";
}
close(UPDATE);

    require "ctime.pl";
    ($day, $month, $date, $time, $year)=split(/\s+/,&ctime(time));
    print OUT "Table generated: $date $month $year </I>\n";
    print OUT2 "Table generated: $date $month $year </I>\n";

print OUT "</BODY>\n";
print OUT "</HTML>\n";
print OUT2 "</BODY>\n";
print OUT2 "</HTML>\n";
close OUT;
close OUT2;

exec("ls -l phonepre.html phonetable.html");



