##############################################################
# This script was wrtitten for Debian.
#
# It is here to provide some checks for better communication
# with the user.
# 
##############################################################

use strict;
use warnings;

# print the error message in html, 
# after a valid HTTP header.
# Then exit with error code 1.
sub error_html($)
{
	my $message = shift;
	my $cgi = $ENV{SCRIPT_NAME};
	
	print "Content-type: text/html\n\n";
	print <<EOF;
<html>
	<head>
	</head>
	
	<body>
	<h1>An error occurs in $cgi</h1>

	<pre>
	$message
	</pre>
	
	</body>
</html>
EOF

	exit 1;
}

# print the error message 
# on STDERR and then exit with
# error code 1.
sub error_stderr($)
{
	my $message = shift;
	warn $message;
	exit 1;
}

# Determine which error_ function to use
# according to the environement.
sub error($)
{
	my $message = shift;
	
	# if we are called from a browser.
	if (defined $ENV{SERVER_NAME} and 
	    defined $ENV{HTTP_HOST} and
	    defined $ENV{SERVER_PROTOCOL}) {
		error_html($message);
	}
	
	# else we are in a shell
	else {
		error_stderr($message);
	}
}


# Currently the only job to do is to 
# be sure that localconfig is redable.

my $localconfig = '/etc/bugzilla/localconfig';
error "File $localconfig is not readable.\n$0 cannot run properly.\n" 
	unless -r $localconfig;

