#!/usr/bin/perl
###############################################
##           Version 05.07.99                ##
###############################################
##   (c)1999 Daevid Vincent                  ##
## Unauthorized use of this code is strictly ##
## prohibited and subject to criminal        ##
## prosecution and or death                  ##
###############################################
##  Generates email lists from User_Table    ##
###############################################

$default_db_name  = "wt_client";
@SQLQuery = ( 
	"SELECT COUNT(*) FROM Player_Table", 
	"SELECT COUNT(*) FROM Player_Table WHERE DAYOFMONTH(last_update) = DAYOFMONTH(NOW()) AND MONTH(date) = MONTH(NOW())", 
	"SELECT COUNT(*) FROM Player_Table WHERE DAYOFMONTH(date) = DAYOFMONTH(NOW()) AND MONTH(date) = MONTH(NOW())", 
	"SELECT COUNT(*) FROM Player_Table WHERE date >= 19990603060000" ,
	"SELECT COUNT(*) FROM User_Table", 
	"SELECT COUNT(*) FROM User_Table WHERE DAYOFMONTH(date) = DAYOFMONTH(NOW()) AND MONTH(date) = MONTH(NOW())", 
	"SELECT COUNT(*) FROM Test_Table", 
	"SELECT COUNT(*) FROM SubTest_Table"
#	"SELECT usertype_name, COUNT(*) FROM User_Table, UserType_Table WHERE usertype_table_id = usertype_id GROUP BY usertype_name", 
);

@SQLQueryName = ( 
	"Total Players", 
	"Players that updated today", 
	"New Players Today", 
	"Players Since Press Release",
	"Total Users", 
	"New Users Today", 
	"Total Test Suites", 
	"Total SubTests" 
#	"Types of users", 
);

use DBI;
require 'ctime.pl';
require "./mail-lib.pl";

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$today = sprintf "%02d.%02d.%02d %02d:%02d",++$mon,$mday,$year,$hour,$min;

print "\n<TABLE BORDER=1 CELLSPACING=0 CELLPADDING=10>\n<TR><TD BGCOLOR=#FFFFFF>Query ran at $today</TD></TR>\n</TABLE>\n<P>\n";

# 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";
}

	print "<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=10>\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";
		    print "<TR ALIGN=RIGHT><TD><B>$SQLQueryName[$i++]:</B></TD><TD><B>$row[0]</B></TD></TR>\n";
		}

		# Fetch the rows back from the SELECT statement
		#while ( @row = $sth->fetchrow() ) {
	    #	print "Row returned: @row\n";
		#}

		# Fetch the data back into separate variables this time
		# while ( ( $id, $name ) = $sth->fetchrow() ) {
		#    print "ID: $id\tName: $name\n";
	    # }

		# Release the statement handle resources
		$sth->finish;
	} # end foreach SQLQuery

	print "</TABLE>\n<P>\n";

# Disconnect from the database
$dbh->disconnect;

exit;
