#!/usr/bin/perl

use Net::FTP;
use File::stat;

sub get_files;

# Configuration 
$host ="ftp.plus.net";	# the FTP server
$username = "username_here";	# username
$password = "password_here";	# password
$domain = "cgi.username.plus.com"; 	# Domain name
$configFile = "/files/homeX/username/webalizer/cgistats.conf";	# Webalizer config file
$outputDir = "/files/homeX/username/webalizer/cgi";	# Webalizer output directory
$webalizer_cmd = "/usr/local/bin/webalizer";	# Webalizer command to run
$webstats_status = "webstats_cgi_status";
$files_processed = 0;



#
# *** NO CONFIGURATION OPTIONS BELOW THIS POINT ***
#

# Change directory
chdir $outputDir || die("Cannot change directory to $outputDir\n");

# Login to ftp server
$ftp = Net::FTP->new("$host", Debug =>0);
$ftp->login("$username","$password") || die( "Cannot login to FTP server '$host' using username '$username'.\n" );
$ftp->cwd("logs") || die( "Cannot change directory to 'logs'.\n" );
$ftp->binary;


print "$domain WebStats analysis started at " . localtime(time) . "\n\n";

# Does an old status file exist?
if( -e "$webstats_status" )
{
	$st = stat("$webstats_status");
	$lastRunTime = $st->mtime;      # this is the timestamp of the last log file analysed
}
else
{
	$lastRunTime = 0 ;      # this script has never run before.
}

if ($lastRunTime == 0)
{
	print "First ever run - if you keep getting this then there are no webstats files\n";
}
else
{
	print "Last run " . localtime($lastRunTime) . " ($lastRunTime)\n";
}


# Determine which log file to download
# This will check for old archives of log files.
if( ($newFileTime = $ftp->mdtm("$domain.6.gz")) > $lastRunTime )
{
	$domainfile = $domain . ".6" ;
	$lastRunTime = $newFileTime;
	get_files();
}
if( ($newFileTime = $ftp->mdtm("$domain.5.gz")) > $lastRunTime )
{
	$domainfile = $domain . ".5" ;
	$lastRunTime = $newFileTime;
	get_files();
}
if( ($newFileTime = $ftp->mdtm("$domain.4.gz")) > $lastRunTime )
{
	$domainfile = $domain . ".4" ;
	$lastRunTime = $newFileTime;
	get_files();
}
if( ($newFileTime = $ftp->mdtm("$domain.3.gz")) > $lastRunTime )
{
	$domainfile = $domain . ".3" ;
	$lastRunTime = $newFileTime;
	get_files();
}
if( ($newFileTime = $ftp->mdtm("$domain.2.gz")) > $lastRunTime )
{
	$domainfile = $domain . ".2" ;
	$lastRunTime = $newFileTime;
	get_files();
}
if( ($newFileTime = $ftp->mdtm("$domain.1.gz")) > $lastRunTime )
{
	$domainfile = $domain . ".1" ;
	$lastRunTime = $newFileTime;
	get_files();
}
if( ($newFileTime = $ftp->mdtm("$domain.0.gz")) > $lastRunTime )
{
	$domainfile = $domain . ".0" ;
	$lastRunTime = $newFileTime;
	get_files();
}
if( ($newFileTime = $ftp->mdtm("$domain.gz")) > $lastRunTime )
{
	$domainfile = $domain ;
	get_files();
}
$ftp->quit;

print "\n$files_processed files processed in this run\n";
print "\n$domain WebStats analysis finished at " . localtime(time) . "\n";


########################################################################################################################

sub get_files
{
	# Get the timestamp and size of the log file from the ftp server
	$newFileSize = $ftp->size("$domainfile.gz");


	# Retreive the file via ftp
	print "Getting $domainfile.gz ($newFileSize bytes)\n";
	print "Log file timestamp: " . localtime($newFileTime) . " ($newFileTime)\n";
	$ftp->get("$domainfile.gz");
	
	# Unzip
	$unzip = `gunzip -f $domainfile.gz 2>&1`;
	if( $unzip )	# unsuccessful
	{
		print "gunzip failed.\n";
		print "$unzip\n";
	}
	else	#successful
	{
		# Run the Webalizer
		print "Running Webalizer...\n";
		$webalizer = `$webalizer_cmd -c $configFile -o $outputDir -T $domainfile`;
		print $webalizer;
	}

	print "Deleting log file... $domainfile \n";
	system( "rm -f $domainfile" );	#delete the log file
	
	# Get the timestamp of the log file
	($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
		localtime($newFileTime);
	$year += 1900; $mon += 1;
	$timestamp = sprintf("%.4d%.2d%.2d%.2d%.2d.%.2d",$year, $mon, $mday, $hour, $min, $sec) ;
	system( "touch -t $timestamp $webstats_status" );	#record when we run
	$files_processed++;
	
}


