#!/s/perl/bin/perl
# $Id:$

###
### Create a LaTeX report from the entries in the phd_info database
###

require 5.000;			# This uses perl 5.000 constructions

$dbName = "phd_info";		# Declare the database

				# Any new or modified fields should go
				# here.  The name $r is magic.
sub field_mod {
    $r->{'name'} = $r->{'lastname'} . ", " . $r->{'firstname'};
}
				# Subroutine to output each record.
				# $r is magic here too.  Remember to
				# escape the '&' characters and '\'
				# characters with '\'.  That is, every
				# '\' to be printed has to be entered
				# as "\\".
sub output_record {
    print "$r->{'name'}\&$r->{'grad-yr'}\&$r->{'advisor'}",
    "\&$r->{'thesis'}\\\\\n";
}
				# Header is defined here.  This text
				# will be printed at the beginning of
				# the output.  Be careful of '\' at the
				# end of a line - the '\' is swallowed.
$Header = <<'_END_OF_HEADER_';
\documentclass{article}
\usepackage{times}
\usepackage{longtable}
\usepackage{vmargin}
\setpapersize{USletter}
\setmargins{0.5in}{0.5in}{7.5in}{10in}{0in}{0in}{0in}{0in}
\pagestyle{empty}
\setlength{\tabcolsep}{3pt}
\begin{document}
\setlongtables
\begin{longtable}[c]{@{}l c l p{3.7in}@{}}
  \caption*{\large\bf Ph.D.~Graduates of the Department of Statistics}
  \\ \hline
  \multicolumn{1}{c}{\textbf{Graduate}}&\multicolumn{1}{c}{\textbf{Year}}&
  \multicolumn{1}{c}{\textbf{Advisor}}&\multicolumn{1}{c}{\textbf{Thesis
      title}}
  \\ \hline
  \endhead
  \endfoot
_END_OF_HEADER_
				# Trailer is defined here.  This text
				# will be printed at the end of the output.
$Trailer = <<'_END_OF_TRAILER_';
\end{longtable}
\end{document}
_END_OF_TRAILER_

$DIR = '/p/stat/Data/admin/';
$FSEP = "\t";

###
### Don't change things after here unless you are prepared to debug it
### yourself.                           DMB. 95/06/30
###

die '$dbName must be defined to print a report!\n'
    unless $dbName;

$DB = $DIR . $dbName;		# Name of database
$FORM = $DB . ".form";		# Name of emacs-lisp form definition
				# for database
open(FORM, "< $FORM") ||
    die ("Unable to open form file $FORM: $!\n");
undef $/; $form = <FORM>; $/ = "\n"; # slurp entire form file into a
				     # string
die "$FORM does not define the fields!\n"
    unless ($form =~ /\(forms-enumerate[^']*'\(([^\)]*)/);
@FIELDS = grep(!/^$/, split(/\s+/, $1));

open(INPUT, "< $dbName") ||
    die("Unable to open $dbName for input: $!\n");

$flen = $#FIELDS;

print $Header;
while (<INPUT>) {
    $rcount++;			# keep track of the record count
    @thisRec = split(/$FSEP/, $_);
    die "Record $rcount has $#thisRec fields.  It should have $flen.\n"
	unless $flen == $#thisRec;
    $r = {};
    for (@FIELDS) {
	$r->{$_} = shift(@thisRec);
    }
    &field_mod;
    &output_record;
}
print $Trailer;
