#
# Copyright (c) Xerox Corporation 1997, 1998. All rights reserved.
#
# License is granted to copy, to use, and to make and to use derivative
# works, provided that Xerox is acknowledged in all documentation
# pertaining to any such copy or derivative work. Xerox grants no other
# licenses expressed or implied. The Xerox trade name should not be used
# in any advertising without its written permission.
#
# XEROX CORPORATION MAKES NO REPRESENTATIONS CONCERNING EITHER THE
# MERCHANTABILITY OF THIS SOFTWARE OR THE SUITABILITY OF THIS SOFTWARE
# FOR ANY PARTICULAR PURPOSE.  The software is provided "as is" without
# express or implied warranty of any kind.
#
# These notices must be retained in any copies of any part of this software.
#
#-------------- error handling --------------------------------
#----- exported variables:
#-----    $errorfile = the name of the htmlmaker errorfile
#-----       used by main
#----- internal variables:
#-----    none
#

sub log_error {
    # write any error messages into the errorfile
    my($errname) = $_[0];
    my($errmesg) = $_[1];
    my($sc,$mn,$hr,$dy,$mo,$yr,$wd,$yd,$isd) = gmtime();
    $mo = $mo +1;
    # okay, let's get ready for year 2000...
    if ($yr < 100) { 
        $yr = "19$yr"; 
    } else {
    # this assumes gmtime really returns 108 for 2008 like it
    # should, but if different times implement different gmtime 
    # the we have trouble
        $yr = $yr -100;
        $yr = "20$yr";
    }
    if ($hr < 10) { $hr = "0$hr"; }
    if ($mn < 10) { $mn = "0$mn"; }
    if ($sc < 10) { $sc = "0$sc"; }
    my($errdate) = "$mo/$dy/$yr $hr:$mn:$sc GMT";
    open(ERRORFILE, ">>$errorfile") ||
        die "Html Maker unable to open error log $errorfile\n";
    if ($errname eq "start") {
	print ERRORFILE "$errdate START: htmlmaker started\n";
    } elsif ($errname eq "webcopy") {
	print ERRORFILE "$errdate ERROR: copy to web server failed\n";
	print ERRORFILE "web copy script is: $errmesg\n";
	print ERRORFILE "DATA NOT COPIED TO WEB SERVER, RESUMING...\n";
    } elsif ($errname eq "nortemon") {
	print ERRORFILE "$errdate ERROR: route monitor has died\n";
	print ERRORFILE "LAUNCHING A NEW ROUTE MONITOR...\n";
    } elsif ($errname eq "copy") {
	print ERRORFILE "$errdate ERROR: unable to copy file to archives\n";
	print ERRORFILE "archive filename was: $errmesg \n";
	print ERRORFILE "DATA NOT ARCHIVED, RESUMING...\n";
    } elsif ($errname eq "nofile") {
	print ERRORFILE "$errdate ERROR: no route stats file exists\n";
	print ERRORFILE "CREATING NEW ROUTE STATS FILE...\n";
    } elsif ($errname eq "noupdate") {
	print ERRORFILE "$errdate ERROR: update triggered but no update file\n";
	print ERRORFILE "update file expected is: $errmesg\n";
	print ERRORFILE "UPDATE TRIGGER IGNORED, RESUMING...\n";
    } elsif ($errname eq "updatebegin") {
	print ERRORFILE "$errdate UPDATE: route stats update started\n";
    } elsif ($errname eq "updateend") {
	print ERRORFILE "$errdate UPDATE: route stats update completed\n";
    } elsif ($errname eq "updateend") {
	print ERRORFILE "$errdate UPDATE: route stats update signal recvd\n";
    } elsif ($errname eq "updateopen") {
	print ERRORFILE "$errdate ERROR: unable to open stats update file\n";
	print ERRORFILE "update file: $errmesg\n";
	print ERRORFILE "UPDATE TRIGGER IGNORED, RESUMING...\n";
    } elsif ($errname eq "updatestart") {
	print ERRORFILE "$errdate ERROR: wrong first update stats line:\n";
	print ERRORFILE "line is: $errmesg";
	print ERRORFILE "UPDATE TRIGGER IGNORED, RESUMING...\n";
    } elsif ($errname eq "updateline") {
	print ERRORFILE "$errdate ERROR: invalid update stats line:\n";
	print ERRORFILE "$errmesg";
	print ERRORFILE "LINE IGNORED, RESUMING...\n";
    } elsif ($errname eq "startline") {
	print ERRORFILE "$errdate ERROR: wrong first route stats line:\n";
	print ERRORFILE "line is: $errmesg";
	print ERRORFILE "invalid route stats file renamed .bak";
	print ERRORFILE "CREATING NEW ROUTE STATS FILE...\n";
    } elsif ($errname eq "statsline") {
	print ERRORFILE "$errdate ERROR: invalid route stats line:\n";
	print ERRORFILE "$errmesg\n";
	print ERRORFILE "LINE IGNORED, RESUMING...\n";
    } elsif ($errname eq "field") {
	print ERRORFILE "$errdate ERROR: unable to look up field $errmesg\n";
	print ERRORFILE "ATTEMPTING TO IGNORE FIELD, RESUMING...\n";
    } elsif ($errname eq "newtable") {
	print ERRORFILE "$errdate ERROR: unable to open new route table\n";
	print ERRORFILE "route monitor table in $errmesg \n"; 
    } elsif ($errname eq "tablestart") {
	print ERRORFILE "$errdate ERROR: wrong initial route table lines:\n";
	print ERRORFILE "line is: $errmesg";
	print ERRORFILE "NEW ROUTE TABLE IGNORED, RESUMING...\n";
    } elsif ($errname eq "oldtable") {
	print ERRORFILE "$errdate ERROR: route table age is too old\n";
	print ERRORFILE "table is $errmesg seconds old \n";
	print ERRORFILE "NEW ROUTE TABLE IGNORED, RESUMING...\n";
    } elsif ($errname eq "interval") {
	print ERRORFILE "$errdate ERROR: route table interval is incorrect\n";
	print ERRORFILE "route table interval was $errmesg minutes \n";
	print ERRORFILE "NEW ROUTE TABLE IGNORED, RESUMING...\n";
    } elsif ($errname eq "sumhtml") {
	print ERRORFILE "$errdate ERROR: unable to generate html summary\n";
	print ERRORFILE "html summary file is: $errmesg \n";
	print ERRORFILE "HTML PAGE NOT UPDATED, RESUMING...\n";
    } elsif ($errname eq "weekhtml") {
	print ERRORFILE "$errdate ERROR: unable to generate week html file\n";
	print ERRORFILE "html weekly worst file is: $errmesg \n";
	print ERRORFILE "HTML PAGE NOT UPDATED, RESUMING...\n";
    } elsif ($errname eq "monthhtml") {
	print ERRORFILE "$errdate ERROR: unable to generate month html file\n";
	print ERRORFILE "html monthly worst file is: $errmesg \n";
	print ERRORFILE "HTML PAGE NOT UPDATED, RESUMING...\n";
    } elsif ($errname eq "weekopen") {
	print ERRORFILE "$errdate ERROR: unable to open  week html file\n";
	print ERRORFILE "html weekly worst file is: $errmesg \n";
	print ERRORFILE "unable to copy current week into last week file\n";
	print ERRORFILE "HTML PAGE NOT UPDATED, RESUMING...\n";
    } elsif ($errname eq "lweekhtml") {
	print ERRORFILE "$errdate ERROR: unable to generate last week file\n";
	print ERRORFILE "html last week  worst file is: $errmesg \n";
	print ERRORFILE "HTML PAGE NOT UPDATED, RESUMING...\n";	
    } elsif ($errname eq "aspidfile") {
	print ERRORFILE "$errdate ERROR: unable to open as lookup pid file\n";
	print ERRORFILE "as lookup pid file is: $errmesg \n";
	print ERRORFILE "UNABLE TO UPDATE AS NUMBERS, RESUMING...\n";
    } elsif ($errname eq "asout") {
	print ERRORFILE "$errdate ERROR: unable to open as lookup results\n";
	print ERRORFILE "as lookup output file is: $errmesg \n";
	print ERRORFILE "UNABLE TO UPDATE AS NUMBERS, RESUMING...\n";
    } elsif ($errname eq "asline") {
	print ERRORFILE "$errdate ERROR: invalid line in as lookup results\n";
	print ERRORFILE "line is: $errmesg";
	print ERRORFILE "AS RESULTS LINE IGNORED, RESUMING...\n";
    } elsif ($errname eq "assrc") {
	print ERRORFILE "$errdate ERROR: as lookup result for unknown src\n";
	print ERRORFILE "source is $errmesg \n";
	print ERRORFILE "AS RESULTS FOR SOURCE IGNORED, RESUMING...\n";
    } elsif ($errname eq "asin") {
	print ERRORFILE "$errdate ERROR: unable to write as lookup requests\n";
	print ERRORFILE "as lookup input file is: $errmesg \n";
	print ERRORFILE "UNABLE TO UPDATE AS NUMBERS, RESUMING...\n";
    } elsif ($errname eq "asstart") {
	print ERRORFILE "$errdate ERROR: unable to launch aslookup\n";
	print ERRORFILE "$errmesg\n";
	print ERRORFILE "AS LOOKUP WILL RETRY NEXT HOUR, RESUMING...\n";
    } else {
        print ERRORFILE "$errdate ERROR: unidentified error code $errname:\n";
        print ERRORFILE "ERROR CODE IGNORED, RESUMING...\n";
    }
    close(ERRORFILE);
}

sub log_quit {
    # write any error messages into the errorfile and exit
    my($errname) = $_[0];
    my($errmesg) = $_[1];
    my($sc,$mn,$hr,$dy,$mo,$yr,$wd,$yd,$isd) = gmtime();
    $mo = $mo + 1;
    # okay, let's get ready for year 2000...
    if ($yr < 100) { 
        $yr = "19$yr"; 
    } else {
    # this assumes gmtime really returns 108 for 2008 like it
    # should, but if different times implement different gmtime 
    # the we have trouble
        $yr = $yr -100;
        $yr = "20$yr";
    }
    if ($hr < 10) { $hr = "0$hr"; }
    if ($mn < 10) { $mn = "0$mn"; }
    if ($sc < 10) { $sc = "0$sc"; }
    my($errdate) = "$mo/$dy/$yr $hr:$mn:$sc GMT";
    open(ERRORFILE, ">>$errorfile") ||
        die "Html Maker unable to open error log $errorfile\n";
    if ($errname eq "pid") {
	print ERRORFILE "$errdate ERROR: unable to write pid file $errmesg\n";
    } elsif ($errname eq "start") {
	print ERRORFILE "$errdate ERROR: unable to start route monitor\n";
	print ERRORFILE "route monitor executable is $errmesg\n";
    } elsif ($errname eq "statsfile") {
	print ERRORFILE "$errdate ERROR: unable to open existing stats file\n";
	print ERRORFILE "statsfile is $errmesg\n";
    } elsif ($errname eq "statwrite") {
	print ERRORFILE "$errdate ERROR: unable to write new stats file\n";
	print ERRORFILE "statsfile is $errmesg\n";
    } else {
        print ERRORFILE "$errdate ERROR: unidentified error code $errname:\n";
    }
    print ERRORFILE "FATAL ERROR, EXITING.\n";
    close(ERRORFILE);
    exit(-1);
}

