#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 2001 neddix, stuttgart
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use esmith::config;
use esmith::util;
use esmith::db;

#------------------------------------------------------------
# run the awstats logfile pre-processor
#------------------------------------------------------------

my $event = $ARGV [0];

my $configDB  = esmith::ConfigDB->open or die("can't open Config DB");
my $status = $configDB->get_prop( "AWStats", "status" );


if (defined $status && $status eq "enabled")
{
	my $awconfpath="/etc/e-smith/web/panels/manager/cgi-bin/.awstats";
	my $mtime;
	my $prev_mtime=0;
	(undef,undef,undef,undef,undef,undef,undef,undef,undef,$mtime)=stat("$awconfpath/awstats.conf");

	exit 0 if (!defined $mtime);

	while ($mtime != $prev_mtime) {
		open (IN, "$awconfpath/awstats.conf") or die "\nCould not open $awconfpath/awstats.conf\n";
		open (OUT, ">$awconfpath/awstats.conf.$$") or die "\nCould not open $awconfpath/awstats.conf.$$\n";
		while (<IN>) {
			print OUT $_;
		}
		close (IN);
		close (OUT);
		(undef,undef,undef,undef,undef,undef,undef,undef,undef,$prev_mtime)=stat("$awconfpath/awstats.conf");
	}

    esmith::util::backgroundCommand (1, "/sbin/e-smith/awstats-pp", "-c$awconfpath/awstats.conf.$$");
}

exit (0);
