#!/usr/bin/perl


#
#  Global data 
#
#  Base directory for Watchdog
$BASE_DIR ="/home/ipaudit";
$CGI_SCRIPT=$ENV{SCRIPT_NAME};


#
#  Read report configuration
#

#  Directory containing output from Report

#
#  Choose whether doing directory listing or showing page
#
if (0==scalar(@ARGV)) {
	&ShowDir;
	exit;
}

#  Get report name
$REPORT_NAME =  $ARGV[0];
$REPORT_DIR  = "$BASE_DIR/reports/$REPORT_NAME";
$HTML_DIR    = "$REPORT_DIR/html";


#  Read report configuration
if ( -r "$REPORT_DIR/report.cfg" ) {
	open (INFILE, "$REPORT_DIR/report.cfg");
	while (<INFILE>) {
		chomp;
		next if /^\s*#/;
		($key,$val) = /^\s*(\S+)\s+(.*)$/;
		$REP{$key} = $val;
	}
	close (INFILE);
}

#  Set default values
$REP{NCOL} = 7  if  ! defined($REP{NCOL});


if (1==scalar(@ARGV)) {
	&ShowListing;
} else {
	$FILE=$ARGV[1];
	&ShowPage;
	}

exit;

##########################################
#
#  Present listing of files Sorted by Date
#
sub ShowListing
{


@SortFiles = &GetFileList;

#  Print list page

print <<EOM;
Content-type: text/html

<html><head><title>$REP{TITLE}</title></head>
<body bgcolor=white>
<center>
<font size=+1>$REP{TITLE}</font>
</center>
<hr noshade>
<font size=-1>
EOM


print <<EOM;
<br><br>
EOM

#  No files found
if ($#SortFiles<0) {
	print "<br><br><font size=+1>No files found.</font><br><br>\n";
#  Files with times
} elsif ($SortFiles[0]=~/-\d{2}:\d{2}/) {
	&ListTimes(@SortFiles);
#  Max 1 file per day
} else {
	&ListDates(@SortFiles);
}



print "</font></body></html>\n";
}



###################################################################
#   Show Individual Page
###################################################################
sub ShowPage
{
#  Find day and time of requested file, or just day or month
if ($FILE=~/(\d{4})-(\d{2})-(\d{2})-(\d{2}:\d{2})/) {
	$DateStr = `date +"%a, %b %d %Y, %I:%M %p" -d "$2/$3/$1 $4"`;
} elsif ($FILE=~/(\d{4})-(\d{2})-(\d{2})/) {
	$DateStr = `date +"%a, %b %d %Y" -d "$2/$3/$1"`;
} elsif ($FILE=~/(\d{4})-(\d{2})/) {
	$DateStr = `date +"%b %Y" -d "$2/1/$1"`;
}


#  List of all files
@SortFiles = &GetFileList;

#  Find file in list
$i=0;
for (@SortFiles) {
	last if ($_ eq $FILE);
	$i++;
}

if ($i>$#SortFiles) {
print <<EOM;
Content-type: text/html

<html><head><title>$REP{TITLE}</title></head>
<body bgcolor=white>
<p><b>
ERROR:  Requested file not found.
</b>
EOM
exit;
}

print "Content-type: text/html\n\n";
print "<html><head><title>$REP{TITLE}: $DateStr</title></head>\n";
print "</head>\n";
print "<body bgcolor=white>\n";

$Curr = $HTML_DIR . "/" . $SortFiles[$i];
$Next = "$CGI_SCRIPT?$REPORT_NAME+$SortFiles[$i-1]"  if ($i>0);
$Prev = "$CGI_SCRIPT?$REPORT_NAME+$SortFiles[$i+1]"  if ($i<$#SortFiles);

print "<center><h3>$REP{TITLE}</h3></center><hr noshade><p>\n";
print "<tt>$DateStr</tt><br>";

print "<br><tt>";
print "&lt;&nbsp;<a href=$Prev>Prev Report</a>\n" if $Prev;
print " | \n";
print "<a href=\"$CGI_SCRIPT?$REPORT_NAME\">Up</a>\n";
print " | \n";
print "<a href=$Next>Next Report</a>&nbsp;&gt;\n" if $Next;
print "</tt><br>\n";

if (open (INFILE,$Curr)) {
	while (<INFILE>) { 
		print $_;
	}
	close (INFILE);
}

print "</body></html>\n";
}


sub GetFileList
{
#
#  Get list of files
#

opendir DATADIR, $HTML_DIR;
@Files = sort readdir DATADIR;
closedir DATADIR;

# Keep files that fit filename pattern only
$next=0;
for ($i=0; $i<=$#Files; $i++)
	{

	if ($Files[$i]=~m/^\d\d\d\d-\d\d.*\.html$/)
		{
		$Files[$next] = $Files[$i] if ($next!=$i);
		$next++;
		}
	}
$#Files=$next-1;

# Sort file
@SortFiles = sort {$b cmp $a} @Files;
}


#  Print links to reports according to their dates
sub ListDates {
my (@SortFiles) = @_;
my ($i,$Date);

#  Print list page

for ($i=0; $i<=$#SortFiles; $i++) {
	($Date) = ($SortFiles[$i]=~/(\d{4}-\d{2}-\d{2})/);
	($Date) = ($SortFiles[$i]=~/(\d{4}-\d{2})/)  if  $Date eq "";
	print "<a href=$CGI_SCRIPT?$REPORT_NAME+$SortFiles[$i]>$Date</a>\n";
	print "&nbsp; &nbsp; &nbsp;\n";
	print "<br>\n" if ( ($i+1) % $REP{NCOL} == 0);
	}
}



sub ListTimes {
my (@SortFiles) = @_;
my ($time,$date,$prevdate,$code,@timelist);

@timelist=();
$prevdate="";
for (@SortFiles) {
	($date,$time) = /^(\d{4}-\d\d-\d\d)-(\d\d:\d\d)/;
	if ($date ne $prevdate) {
		if (@timelist) {
			print "<b>$prevdate</b>: &nbsp;\n";
			print join("\n", @timelist);
			print "\n<br>\n";
			@timelist=();
		}
		$prevdate = $date;
		@timelist = ();
	}
	$time=~/^(\d\d):(\d\d)$/;
	if ($2 eq "30") {
		$code = ".";
	} else {
		$code = $1;
	}
	unshift @timelist, ("<a href=$CGI_SCRIPT?$REPORT_NAME+$_>$code</a>\n");
}
if (@timelist) {
		print "<b>$date</b>: &nbsp;\n";
		print join("\n", @timelist);
		print "\n<br>\n";
}
}



sub ShowDir {
print <<"EOM";
Content-type: text/html

<html><head></head><body></body></html>
EOM
}
