#!/usr/bin/perl
#
# Master Pre-Installation Tester 
#          Version 1.0
# October 16, 2000
# Author: William Bontrager
# Author's E-mail: william@willmaster.com
# Author's Website: http://www.willmaster.com/
#
# Master Pre-Installation Tester is designed to test your server in various 
#     ways to assist you in determining directory paths and whether or not 
#     specific Perl scripts will run on your server.
#
# It is designed to be executed with Perl 5+; it won't work with versions 
#      prior to 5. It was developed and tested on a Unix server.
#
# COPYRIGHT NOTICE
# Copyright 1998-2000 by William Bontrager. All rights reserved.
#
# Before installing and/or using Master Pre-Installation Tester, 
# you must agree to the complete license agreement
# linked from http://willmaster.com/master 
#
#===============================
#
#        I N S T R U C T I O N S
#
# All modifications must be made with an ASCII/plain text word processor (NotePad 
# and BBEdit are good). Also, when you upload the script, it must be uploaded as 
# ASCII/plain text.
#
# The file name of Master Pre-Installation Tester must have the extension your
# server requires for Perl CGI scripts. Usually, that will be .cgi but sometimes
# .pl is required. If in doubt, try MasterPreInstallationTester.cgi first.
#
# Step-by-step installation instructions:
#
# (1) Ensure the first line of this file points toward your Perl 5+ program.
#     Leave the "#!" at the beginning of the line.
#
# (2) Upload MasterPreInstallationTester.cgi into a directory allowed to run CGI programs.
#
# (3) Set file execution permission to MasterPreInstallationTester.cgi. 
#
#     If you're setting execution permission with your FTP program, the settings 
#     are:
#
#            owner -- read/write/execute
#            group -- read/execute
#            world -- read/execute
#            (some FTP programs use "other" instead of "world")
#
#     If you're setting execution permission with Telnet, type:
#
#            chmod 0755 MasterPreInstallationTester.cgi
#
# To use Master Pre-Installation Tester just type its URL into your browser.
# Example: http://www.yourdomain.com/cgi-bin/MasterPreInstallationTester.cgi
#
#===============================
#
# No other customization needed.
#
#===============================

require 5;
$ME = $0;
$ME =~ s/.*\/(.*?)/$1/;
%R = (nflag => '', yflag => ' CHECKED');
@SNDtry = qw(
	/usr/sbin/sendmail
	/sbin/sendmail
	/usr/bin/sendmail
	/bin/sendmail
	/usr/lib/sendmail
	/lib/sendmail
	/usr/slib/sendmail
	/slib/sendmail
	/usr/sendmail
	/sendmail
	sendmail
);
sub Exit { goto THE_END; }

sub ValidEmail
{
	return 0 if $_[0] =~ /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/;
	return 0 if $_[0] !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/;
	return 1;
} # sub ValidEmail

sub PrintSpace { print '<p></p>'; }

sub PageTop
{
	print "Content-type: text/html\n\n";
	print <<EOT;
<html><head>
<TITLE>Master Pre-Installation Tester</TITLE>
<script type="text/javascript" language="JavaScript"><!-- 
function H(F) {
var properties = 'height=200,width=400,scrollbars=yes,resizable=yes';
var url = '$ME\?h=' + F;
helpwindow = window.open(url,'h1',properties);
}// -->
</script>
</head><body text="#000000" bgcolor="#CCCCCC" link="#003399" vlink="990000">
<div align="center">
<table width="500" border="0" cellpadding="0" cellspacing="0"><tr><td align="center">
<font size="+1"><b><br><a href="http://www.willmaster.com/master/">Master 
Pre-Installation Tester</a></b></font><br>&nbsp;
<p><form method="POST" action="$ME">
EOT
	PrintSpace;
} # PageTop


sub PageBottom { print '</form>Copyright 2000 <a href="http://willmaster.com/">William Bontrager.</a></td></tr></table></div></body></html>'; Exit; }

sub PrintMainMenu
{
	print <<EOT;
<table width="100%" bgcolor="#FFFFFF" border="0" cellpadding="18" cellspacing="0"><tr><td>
<table width="100%" border="1" cellpadding="18" cellspacing="0"><tr><td align="center">
<font size="+1">Main Menu</font>
</td></tr><tr><td align="center">
<noscript>
<table border="0" cellpadding="0" cellspacing="0"><tr>
<td align="right" valign="top"><font size="+1">NOTE:&nbsp;</font></td><td><b>A 
JavaScript enabled browser is required to use the [?] help 
links.</b></td></tr></table>
<hr>
</noscript>
<table border="0" cellpadding="3" cellspacing="0"><tr>
<td align="right" valign="top"><input type="checkbox" name="env" value="yes"> 
<font size="-1"><b><a href="javascript:H('env')">[?]</a></b></font></td>
<td>Report environment variables.</td>
</tr><tr>
<td align="right" valign="top"><input type="checkbox" name="ver" value="yes"> 
<font size="-1"><b><a href="javascript:H('ver')">[?]</a></b></font></td>
<td>Report Perl version.</td>
</tr><tr>
<td align="right" valign="top"><input type="checkbox" name="cwd" value="yes"> 
<font size="-1"><b><a href="javascript:H('h_cwd')">[?]</a></b></font></td>
<td>Report current directory.</td>
</tr><tr>
<td align="right" valign="top"><input type="checkbox" name="snd" value="yes"> 
<font size="-1"><b><a href="javascript:H('snd')">[?]</a></b></font></td>
<td>Report location of sendmail.</td>
</tr><tr>
<td align="right" valign="top"><input type="checkbox" name="inc" value="yes"> 
<font size="-1"><b><a href="javascript:H('inc')">[?]</a></b></font></td>
<td>Report directories included in module search.</td>
</tr><tr>
<td align="right" valign="top"><input type="checkbox" name="soc" value="yes"> 
<font size="-1"><b><a href="javascript:H('soc')">[?]</a></b></font></td>
<td>Report whether or not module
<br>Socket is installed.</td>
</tr><tr>
<td align="right" valign="top"><input type="checkbox" name="smp" value="yes"> 
<font size="-1"><b><a href="javascript:H('smp')">[?]</a></b></font></td>
<td>Report whether or not module
<br>LWP<b>::</b>Simple is installed.</td>
</tr><tr>
<td align="right" valign="top"><input type="checkbox"> 
<font size="-1"><b><a href="javascript:H('custom')">[?]</a></b></font></td>
<td>Report whether or not module
<br><input type="text" name="module" size="13"> is installed.</td>
</tr></table>

</td></tr><tr><td align="center">
<input type="submit" value="          Do  It          ">
</td></tr></table>
</td></tr></table>
EOT
} # sub PrintMainMenu

sub Parse
{
	my $buffer;
	if ($ENV{REQUEST_METHOD} eq 'GET') { $buffer = $ENV{QUERY_STRING}; }
	else { read(STDIN,$buffer,$ENV{CONTENT_LENGTH}); }
	my @p = split /&/,$buffer;
	foreach(@p)
	{
		my ($n,$v) = split(/=/,$_,2);
		$n =~ tr/+/ /;
		$v =~ tr/+/ /;
		$v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
		$v =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
		$In{$n} = $v;
	}
} # sub Parse

sub PresentEnv
{
	print <<EOT;
<table width="100%" bgcolor="#FFFFFF" border="0" cellpadding="18" cellspacing="0"><tr><td>
<table width="100%" border="1" cellpadding="18" cellspacing="0"><tr><td>
<font size="+1">Environment Variables</font>
</td></tr><tr><td>
<table width="100%" border="0" cellpadding="3" cellspacing="0">
EOT
	for(sort keys %ENV)
	{
		print <<EOT;
<tr>
<td align="right" valign="top">$_</td>
<td>$ENV{$_}</td>
</tr>
EOT
	}
	print '</table></td></tr></table></td></tr></table>';
	PrintSpace
} # PresentEnv

sub PresentVer
{
	print <<EOT;
<table width="100%" bgcolor="#FFFFFF" border="0" cellpadding="18" cellspacing="0"><tr><td>
<table width="100%" border="1" cellpadding="18" cellspacing="0"><tr><td>
<font size="+1">Perl Version</font> 
</td></tr><tr><td>
$]
</td></tr></table>
</td></tr></table>
EOT
	PrintSpace
} # PresentVer

sub PresentInc
{
	print <<EOT;
<table width="100%" bgcolor="#FFFFFF" border="0" cellpadding="18" cellspacing="0"><tr><td>
<table width="100%" border="1" cellpadding="18" cellspacing="0"><tr><td>
<font size="+1">Module Search Include Directories</font>
</td></tr><tr><td>
EOT
	my $break = '';
	for(@INC) { print "\n$break$_"; $break = '<br>'; }
	print '</td></tr></table></td></tr></table>';
	PrintSpace
} # PresentInc

sub PresentSnd
{
	print <<EOT;
<table width="100%" bgcolor="#FFFFFF" border="0" cellpadding="18" cellspacing="0"><tr><td>
<table width="100%" border="1" cellpadding="18" cellspacing="0"><tr><td>
<font size="+1">Location of Sendmail</font>
</td></tr><tr><td>
EOT
	my $break = '';
	for(@SNDtry) { if(-e $_) { print "\n$break$_"; $break = '<br>'; } }
	unless($break) { print <<EOT;
No sendmail was found on the server. That does not mean you are without 
sendmail, it just means that I didn't find it.
EOT
	}
	print '</td></tr></table></td></tr></table>';
	PrintSpace
} # PresentSnd

sub PresentCwd
{
	my $dir = <<EOT;
Unable to obtain current directory with certainty (module 
Cwd is not available). You may be able to obtain the current 
directory from the Environment Variables report.
EOT
	if(eval("require Cwd")) { use Cwd; $dir = cwd(); }
	print <<EOT;
<table width="100%" bgcolor="#FFFFFF" border="0" cellpadding="18" cellspacing="0"><tr><td>
<table width="100%" border="1" cellpadding="18" cellspacing="0"><tr><td>
<font size="+1">Current Directory</font>
</td></tr><tr><td>
$dir
</td></tr></table>
</td></tr></table>
EOT
	PrintSpace
} # PresentCwd

sub PresentMod
{
	my $r = "Sorry, I can not find Perl module $_[0] on this server.";
	$r = "Yes! Perl module $_[0] is installed on this server." if eval("require $_[0]");
	print <<EOT;
<table width="100%" bgcolor="#FFFFFF" border="0" cellpadding="18" cellspacing="0"><tr><td>
<table width="100%" border="1" cellpadding="18" cellspacing="0"><tr><td>
<font size="+1">Module $_[0] Availability</font>
</td></tr><tr><td>
$r
</td></tr></table>
</td></tr></table>
EOT
	PrintSpace
} # PresentMod

sub env
{
return <<SUBenv;
This provides a list of what your installation of Perl knows about itself, you, and the 
server it is on.
<p>
Among other things, the data can be useful to determine the server paths of 
public and other directories.
</p>
SUBenv
} # sub env

sub ver
{
return <<SUBver;
This reports the version of Perl you are using.
SUBver
} # sub ver

sub h_cwd
{
return <<SUBh_cwd;
This tells you what directory you are currently using.
SUBh_cwd
} # sub h_cwd

sub snd
{
return <<SUBsnd;
This causes Master Pre-Installation Tester to look for your server's 
installation of sendmail. It then reports its findings.
SUBsnd
} # sub snd

sub inc
{
return <<SUBinc;
This causes Master Pre-Installation Tester to print the list of directories 
where Perl looks for modules that your scripts use.
SUBinc
} # sub inc

sub soc
{
return <<SUBsoc;
This reports whether or not the module &quot;Socket&quot; is installed 
on your server.
<p>
Socket is a module often used by scripts.
</p>
SUBsoc
} # sub soc

sub smp
{
return <<SUBsmp;
This reports whether or not the module &quot;LWP<b>::</b>Simple&quot; is installed 
on your server.
<p>
LWP<b>::</b>Simple is a module often used by scripts.
</p>
SUBsmp
} # sub smp

sub custom
{
return <<SUBcustom;
Use this to determine whether or not a specific module is installed on 
you server.
<p>
Just type the module's name in the space provided. The module name is case 
sensitive.
</p>
SUBcustom
} # sub custom

sub ProvideHelp
{
	my $h = eval("$In{h}");
	print "Content-type: text/html\n\n";
	print <<EOT;
<html><body bgcolor="white" onLoad="self.focus()" onBlur="self.close()">
<table border="0" cellpadding="0" cellspacing="0"  width="100%" height="100%"><tr>
<td valign="middle" height="100%">$h
<div align="center"><p>
<form><input type="button" value="Close Window" onClick="self.close()"></form>
</p></div>
</td></tr></table></body></html>
EOT
} # sub ProvideHelp

Parse;
if($In{h}) { ProvideHelp; Exit; }
if($In{flag} eq 'yes') { $R{yflag} = ' CHECKED'; $R{nflag} = ''; }
else { $R{yflag} = ''; $R{nflag} = ' CHECKED'; }
PageTop;
PresentEnv if $In{env};
PresentVer if $In{ver};
PresentCwd if $In{cwd};
PresentSnd if $In{snd};
PresentInc if $In{inc};
PresentMod('Socket') if $In{soc};
PresentMod('LWP::Simple') if $In{smp};
PresentMod($In{module}) if $In{module};
PrintMainMenu;
PageBottom;
THE_END:
