#!/usr/bin/perl
#
#-------------------------------------------------------------------------------
# DISCUS COPYRIGHT NOTICE
#
# Discus is copyright (c) 2002 by DiscusWare, LLC, all rights reserved.
# The use of Discus is governed by the Discus License Agreement which is
# available from the Discus WWW site at:
#    http://www.discusware.com/discus/license
#
# Pursuant to the Discus License Agreement, this copyright notice may not be
# removed or altered in any way.
#-------------------------------------------------------------------------------

use strict;
use vars qw($GLOBAL_OPTIONS $PARAMS $DCONF);

#-------------------------------------------------------------------------------

$| = 1;
print "Content-type: text/html\n\n";
print "<html><head><title>Discus Program Diagnostics</title>\n";
print "</head>\n";
print "<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#0000ff\" vlink=\"#800080\">\n";
print "<font face=\"verdana,arial,helvetica\" size=\"2\">\n";
print "<center><b>Discus Program Diagnostics</b></center><hr>\n";

#-------------------------------------------------------------------------------

$DCONF->{script_dir} = "/mnt/kw2/03/955/00000018/htdocs/cgi-bin/forum";

#-------------------------------------------------------------------------------

print "</font><pre><font face=\"courier new\" size=\"2\">\n";
print "<b>Your Perl Version is:</b>", " " x (50 - length("Your Perl Version is:"));
print "$]\n";

#-------------------------------------------------------------------------------

print "<b>Getting CGI extension</b>", " " x (50 - length("Getting CGI extension"));
my $e = get_cgi_extension($0);
if ($e ne "") {
	print "<font color=\"#00aa00\">PASSED</font> ($e)\n";
} else {
	print "<font color=\"#ff0000\">FAILED</font>\n";
	print "Script Execution Error: <b>config.*** location error [1]</b>\n";
	print "</font></pre></body></html>\n";
	exit(0);
}

#-------------------------------------------------------------------------------

print "<b>Checking existence of config.$e</b>", " " x (50 - length("Checking existence of config.$e"));
if (-e "$DCONF->{script_dir}/config.$e") {
	print "<font color=\"#00aa00\">PASSED</font> (1)\n";
} elsif (-e "./config.$e") {	
	print "<font color=\"#00aa00\">PASSED</font> (2)\n";
} else {
	print "<font color=\"#ff0000\">FAILED</font>\n";
	print "Script Execution Error: <b>config.*** location error [1]</b>.\n";
	print "</font></pre></body></html>\n";
	exit(0);
}

#-------------------------------------------------------------------------------

print "<b>Reading config.$e via 'require'</b>", " " x (50 - length("Reading config.$e via 'require'"));
$PARAMS->{no_execute_config} = 1;
if (-e "$DCONF->{script_dir}/config.$e") {
	if (require "$DCONF->{script_dir}/config.$e") {
		print "<font color=\"#00aa00\">PASSED</font>\n";
	} else {
		diag_bottom("Could not require config.$e (1): $!\n");
	}
} elsif (-e "./config.$e") {
	if (require "./config.$e") {
		print "<font color=\"#00aa00\">PASSED</font>\n";
	} else {
		diag_bottom("Could not require config.$e (2): $!\n");
	};
}
$PARAMS->{no_execute_config} = 0;

#-------------------------------------------------------------------------------

print "<b>Checking existence of discus.conf file:</b>", " " x (50 - length("Checking existence of discus.conf file:"));
my $discus_conf = discus_conf();
if (-e $discus_conf) {
	print "<font color=\"#00aa00\">PASSED</font>\n";
} else {
	exit discus_conf_doesnt_exist();
}

#-------------------------------------------------------------------------------

print "<b>Reading discus.conf file:</b>", " " x (50 - length("Reading discus.conf file:"));
if (open (DISCUSCONF, "< $discus_conf")) {
	$DCONF = {};
	while (<DISCUSCONF>) {
		if (/^(\w+)=(.*)/) {
			my ($one, $two) = ($1, $2); $two =~ s/\r//g;
			$DCONF->{$one} = $two;
		}
	}
	close (DISCUSCONF);
	print "<font color=\"#00aa00\">PASSED</font>\n";
} else {
	diag_bottom("Could not read discus.conf: $!\n");
}

#-------------------------------------------------------------------------------

print "<b>Checking source directory existence:</b>", " " x (50 - length("Checking source directory existence:"));
$DCONF->{source_dir} = get_source_dir() if ! $DCONF->{source_dir};
if (-e $DCONF->{source_dir}) {
	print "<font color=\"#00aa00\">PASSED</font>\n";
} else {
	diag_bottom("Make sure you spelled 'source' correctly during installation :)\n");
}		

#-------------------------------------------------------------------------------

if ($DCONF->{pro}) {
	print "<b>Obtaining Pro File ID:</b>", " " x (50 - length("Obtaining Pro File ID:"));
	$PARAMS->{no_execute_config} = 1;
	$DCONF->{pro_fileid} = get_pro_fileid();
	if ($DCONF->{pro_fileid} > 0) {
		print "<font color=\"#00aa00\">PASSED</font>\n";
	} else {
		diag_bottom("Make sure you capitalized 'PRO' under the 'source' directory\n");
	}
}

#-------------------------------------------------------------------------------

print "<b>Checking existence of 'common.pl' script:</b>", " " x (50 - length("Checking existence of 'common.pl' script:"));
if (-e "$DCONF->{source_dir}/common.pl") {
	print "<font color=\"#00aa00\">PASSED</font>\n";
} else {
	diag_bottom("Make sure you uploaded the common.pl file to the 'source' directory\n");
}

#-------------------------------------------------------------------------------

print "<b>Checking integrity of 'common.pl' script:</b>", " " x (50 - length("Checking integrity of 'common.pl' script:"));
my $complete = 0;
my $ascii = 1;
if (open (COMMON, "< $DCONF->{source_dir}/common.pl")) {
	while (<COMMON>) {
		$complete = 1 if /^1;\s+$/;
		$ascii = 0 if /\r/;
	}
	close (COMMON);	
} else {
	diag_bottom("Could not open common.pl file: $!\n");
}
if (! $complete) {
	diag_bottom("Try re-uploading the common.pl file; it was not completely uploaded\n");
} else {
	print "<font color=\"#00aa00\">PASSED</font>\n";
}
print "<b>Checking transfer mode of 'common.pl' script:</b>", " " x (50 - length("Checking transfer mode of 'common.pl' script:"));
if (! $ascii) {
	diag_bottom("The common.pl and other files under 'source' must be uploaded in ASCII mode\n");
} else {
	print "<font color=\"#00aa00\">PASSED</font>\n";
}

#-------------------------------------------------------------------------------

print "<b>Reading in 'common.pl' using 'require':</b>", " " x (50 - length("Reading in 'common.pl' using 'require':"));
if (require "$DCONF->{source_dir}/common.pl") {
	print "<font color=\"#00aa00\">PASSED</font>\n";
} else {
	diag_bottom("Failed to read in common.pl: $!");
}	

#-------------------------------------------------------------------------------

print "</font><font face=\"verdana,arial,helvetica\" size=\"2\">\n";
print "<p>At this time, control is being passed to the common.pl subroutine so that\n";
print "the diagnostics from the 'diags.pl' script in your 'source' directory can be\n";
print "executed.  If you see nothing below this point, passing control to common.pl\n";
print "failed, and you may see more information in your server's error log.</p>\n";
print "</font>\n";

discus("diagnose");

#-------------------------------------------------------------------------------

sub get_cgi_extension {
	my ($zero) = @_;
	if ($zero =~ m|\.(\w+)$|) {
		return $1;
	}
	foreach my $try ('cgi', 'pl') {
		return $try if -e "./config.$try";
	}
	opendir(DIR, ".");
	while (my $i = readdir(DIR)) {
		return $1 if $i =~ m|^config\.(\w+)$|;
	}
	closedir(DIR);
	return "";
}

sub diag_bottom {
	my ($message) = @_;
	print "<font color=\"#ff0000\">FAILED</font>\n";
	print $message;
	print "</font></pre></body></html>\n";
	exit(0);	
}

sub discus_conf_doesnt_exist {
	print "<font color=\"#ff0000\">FAILED</font>\n";
	print "</font></pre>\n";
	print "<font size=\"2\" face=\"verdana,arial,helvetica\">\n";
	print "<p align=center><b>Script Execution Error</b></p>\n";
	print "<hr>\n";
	print "<p>Your discus.conf file could not be found.  This script will now\n";
	print "attempt to locate your discus.conf file based on your settings.  Please\n";
	print "stand by...</p>\n";
	
	print "</font></body></html>\n";
	return 0;	
}

1;
