#!/usr/bin/perl
# The aim of this PerlScript is to help connecting a VisualWorks
# Webserver via cgi2vw to an Apache Server. A crucial point
# is the accessibility of the file cgi2vw.ini. To get acccess
# to this file, VisualWorks needs an EnvironmentVariable called
# SystemRoot to be set to a path leading to the directory
# VisualWave/ containing the .ini file.
# Because no debugging from VisualWorks side is possible if
# the access fails, this script should help to get some info
# about the EnvironmentVariables transmitted to a cgi relay
# and thus are available within a script. At least the access
# to the file cgi2vw.ini is tested.
# To use this script, copy it into the directory where your
# cgi2vw resides, for example /srv/cgi-bin. Make it executable
# for an appropriate user. And than from a browser call
# http://MyHostName/cgi-bin/reportENV.pl
#---------------------------------------------------------
#                     DieFileNotFound
#---------------------------------------------------------
# Returnes an error message if the file '$FileName' is
# inaccessible.
#---------------------------------------------------------
sub DieWhenFileNotFound{
  local($FileName) = @_;
  #returns error message if a file could not be opened
  print <<EOF;
<head>
<title>Error Message - Could not open File</title>
<h1>File IO Error</h1>
</head>
<body BGCOLOR="#ffffff" LINK="#008000" ALINK="#008000" VLINK="#fb8000">
 <em>$FileName</em> could not be opened for reading.
</body>
EOF
#  die "\n";
}


#----------------------------------------------------------
#                 PrintEnv
#----------------------------------------------------------
#----------------------------------------------------------
# Print out the complete Environment Array
#----------------------------------------------------------
sub PrintEnv{
  

foreach $P (keys %ENV)
{
	print "$P";
	print " : ";
	print "$ENV{$P}";
	print "<br>";
}

}
#-------------------------------------------------------
# 
#-------------------  MAIN -----------------------------
#-- Mime header for ncsa httpd 1.2
print "Content-type: text/html;Charset=iso-8859-1\n";
print "\n";
print "<h2>Environment Test for VisualWorks cgi2vw Relay</h2><br>";
$SR = $ENV{SystemRoot};
$CGI_InfFileName = "$SR/VisualWave/cgi2vw.ini";
print "To use the VisualWorks cgi relay it is essential that the cgi relay will have read access to the following file while running under the web server:<br>";
print "<b>\$SYSTEMROOT</b>/VisualWave/cgi2vw.ini<br><br>";

print "First, we test whether the cgi2vw.ini file is readable from within the serving process:\n";
print "<hr>";
if (open(FH1,$CGI_InfFileName)) 
  {print "<br><big>The file <i><b>cgi2vw</b></i> is readable!</big><br><br>";}
else 
  {&DieWhenFileNotFound($CGI_InfFileName);}
print "<hr>";
print "If the above test succeeded, then your relay is probably working. ";
print "If not, then you need to check the path $SYSTEMROOT/VisualWave/cgi2vw.ini, which is hard coded in the cgi2vw relay. The variable";
print " <b>SYSTEMROOT</b> is an environment variable  which must be set in the environment in which the cgi program is running. <br><br>";
print "If <b>SYSTEMROOT</b> is empty or does not lead to a directory including \'VisualWave/\' the relay will not work.<br>";
print "The <b>USER</b> variable is also of interest because it determines file permissions. If the file is present, but cannot ";
print "be read by the executable, then the relay will fail.<br><br>";
print "<b>SYSTEMROOT</b> is actually set to:  <b>$ENV{SystemRoot}</b><br>";
print "<b>USER</b> is actually set to: <b>$ENV{USER}</b><br><br>";
print "If an EnvironmentVariable is missing, it may be that the variable is not set (this is particularly likely for <b>SYSTEMROOT</b>). ";
print "However, it may also be that it is set in the general environment, but the web server is not passing it to the script. ";
print "In Apache 2.x it is necessary to direct Apache to pass the variable to the cgi script. The directive is: <i>PassEnv Variablename </i>, for example: <i>PassEnv SystemRoot </i>. This can be done in 'httpd.conf.local' for example.<br><br>";
print "Here is the complete but unsorted set of environment variables available to the cgi program: <br><br>";
&PrintEnv();
print "<br>";


exit 0;
