#!/usr/bin/perl

use String;

# Testing that both MYSQL and GEOIP are working in conjunction
my ( $both, $mysql, $geoip );
print "Testing MYSQL and GEOIP are operational ... ";
system("./Setup_Tests/INTEGRATED_Test.pl >| INTEGRATED_Test.txt");
$both = system("diff INTEGRATED_Test.txt Setup_Tests/INTEGRATED_Test_Output.txt >| Logs/Setup_Tests.log");
system("rm INTEGRATED_Test.txt");
if ( $both == 0 ) {
	print "ok\n";
} else {
	print "TEST FAILED\nNow testing each component individually\nTo see the errors look in the Setup_Tests log file\n";

	# Testing the connectivity to the database.
	print "Testing MYSQL capabilities ... ";
	system("./Setup_Tests/MYSQL_Test.pl >| MYSQL_Test.txt");
	$mysql = system("diff MYSQL_Test.txt Setup_Tests/MYSQL_Test_Output.txt >> Logs/Setup_Tests.log");
	if ( $mysql == 0 ) {
		print "ok\n";
	} else {
		print "TEST FAILED\nTo see the errors look in the Setup_Tests log file\n";
	}
	system("rm MYSQL_Test.txt");

	# Testing the GEO-IP setup gives the correct responses
	print "Testing GEO-IP ... ";
	system("./Setup_Tests/GEOIP_Test.pl >| GEOIP_Test.txt");
	$geoip = system("diff GEOIP_Test.txt Setup_Tests/GEOIP_Test_Output.txt >> Logs/Setup_Tests.log");
	if ( $geoip == 0 ) {
		print "ok\n";
	} else {
		print "TEST FAILED\nTo see the errors look in the Setup_Tests log file\n";
	}
	system("rm GEOIP_Test.txt");
}

# Testing that the cgi-works
print "Testing GCI capabilities, note assumes uses default cgi-bin folder for cgi scripts\n";
print "If the web page displays a table of ip, longitude, latitude, city and county with all places in Brazil then CGI is enabled on this computer\n";
print "copying CGI script ...";
system("cp CGI_Tests/CGI_Test.pl /var/www/cgi-bin/CGI_Test");
print " ok\n";
print "changing permissions ...";
system("chmod 755 /var/www/cgi-bin/CGI_Test");
print " ok\n";
system(`/cygdrive/c/Program" "Files/Mozilla" "Firefox/firefox.exe localhost/cgi-bin/CGI_Test &`);
my $result;
while (1) {
	print "Did the correct web page show? [y n] ";
	$result = <>;
	my $str = new String($result);
	$str = $str->charAt(0);
	if ( $str eq 'y' || $str eq 'Y') { 
		print "CGI capabilities ... ok\n";
		last;
	}    
	if( $str eq 'n' || $str eq 'N' ) {
		print "TEST FAILED CGI NOT WORKING\n";
		last;
	}
}
print "deleting CGI script from cgi-bin folder ...";
system("rm /var/www/cgi-bin/CGI_Test");
print "ok\n";

print "Testing GCI capabilities with KML files and Google Earth, note assumes uses default cgi-bin folder for cgi scripts\n";
print "If a dialog to download a KML file appears, CGI with KML files is working correctly\n";
print "copying KML CGI script ...";
system("cp CGI_Tests/CGI_KML_Test.pl /var/www/cgi-bin/CGI_KML_Test");
print " ok\n";
print "changing permissions ...";
system("chmod 755 /var/www/cgi-bin/CGI_KML_Test");
print " ok\n";
system(`/cygdrive/c/Program" "Files/Mozilla" "Firefox/firefox.exe localhost/cgi-bin/CGI_KML_Test &`);
while (1) {
	print "Did the download appear and open in Google Earth? [y n] ";
	$result = <>;
	my $str = new String($result);
	$str = $str->charAt(0);
	if ( $str eq 'y' || $str eq 'Y') {
		print "KML CGI capabilities ... ok\n"
	}
	if ( $str eq 'n' || $str eq 'N'){
		print "TEST FAILED -- KML CGI Capabilities not working"
	}
last; 	
}
print "deleting CGI script from cgi-bin folder ...";
system("rm /var/www/cgi-bin/CGI_KML_TEST");
print "ok\n";
