#!/usr/bin/perl
###############################################
##           Version 05.07.99                ##
###############################################
##   (c)1999 Daevid Vincent                  ##
##  (Edited by Howard Fenter III)            ##
## Unauthorized use of this code is strictly ##
## prohibited and subject to criminal        ##
## prosecution and or death                  ##
###############################################
##         Mails Download Stats              ##
###############################################
use DBI;
require 'ctime.pl';
require "/home/httpd/html/private/sql/mail-lib.pl";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$today = sprintf "%02d.%02d.00",++$mon,$mday,00;
$date= sprintf "2000%02d%02d000000",$mon,$mday;
$mon = $date - 7000000;
$tue = $date - 6000000;
$wed = $date - 5000000;
$thur = $date - 4000000;
$fri = $date - 3000000;
$sat = $date - 2000000;
$sun = $date - 1000000;

$default_db_name  = "wt_client";
@SQLQuery = ( 
	"SELECT COUNT(1) FROM Player_Table", 
	"SELECT COUNT(1) FROM Player_Table WHERE date >= $mon AND date < $date", 
	"SELECT COUNT(1) FROM Player_Table WHERE date >= $mon  and date < $tue", 
	"SELECT COUNT(1) FROM Player_Table WHERE date >= $tue  and date < $wed" ,
	"SELECT COUNT(1) FROM Player_Table WHERE date >= $wed  and date < $thur", 
	"SELECT COUNT(1) FROM Player_Table WHERE date >= $thur  and date < $fri", 
	"SELECT COUNT(1) FROM Player_Table WHERE date >= $fri  and date < $sat", 
	"SELECT COUNT(1) FROM Player_Table WHERE date >= $sat  and date < $sun",
	"SELECT COUNT(1) FROM Player_Table WHERE date >= $sun  and date < $date",
);

@SQLQueryName = ( 
	"      Total Installed Players ", 
	" Players Downloaded This Week ", 
	"   Driver Installs    -------------------------------------------------\n                       Monday ", 
	"			Tuesday ",
	"                     Wednsday ", 
	"                     Thursday ", 
	"                       Friday ", 
	"                     Saturday ",
	"                       Sunday " 
);


# connect to the database, and if there's an error, email someone important.
$dbh = DBI->connect("DBI:mysql:$default_db_name", "nobody", "oct1497");
if ( !defined $dbh ) {
	&send_mail("mySQL server","webmaster\@WildTangent.com","mySQL Database Not Responding.","[$today] Error Connecting to $default_db_name:\n $Mysql::db_errstr\n");
    die "Cannot connect to mySQL server: $DBI::errstr\n";
}

	$mailTxt="Here is your CONFIDENTIAL Report for site activity on $today\n\n**********************************************************************\n\n";
	$i = 0;
	foreach $sql (@SQLQuery) {

		$sth = $dbh->prepare( "$sql");
		if ( !defined $sth ) {   die "Can't Prepare Statement: $DBI::errstr\n";  }
		$sth->execute;

		while(@row = $sth->fetchrow) {
			#print "size of row $#row";
		    $mailTxt.= "$SQLQueryName[$i++]:		$row[0]      \n";
		}

		# Release the statement handle resources
		$sth->finish;
	} # end foreach SQLQuery
	$mailTxt.="\n\n		webmaster\@wildtangent.com";
	&send_mail("webmaster","howard\@wildtangent.com,alyson\@wildtangent.com,alex\@wildtangent.com,bob\@nmpartners.com,michael\@nmpartners.com,steve\@wildtangent.com,jez\@wildtangent.com,kevin\@wildtangent.com,c_myhrvold\@hotmail.com,libby\@wildtangent.com,doug\@wildtangent.com,bargen\@wildseed.com","WILDTANGENT WEEKLY DOWNLOAD STATS FOR $today","$mailTxt");


# Disconnect from the database
$dbh->disconnect;

exit;