#!/usr/local/bin/perl
$ScriptDir = "/local/www/etc";
$BaseHtmlDir = "/local/www/htdocs/stats";
$BaseLogDir = "/local/logs";
$timeout = 30;                               # wait n  Minutes
$Zcat="/usr/local/bin/gzip -cd";

# The next Line has to be always at this line (before calling the other subs)
&SetDates;

# ===================== Now you might change the lines ==================
#                                                                       #
# First we wait, until access_log is compressed ...
#&CheckFile("access_log.${oldyear}${oldmonth}.gz","$BaseLogDir/WWW/SAVE");
# resolve unresolved entries in access_log
#&PreProcessAccessLog("$BaseLogDir/WWW/SAVE");

# Make all WWW related stuff
&SetupHtmlDirs("${BaseHtmlDir}/WWW");
&SetupHtmlDirs("${BaseHtmlDir}/elkner");
&MakeStats("www");

&CheckFile("referer_log.${oldyear}${oldmonth}.gz","$BaseLogDir/WWW/SAVE");
&MakeStats("referer");

&CheckFile("agent_log.${oldyear}${oldmonth}.gz","$BaseLogDir/WWW/SAVE");
&MakeStats("agent");

&CheckFile("proxy_log.${oldyear}${oldmonth}.gz","$BaseLogDir/WWW/SAVE");
&MakeStats("proxy");

system("${ScriptDir}/mkHtmlStatTable.pl -o index.html -p ${BaseHtmlDir}/WWW -s IRB.CS.Uni-Magdeburg.De");
system("${ScriptDir}/mkHtmlStatTable.pl -o index.html -p ${BaseHtmlDir}/elkner -s \"IRB.CS.Uni-Magdeburg.De<br>~elkner\"");

# Make all FTP related stuff
&SetupHtmlDirs("${BaseHtmlDir}/FTP");
&SetupHtmlDirs("${BaseHtmlDir}/FTP/gnu");
&SetupHtmlDirs("${BaseHtmlDir}/FTP/linux");
&SetupHtmlDirs("${BaseHtmlDir}/FTP/Java");
&CheckFile("xferlog.${oldyear}${oldmonth}.gz","$BaseLogDir/FTP/SAVE");
&MakeStats("ftp");

system("${ScriptDir}/mkHtmlStatTable.pl -o index.html -p ${BaseHtmlDir}/FTP -s IRB.CS.Uni-Magdeburg.De -t ftp -H ${BaseHtmlDir}/FTP/head.txt");
system("${ScriptDir}/mkHtmlStatTable.pl -o index.html -p ${BaseHtmlDir}/FTP/gnu -s \"IRB.CS.Uni-Magdeburg.De<br>GNU Mirror\" -t ftp");
system("${ScriptDir}/mkHtmlStatTable.pl -o index.html -p ${BaseHtmlDir}/FTP/linux -s \"IRB.CS.Uni-Magdeburg.De<br>Linux Mirror\" -t ftp");
system("${ScriptDir}/mkHtmlStatTable.pl -o index.html -p ${BaseHtmlDir}/FTP/Java -s \"IRB.CS.Uni-Magdeburg.De<br>Java Mirror\" -t ftp");
#                                                                         #
# ====================== No further changes are necessary =================
sub SetupHtmlDirs {
    $Dir = $_[0];
    chdir "$Dir";
    mkdir ("${thisyear}${thismonth}",22) if (! -d "${thisyear}${thismonth}" );
    mkdir ("${oldyear}${oldmonth}",22) if (! -d "${oldyear}${oldmonth}" );
    mkdir ("total${thisyear}",22) if (! -d "total${thisyear}" );
    mkdir ("total${oldyear}",22) if (! -d "total${oldyear}" );
    chmod 0755, "${thisyear}${thismonth}", "${oldyear}${oldmonth}", "total${thisyear}", "total${oldyear}";
    rmdir "current" if (-d "current");
    unlink "current" if (-l "current");
    symlink("${thisyear}${thismonth}", "current");
}

sub CheckFile {
    ($file,$Dir) = $_[0,1];
    chdir "$Dir";
    $oldsize=0;
    $newsize=1;
    $count=0;
STEP: sleep (60);
    if (! -r $file) {
	$count++;
	goto STEP if ($count < $timeout);
	warn "timeout of $timeout minutes exceeded (no $file)!\n $0 aborted\n.";
	return 0;
    }
    $count=0;
CHECK: sleep(60);
    $newsize = (stat($file))[7];
    if ( ($newsize == 0) || ($newsize != $oldsize)) {
	$count++;
	$oldsize = $newsize;
	goto CHECK if  ($count < $timeout);
	warn "timeout of $timeout minutes exceeded ($file still grows)!\n $0 aborted\n.";
	return 0;
    }
    1;
}

sub MakeStats {
    $Type = $_[0];
    system("$ScriptDir/makestats $Type ${oldyear}${oldmonth}");
    system("$ScriptDir/makestats $Type total $oldyear");
    system("$ScriptDir/makestats $Type current");
    system("$ScriptDir/makestats $Type total $thisyear") if ( $oldyear != $thisyear);
}

sub PreProcessAccessLog {
    $LogDir = $_[0];
    chdir "$LogDir";
    $myfile = "access_log.${oldyear}${oldmonth}";
    system ("$ScriptDir/ppaccesslog.pl ${myfile}.gz");
    if ( ! open(FILE,"$Zcat $myfile.gz |")) {
	die "Can't open ${myfile}.gz\n $0 aborted!\n";
    }
    $count=0;
    while (<FILE>) {
	$count++;
    }
    close(FILE);
    $oldsize=$count;
    if ( ! open(FILE,"$Zcat ${myfile}.new.gz |")) {
	die "Can't open ${myfile}.new.gz\n $0 aborted!\n";
    }
    $count=0;
    while (<FILE>) {
	$count++;
    }
    close(FILE);
    if ( $oldsize == $count ) {
	rename("${myfile}.new.gz", "${myfile}.gz");
    }
    else {
	if ($count < $oldsize) {
	    die "New access_log is shorter than the old one!\n" ;
	}
	else {
	    die "New access_log is bigger than the old one!\n" ;
	}
    }
}

sub SetDates {
    ($thismonth,$thisyear) = (localtime(time))[4,5];
    if ($thismonth == 0) {
	$oldmonth = 12;
	$oldyear = $thisyear -1 ;
    }
    else {
	$oldmonth = $thismonth;
	$oldyear = $thisyear ;
    }
    $thismonth++;
    $thismonth = "0$thismonth" if ($thismonth < 10);
    $oldmonth = "0$oldmonth" if ($oldmonth < 10);
    $thisyear = $thisyear - 100 if ($thisyear > 99);
    $oldyear = $oldyear - 100 if ($oldyear > 99);
    $thisyear = "0$thisyear" if ($thisyear < 10);
    $oldyear = "0$oldyear" if ($oldyear < 10);
}
