# This is a -*- perl -*- package that is not directly runnable, it should
# be required from another script.

# {{{ RCS header

#
# $Header: /local/www/cgi-bin/RCS/cgi.pl,v 1.1 1994/10/09 21:46:27 ssb Exp $
#

# }}}
# {{{ Copyright notice

#
# © Copyright Stig Sæther Bakken 1994
# Redistribution and modification allowed as long as all copyrights and
# credits remain.
#

# }}}
# {{{ Brief documentation

#
# Please mail ideas, bug reports, missing countries and other feedback to
# me at <ADDRESS>Stig.Bakken@pvv.unit.no</ADDRESS>
#

#
# When you require this package, you'll get the following 
# global variables:
#
# @http_accept		what MIME types are accepted
# $path			program path (/bin:/usr/bin etc.)
# $server_software	what server/version we are being run by (eg. NCSA/1.2)
# $server_name		the internet name of the server host
# $gateway_interface	what gateway standard/version (eg. CGI/1.1)
# $server_protocol	what protocol/version the server speaks (eg. HTTP/1.0)
# $server_port		what TCP port the server is running on
# $request_method	how to fetch data for us.  Can be either "POST" or
#			"GET".  At least in Mosaic, "GET" is the default
#			method.  The difference between them is that with
#			"GET", all parameters are sent as a part of the URL,
#			but with "POST" they are sent though stdin.  Both
#			have their advantages and disadvantages.
# $path_info		??
# $path_translated	??
# $script_name		The server's path of the script (eg. /cgi-bin/test)
# $query_string		parameters to the script.  Use %args instead.
# $remote_host		host running the client
# $remote_addr		ip address of host running the client
# $remote_ident		usually not given.  the user running the client on
#			the remote host.  You should never use this for
#			authentication, it's not secure enough for that!
# $auth_type		what sort of authentication we have used, if anything
# $content_type		MIME type of output
# $content_length	length in characters of the output (not needed?)
# %args			an associate array with arguments given to the script.
#			You can access them like this:
#			if ($args{"type"} eq "unsigned") ...
#

#
# You will also get the following functions available:
# &init_countries	Has to be called if you want to use &country.
# &country		takes one argument; a hostname.
#			Returns with plain text what country that host is in.
#

#
# NOTE:
# If you don't know the content type of the output in the beginning and
# want to figure it out yourself, do
# $will_take_care_of_content_type = 1;
# before requiring this package.
#
# do
# $cgi_debug = 1;
# before requiring this package is you want some debug output.
#

# }}}

undef @http_accept;	# MIME types the client will accept
undef %args;		# an associative array with all script arguments

# Initialize stuff from the CGI arguments
# We just take it all, most of it is junk though
@http_accept       = split(", ", $ENV{"HTTP_ACCEPT"});
$document_root     = $ENV{"DOCUMENT_ROOT"};
$path              = $ENV{"PATH"};
$server_software   = $ENV{"SERVER_SOFTWARE"};
$server_name       = $ENV{"SERVER_NAME"};
$gateway_interface = $ENV{"GATEWAY_INTERFACE"};
$server_protocol   = $ENV{"SERVER_PROTOCOL"};
$server_port       = $ENV{"SERVER_PORT"};
$request_method    = $ENV{"REQUEST_METHOD"};
$path_info         = $ENV{"PATH_INFO"};
$path_translated   = $ENV{"PATH_TRANSLATED"};
$script_name       = $ENV{"SCRIPT_NAME"};
$remote_host       = $ENV{"REMOTE_HOST"};
$remote_addr       = $ENV{"REMOTE_ADDR"};
$remote_ident      = $ENV{"REMOTE_IDENT"};
$auth_type         = $ENV{"AUTH_TYPE"};
$content_type      = $ENV{"CONTENT_TYPE"};
$content_length    = $ENV{"CONTENT_LENGTH"};
$http_agent        = $ENV{"HTTP_USER_AGENT"};
$http_referer      = $ENV{"HTTP_REFERER"};

$html3_table_capable = 0;

if ($request_method eq "POST") {
    for ($i = 0; $i < $content_length; $i++) {
	$query_string .= getc;
    }
}
else {
    $query_string = $ENV{"QUERY_STRING"};
}

$content_type = "text/html" unless $content_type;

print "Content-type: $content_type\n\n"
    unless $will_take_care_of_content_type;

print "<PRE>" if $cgi_debug;
foreach (split("&", $query_string)) {
    ($name, @value) = split("=", $_);
    $value = join("=", @value);
    $value =~ s/\+/ /g;
    # Fix all %HH patterns (hex representations of characters)
    $value =~ s/%(..)/pack("c",hex($1))/ge;
    print "$name = \"$value\"\n" if $cgi_debug;
    $args{$name} = $value;
}
print "</PRE>" if $cgi_debug;

# hack to tell CGI scripts about clients that can handle html3 tables
if ($user_agent) {
    if ($user_agent =~ m=^Mozilla/1\.1=) {
	$html3_table_capable = 1;
    }
}

sub init_countries
{
$cntr{"ar"} = "Argentina";
$cntr{"au"} = "Australia";
$cntr{"at"} = "Austria";
$cntr{"bh"} = "Bahrain";
$cntr{"be"} = "Belgium";
$cntr{"br"} = "Brazil";
$cntr{"bg"} = "Bulgaria";
$cntr{"ca"} = "Canada";
$cntr{"cl"} = "Chile";
$cntr{"hr"} = "Croatia";
$cntr{"cz"} = "Czech Republic";
$cntr{"dk"} = "Denmark";
$cntr{"eg"} = "Egypt";
$cntr{"sv"} = "El Salvador";
$cntr{"er"} = "Eritrea";
$cntr{"fk"} = "Falkland Islands";
$cntr{"fi"} = "Finland";
$cntr{"fr"} = "France";
$cntr{"de"} = "Germany";
$cntr{"gi"} = "Gibraltar";
$cntr{"gr"} = "Greece";
$cntr{"hk"} = "Hong Kong";
$cntr{"hr"} = "Croatia";
$cntr{"hu"} = "Hungary";
$cntr{"is"} = "Iceland";
$cntr{"in"} = "India";
$cntr{"ie"} = "Ireland";
$cntr{"il"} = "Israel";
$cntr{"it"} = "Italy";
$cntr{"jp"} = "Japan";
$cntr{"kr"} = "South Korea";
$cntr{"lv"} = "Latvia";
$cntr{"lu"} = "Luxembourg";
$cntr{"mx"} = "Mexico";
$cntr{"ma"} = "Morocco";
$cntr{"nl"} = "Netherlands";
$cntr{"no"} = "Norway";
$cntr{"pl"} = "Poland";
$cntr{"ro"} = "Romania";
$cntr{"sg"} = "Singapore";
$cntr{"sk"} = "Slovakia";
$cntr{"si"} = "Slovenia";
$cntr{"za"} = "South Africa";
$cntr{"es"} = "Spain";
$cntr{"se"} = "Sweden";
$cntr{"ch"} = "Switzerland";
$cntr{"tw"} = "Taiwan";
$cntr{"th"} = "Thailand";
$cntr{"tr"} = "Turkey";
$cntr{"ua"} = "Ukraine";
$cntr{"ae"} = "United Arab Emirates";
$cntr{"uk"} = "United Kingdom";
$cntr{"gb"} = "United Kingdom";
$cntr{"us"} = "USA";
$cntr{"com"}= "Commercial";
$cntr{"edu"}= "USA Educational";
$cntr{"gov"}= "USA Government";
$cntr{"mil"}= "USA Military";
$cntr{"net"}= "The Network";
$cntr{"org"}= "Non-profit Organization";
$cntr{'su'} = 'Ex Soviet Union';
$cntr{'nz'} = 'New Zealand';
}

sub country
{
    ($host) = @_;
    ($isocode) = $host =~ m/\.([^\.]+)$/;
    return $cntr{"\l$isocode"};
}

sub mydate
{
    local($date);
    chop($date=`date`);
    $date =~ s/ MET DST//;
    return $date;
}

1;
