#! /usr/bin/perl

#  Copyright 2001-2020 Leslie Richardson

#  This file is part of Open Admin for Schools.

#  Open Admin for Schools is free software; you can redistribute it 
#  and/or modify it under the terms of the GNU General Public License
#  as published by the Free Software Foundation; either version 2 of 
#  the License, or (at your option) any later version.


my %lex = ('Error' => 'Error',
	   'Main' => 'Main',
	   'Bus Route' => 'Bus Route',
	   'Students' => 'Students',
	   'Route' => 'Route',
	   'Name' => 'Name',
	   'Grade' => 'Grade',
	   'HRm' => 'HRm',
	   'Address' => 'Address',
	   'Ph' => 'Ph',
	   'Parent 1' => 'Parent 1',
	   'Parent 2' => 'Parent 2',
	   'View/Download' => 'View/Download',
	   'View Log File' => 'View Log File',
	   'Rm' => 'Rm',
	   'Report' => 'Report',
	   'Not Found' => 'Not Found',

	   );

use DBI;
use CGI;

# Basic Constants
my $logfile = "pdflog$$.txt";
my $shortname = "busrpt$$";
my $filename = "$shortname.tex";
my $maxlines = 19;

# Read config variables
eval require "../etc/admin.conf";
if ( $@ ) {
    print $lex{Error}. ": $@<br>\n";
    die $lex{Error}. ": $@\n";
}

eval require "../lib/liblatex.pl";
if ( $@ ) {
    print $lex{Error}. " $@<br>\n";
    die $lex{Error}. " $@\n";
}


my $q = new CGI;
my %arr = $q->Vars;
print $q->header( -charset, $charset );

if ($arr{maxstudents}) {
    $maxlines = $arr{maxstudents};
}


my $dsn = "DBI:$dbtype:dbname=$dbase";
my $dbh = DBI->connect($dsn,$user,$password);
$dbh->{mysql_enable_utf8} = 1;

my ($sec, $min, $hour, $mday, $mon, $year, $wday, 
 $yday, $iddst) = localtime(time);
$year = $year + 1900;
$mon++;
$wday++;
my $currdate = "$dow[$wday], $month[$mon] $mday, $year";

my $title = "$lex{'Bus Route'} $lex{Report}";
print qq{$doctype\n<html><head><title>$title</title>\n};
print qq{<link rel="stylesheet" href="$css" type="text/css">\n};
print qq{$chartype\n</head><body>\n};

print qq{[ <a href="$homepage">$lex{Main}</a> ]\n};
print qq{<h1>$title</h1>\n};


my $sortorder = 'busroute, lastname, firstname';
my $route;
if( $arr{route} ){ 
    $route = "where busroute = '$arr{route}'"; 
} else {
    $route = "where busroute is not NULL and  busroute != ''";
}


$sth = $dbh->prepare("select * from student $route 
  order by busroute, lastname, firstname");
$sth->execute;
if ($DBI::errstr){ print $DBI::errstr; die; }

my $curroute = -1;
my $oldroute;
my $studcount;

# print qq{<div style="font-size:130%;font-weight:bold;">Route</div>\n};

# Read in a student record.
my $first = 1;
while ( $ref = $sth->fetchrow_hashref ) {  
    my %r = %$ref;

    foreach my $val ( values %r ) {
	( $val ) = latex_filter( $val );
    }

    if ( $first ) {  # setup TEX file
	open(TEX,'>', $filename) || die "Can't open tex file";

	print TEX "\\documentclass[10pt,letterpaper]{article}
\\pagestyle{empty}
\\setlength{\\textwidth}{8in}
\\setlength{\\textheight}{10.6in}
\\setlength{\\hoffset}{-1.65in}
\\setlength{\\voffset}{-1.4in}
\\setlength{\\parindent}{0pt}
\\setlength{\\tabcolsep}{3.5pt}\n";

	print TEX "\\begin{document}\n";

    }


    $oldroute = $curroute;
    $curroute = $r{busroute};

#    print "<div>$r{busroute}</div>\n";

    #remove any accidental spaces from the front or end of the route
    $curroute =~ s/^\s+//;
    $curroute =~ s/\s+$//; 

   
    if ($linecount > $maxlines || $first || $oldroute ne $curroute) {
	# We'll print a new page header here
	if ( not $first ) {
	    if($oldroute ne $curroute){
		print TEX "Students: $studcount & & & &  \\\\ \\hline";
		$studcount = 0;
	    }
	    print TEX "\\end{tabular}\\\\";
	}
	$linecount = 0;
       
	print TEX "\\newpage\n\\center{{\\bf $schoolname $lex{'Bus Report'}} - $currdate \\\\ \n";
	print TEX "{\\bf $lex{Route}: $curroute } \\\\ \n";
	print TEX "\\medskip\n\\begin{tabular}{|p{4.5cm}|p{4.0cm}|p{3.5cm}|p{3.5cm}|p{3.5cm}|}\n";
	print TEX "\\hline\n {\\bf $lex{Name}} & {\\bf $lex{Grade}/$lex{HRm} }";
	print TEX "& {\\bf $lex{Address}/$lex{Ph} } & {\\bf $lex{'Parent 1'} }";
	print TEX "& {\\bf $lex{'Parent 2'}} \\\\ \n\\hline";
	$first = 0;

    } # End of New Page Header


    # Normal record printing.
    print TEX "{\\bf $r{lastname}, $r{firstname} $r{middlename} } & Grade: $r{grade} & ";
    print TEX qq{$r{address1} & $r{par1_firstname} $par1{lastname} &};
    print TEX "$r{par2_firstname} $r{par2_lastname} \\\\ \n";
    print TEX "($r{studnum}) $lex{Rm}: $r{homeroom} & $lex{Ph}: $r{hphone1} & &  \\\\ \\hline \n"; 

    $linecount++;
    $studcount++;
	
}  # End of student loop

if ( $first ) {
    print qq{<h3>$lex{Route} $lex{'Not Found'}</h3>\n};
    print qq{</body></html>\n};
    exit;
}

print TEX "$lex{Students}: $studcount & & & &  \\\\ \\hline\n\\end{tabular}\\\\ \n \\end{document}";
close TEX;

system("$pdflatex $filename > $logfile");
system("mv $shortname.pdf $downloaddir");
system("mv $logfile $downloaddir");
system("rm -f $shortname.*");

print qq{<p></p>\n};
print qq{<div style="font-size:130%;font-weight:bold;margin:1em 0.4em 1em 0.4em;">\n};
print qq{[ <a href="$webdownloaddir/$shortname.pdf">$lex{'View/Download'} $title</a> ]</div>\n};

print qq{<div>[ <a href="$webdownloaddir/$logfile">$lex{'View Log File'}</a> | <a href="$homepage">$lex{Main}</a> ]</div>\n};

print qq{</body></html>\n};

