#!/usr/bin/perl
# Makelogs script for use with Analog log analyzer
# Location: /usr/local/analog-4.13/makelogs
# Last Updated 12/17/2001.
#  Now works with apache's split-logfile stuff.

# Load virtual hosting info.
open(FILE,"/usr/local/apache/conf/core/hosts.dat") or dienice("can't open virtual host data");
@freeq = <FILE>;
close(FILE);

foreach $line (@freeq) {
        chomp($line);
        ($thetype,$theuser,$thedomain) = split(/ /i,$line);
        if ($thetype ne "#") {
                $domain{$theuser} = $thedomain;
                $type{$theuser} = $thetype;
                $user{$thedomain} = $theuser;
        }
}

#Static setting for hard-coded sites.
$type{'staff'} = "";

#Remove all the old HTML reports.
$dumpme = `rm /home/websites/admin.vectorstar.net/stats/*.html`;

# Do a Directory search.
$filetype = "*.log";
@filelist = `ls -1 --color=no /usr/local/apache/logs/tmp/$filetype`;
for ($counter = 0;$counter <= $#filelist;$counter++) {
        chomp($filelist[$counter]);
}

#Work on each logfile and process.
foreach $file (@filelist) {
        # We want to open the default.cfg from /usr/local/analog-4.13/.
        # We want to replace "replaceme" with the filename, minus the ".log".
        # We want to write analog.cfg and run analog.
        open(MYNUTS,"/usr/local/analog-4.13/default.cfg") or dienice("Could not open $file for input.");
        @farray = <MYNUTS>;
        close(MYNUTS);
        $myinput = "";
        for ($n = 0;$n <= $#farray;$n++) {
                $myinput .= $farray[$n];
        }
        $logfile = $file;
        $logfile =~ s/\/usr\/local\/apache\/logs\/tmp\///g;
        $logfile =~ s/.log//g;

        if (defined($user{$logfile})) {
		#The domain name is defined and known.
                $username = $user{$logfile};
                $thedomain = $logfile;
        } else {
                #This is for an administrative or statically-configured site.
                $username = "staff";
                $thedomain = "$logfile";
        }

        $myinput =~ s/username/$username/g;
        $myinput =~ s/domain/$thedomain/g;
        $myinput =~ s/logfile/$logfile/g;

        open(THISDICK,">/usr/local/analog-4.13/analog.cfg") or dienice("Could Not Open analog.cfg for output.");
        print THISDICK "$myinput";
        close (THISDICK);

        #Done configuring the analog.cfg. Run!

        $dumpme = `/usr/local/bin/analog >/tmp/analog.html 2>/tmp/analog.err`;
	$dump = `cat /tmp/analog.html`;
	$dump =~ s/\"\/analog\/images/\"http:\/\/www.vectorstar.net\/analog\/images/g;
	open(OUT,">/tmp/analog.html") or die("could not write results!\n");
	print OUT $dump;
	close(OUT);
        $dumpme = `cp /tmp/analog.html /home/websites/admin.vectorstar.net/stats/$thedomain.html`;

        if ($type{$username} eq "user") {
                $cmd = "cp /tmp/analog.html /home/".$username."/public_html/stats.html";
                $dumpme = `$cmd`;
        } elsif ($type{$username} eq "group") {
                $cmd = "cp /tmp/analog.html /home/websites/".$thedomain."/stats.html";
                $dumpme = `$cmd`;
        }
        $dumpme = `rm /tmp/analog.html`;
#End foreach file
}

$dumpme = "";

sub dienice {
        my($errmsg) = @_;
        print "$errmsg\n";
        exit;
}

