#!/usr/bin/perl

use Net::FTP;
use File::Find;

my $host = "XXXXXXXX";
my $user = "XXXXXXX";
my $password = "XXXXXXX";
my $raw_log = "/tmp/raw_logfile.dat";
my $efi_logfile = "/tmp/034efi.com.dat";
my $tq_logfile = "/tmp/80tq.com.dat";
my $tempfile = "/tmp/raw_logfile_temp.dat";
my $htdoc_root = "/usr/local/apache/htdocs";
my $line;

my $ftp = Net::FTP->new($host) or die "Could Not open FTP connection to $host!: $!\n";
$ftp->login($user,$password) or die "Login failed.. : $!\n";
$ftp->cwd(".logs/data") or die "Could not change directory to .logs/data: $!\n";
my @lines = $ftp->dir("./") or die "Could not ls the directory: $!\n";
foreach $line (@lines) {
	if($line =~ /(mswanson\.com.+$)/) {
		$ftp->get($1,"$raw_log.gz") or die "Could not get $1: $!\n";
		break;
	}
}
#step back to the base directory
$ftp->cwd("../../") or die "Could not change directory to root: $!\n";
#need to unzip the log
my $command = "gunzip -f $raw_log.gz";
system($command);
parse_log("80tq.com",$tq_logfile);
parse_log("034efi.com",$efi_logfile);
run_webalizer($tq_logfile,"80tq.com");
run_webalizer($efi_logfile,"034efi.com");
run_webalizer($raw_log,"mswanson.com");

$ftp->quit();

sub upload_stats {
	my $website_name = shift;
	#check the ftp connection 
	if(!$ftp) { #connection has gone away, reconnect!
		$ftp = Net::FTP->new($host) or die "Could Not open FTP connection to $host!: $!\n";
		$ftp->login($user,$password) or die "Login failed.. : $!\n";
	}
	if($website_name ne "mswanson.com") {
		$ftp->cwd($website_name) or die "Could not change directory to $website_name: $!\n";
	}
	$ftp->cwd("stats") or die "Could not change to the stats directory: $!\n";
	opendir(DIR,"$htdoc_root/$website_name/stats") or die "could not open directory $htdoc_root/$website_name/stats: $!\n";
	while(defined($file = readdir(DIR))) {
		if($file =~ /^\./) {
			next;
		}
		if($file =~ /^\.\.$/) {
			next;
		}
		if(!-f "$htdoc_root/$website_name/stats/$file") {
			next;
		}

		#this is a file that needs to be uploaded to the server
		$ftp->put("$htdoc_root/$website_name/stats/$file") or die "Upload of file: $htdoc_root/$website_name/stats/$file failed: $!\n";
	}
	closedir(DIR);
	#step back the appropriate number of levels
	if($website_name eq "mswanson.com") { 
		$ftp->cwd("../") or die "Could not go back one directory: $!\n";
	}
	else {
		$ftp->cwd("../../") or die "Could not go back two directories: $!\n";
	}
}
sub run_webalizer {
	my $log_to_process = shift;
	my $website_name = shift;
	my $command = "logresolve < $log_to_process > $tempfile";
	system($command);
	#now, actually run webalizer
	$command = "webalizer -Q -n $website_name -o /usr/local/apache/htdocs/$website_name/stats -i $tempfile";
	system($command);
	upload_stats($website_name);
}

sub parse_log {
	my $string = shift;
	my $output_log = shift;
	#parse the logfile of anything with $string in it
	open LOG, $raw_log;
	open OUTPUT_LOG, ">$output_log";
	open NEW_RAW_LOG, ">$tempfile";
	while(<LOG>) {
		if(/$string/) {
			print OUTPUT_LOG $_;
		}
		else {
			print NEW_RAW_LOG $_;
		}
	}
	close(OUTPUT_LOG);
	close(NEW_RAW_LOG);
	close(LOG);
	system("mv -f $tempfile $raw_log");
}	
