#!/usr/local/bin/perl
require "getopts.pl";
require 'ctime.pl';

$Version="MakeHtmlStatTable 1.0";

$maintainer="Jens Elkner";
$email="elkner\@irb.cs.uni-magdeburg.de";

#---------------------------------WWW Index--------------------------------
# The names of all statfiles
@www_files = ("l.html","c.html","g.html","r.html","a.html","p.html");

# The Names for http-links in the same Order as above
@www_abrev = ("L","C","G","R","A","P");

#The description for the abbriavation in the same order as above
@www_desc = (
	     "Lite Web Log Report &nbsp;&nbsp;<i>(about 10 &plusmn; 3 KBytes)</i>",
	     "Complete Web Log Report &nbsp;&nbsp;<i>(about 1024 &plusmn; 300 KBytes)</i>",
	     "Graphic Web Log Report &nbsp;&nbsp;<i>(about 100 &plusmn; 15 KBytes)</i>",
	     "Referring URL Statistics &nbsp;&nbsp;<i>(about 180 &plusmn; 50 KBytes)</i>",
	     "WWW Browser Agent Statistics &nbsp;&nbsp;<i>(about 220 &plusmn; 50 KBytes)</i>",
	     "Proxy Access Statistics &nbsp;&nbsp;<i>(about 10 &plusmn; 5 KBytes)</i>"
	     );

#---------------------------------FTP Index---------------------------------
# The names of all statfiles
@ftp_files = ("l.html","c.html","g.html");

# The Names for http-links in the same Order as above
@ftp_abrev = ("L","C","G");

#The description for the abbriavation in the same order as above
@ftp_desc = (
	     "Lite Web Log Report&nbsp;&nbsp;&nbsp;&nbsp;<i>(about 10 +- 3 KBytes)</i>",
	     "Complete Web Log Report&nbsp;&nbsp;&nbsp;&nbsp;<i>(about 1024 +- 300 KBytes)</i>",
	     "Graphic Web Log Report&nbsp;&nbsp;&nbsp;&nbsp;<i>(about 100 +- 15 KBytes)</i>"
	     );

#------------------------------Start-------------------------
&Initialize;
if ($header) {
    print OUT <HEADER>;
    close(HEADER);
}
else {
    print OUT 
	"<html> <head>\n",
	"<title>$tmp Statistics of $server</title>\n<head>\n\n\<body>\n<center>",
	"<h1>$tmp Statistics of <blink>$server</blink></h1></center>\n\n",
	"<hr>\n<p>\n";
}

print OUT "<p>\n<ul>\n";
for (0..$#files) {
    print OUT "<b>",$abrev[$_],"</b> ... ",$desc[$_],"<br>\n";
}

print OUT 
    "<p>\n</ul>\n<hr>\n<center>\n<p>\n",
    "<table border=3 cellpadding=2>\n",
    "<caption align=bottom>\n",
#    "<br><b>Note:</b> The current month is not included in total statistics!\n",
    "</caption>\n",
    "<tr align=center>\n",
    "<th>&nbsp;\n";

$tmp = $thisyear;
$end = $thisyear - $years;
while ( $tmp > $end ) {
    push(@allyears,$tmp);
    $tmp--;
}
foreach (@allyears) {
    print OUT "    <th>$_\n";
}
$month=0;
foreach (@allmonth) {
    $month++;
    $s_month = ($month < 10) ? "0".$month : $month;
    print OUT "<tr align=center>\n<th>$_\n";
    foreach (@allyears) {
	$year = $_;
	$year =~ s/^..//;
	print OUT "    <td>\n";
	$txt = "";
	$count=0;
	foreach (@files) {
	    if ( -f "$path/${year}${s_month}/$_" ) {
		$txt .= "        <a href=\"${year}${s_month}/$_\">".
		    $abrev[$count]."</a>\n";
	    }
	    $count++;
	}
	if ($txt eq "") {
	    $txt = "        &nbsp;\n";
	}
	print OUT $txt;
    }
}
print OUT "<tr align=center>\n<th>TOTAL\n";
foreach (@allyears) {
    $year = $_;
    $year =~ s/^..//;
    print OUT "    <td>\n";
    $txt = "";
    $count=0;
    foreach (@files) {
	if ( -f "$path/total${year}/$_" ) {
	    $txt .= "        <a href=\"total${year}/$_\">".
		$abrev[$count]."</a>\n";
	}
	$count++;
    }
    if ($txt eq "") {
	print OUT "        &nbsp;\n";
    }
    else {
	print OUT $txt;
    }
}
    
$tmp = &ctime(time);
print OUT "</table>\n</center><br>\n<hr>\n",
    "<b>Note:</b><br>\n",
    "If you can't see a table or the links within the table you should\n",
    "change to a HTML 3.0 capable Browser (Netscape is recommended).<br>\n",
    "<hr>\n";

if ($trailer) {
    print OUT <TRAILER>;
    close(TRAILER);
}
else {
    print OUT
	"<address><a href=\"mailto:$email\">\n", $maintainer, 
	" &lt;", $email, "&gt;</a></address>\n",
	"<!-- hhmts start -->\nLast modified: ", $tmp, "<!-- hhmts end -->\n",
	"</body> </html>";
}
 
sub Initialize {
    $result = &Getopts('H:ho:p:s:T:t:v');
    @allmonth = ("Januar",
		 "February",
		 "March",
		 "April",
		 "May",
		 "June",
		 "July",
		 "August",
		 "September",
		 "October",
		 "November",
		 "December"
		 );

    &Usage if $opt_h;
    &Version if $opt_v;
    
    # Header file - will be inserted at the very beginning of the  Output file
    if ($opt_H) {
	$header = $opt_H;
	if ( ! open(HEADER,$header)) {
	    die "Can not read header file $header\n";
	}
    }

    # Output file, where the generated HTML File
    $output = $opt_o ? $opt_o : "-" ;

    # system path, where the html stat files are located
    $path = $opt_p ? $opt_p : ".";
    die "$path does not exist!\n" if ( ! -d $path );
    if ( ! open(OUT,">$path/$output") ) {
	close(HEADER) if $header;
	die "Can not open output file $output\n";
    }

    # Trailer file - will be appended at the end of the Output file
    if ($opt_T) {
	$trailer = $opt_T;
	if ( ! open(TRAILER,$trailer) ) {
	    close(HEADER) if $header;
	    close(OUT);
	    die "Can not read trailer file $trailer\n";
	}
    }

    # Name of the server
    $server = $opt_s if $opt_s;

    # Type of Table
    $type = $opt_t ? $opt_t : "www";
    if ( ($type ne "www") && ($type ne "ftp") ) {
	die "Unknown type $type !\n";
    }

    # Type of Table
    $years = $opt_y ? $opt_y : 3;
    if ( (! ($years =~ m#^\d+$#o) || ($years < 1) )) {
	die "Need to know how many years - use a number > 0 !\n";
    }
    if ($type eq "www") {
	@files = @www_files;
	@abrev = @www_abrev;
	@desc = @www_desc;
	$tmp = "WWW";
    }
    if ($type eq "ftp") {
	@files = @ftp_files;
	@abrev = @ftp_abrev;
	@desc = @ftp_desc;
	$tmp = "FTP";
    }
    $thisyear = &ctime(time);
    chop $thisyear;
    $thisyear =~ s/^.*(\d{4})$/$1/;
}

sub Usage {
    die <<"EndUsage";
$Version

Usage: makeHtmlStatTable.pl [-h] [-v] [-H file] [-o file] -p path [-s name]
                            [-T file] [-t type] [-y years]

Options:
  -h        Display this message and quit.
  -v        Display version and quit.

  -H file   This header file will be inserted at the very beginning of the
            generated file
  -o file   File where to store generated results (default = STDOUT)
  -p path   System path, where the html stat files are located
  -s name   Name of the Server (only used, if there is no header file)
  -T file   This trailer file will be appended at the End of the generated file
  -t type   Type of the stat html table (default = www)
  -y years  How many years do you want to have in the generated table 
            (default = 3)
EndUsage
}

sub Version {
    die << "EndVersion";
$Version
EndVersion
}

