#!/usr/local/bin/perl5 -w

###########################################################################
### $Id:$
###########################################################################
###
###########################################################################

use FindBin '$RealBin';                             # locate libraries
BEGIN { unshift(@INC,"$RealBin/../lib") }

use strict;
use CGI;
use Mail::Field::AddrList;
use wham;

my( $cgi, $cmd, $line, $class, $count, $mid, $whoami, $frm, $f, %config, $list);

open(STDERR, ">&STDOUT");
$|=1;
$cgi=new CGI;

chomp($whoami=`whoami`);

### Set up the HTML
print $cgi->header;
print <<EOF ;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Wham! Administration for $whoami</title>
<style type='text/css'>
th              {       background: #999999;}
td		{	vertical-align: top;}
td.a            {       background: #aaaaaa;}
td.b            {       background: #cccccc;}
A:link          {       color: red }
A:visited       {       color: maroon }
A:active        {       color: blue }
</style>
</head>
<body>
<center><h1>Wham! Administration for $whoami</h1></center>
<center>
EOF

### Quick security check 
$ENV{REMOTE_USER} = "" if( !defined( $ENV{REMOTE_USER}));
if( $whoami ne $ENV{REMOTE_USER}) {
	print "<table cellpadding=0 cellspacing=0 border=0><tr><td bgcolor='#000000'>\n";
	print "<table cellpadding=2 cellspacing=1 border=0><tr><td class='b'>\n";
	print "<font color='#ff0000'>&nbsp;<p>ERROR: you do not appear to have authenticated correctly '$whoami' ne '$ENV{REMOTE_USER}'<p>&nbsp;</font>\n";
	print "</td></tr></table>\n";
	print "</td></tr></table>\n";
# 	exit;
}

%config = &LoadConfig();


print "<table cellpadding=0 cellspacing=0 border=0><tr><td bgcolor='#000000'>\n";
print "<table cellpadding=2 cellspacing=1 border=0>\n";
foreach $list ("black", "white")
{
#	print "<font color='ffffff'>)))" , $cgi->param('save');
	if( defined( $cgi->param('save')) && $cgi->param('save') eq $list)
	{
		### Try to save changes
		if( 0==0) {
			&ShowList( $list);
		} else {
			### Some error occured!
			&EditList( $list);
		}
	} elsif( defined( $cgi->param('edit')) && $cgi->param('edit') eq $list)
	{
		&EditList( $list);
	} else {
		&ShowList( $list);
	}
}
print "</table>\n";
print "</td></tr></table>\n";


###########################################################################
###
###########################################################################
###
###########################################################################
sub ShowList
{
	my( $list) = @_;
	print "<tr><th>",
		$cgi->start_form, $cgi->hidden(-name=>'edit',-value=>$list),
		" ${list}list ", 
		$cgi->submit(-name=>"edit"),$cgi->end_form,
		"</th></tr>\n";
	print "<tr><td class='b'><pre>\n";
#	print "<textarea cols=80 rows=20>\n";
	open( LIST, $config{$list}) || die "ERROR: could not open $config{$list}\n";
	while( defined( $line = <LIST>))
	{
		$line =~ s/(#.*)/<font color='#008800'>$1<\/font>/;
		print $line;
	}
#	print "</textarea>\n";
	print "</pre></td></tr>\n";
}

###########################################################################
### &EditList( "black");
###########################################################################
###
###########################################################################
sub EditList
{
	my( $list) = @_;

	print "<tr><th>",
		" ${list}list ", 
		"</th></tr>\n";
	print "<tr><td class='b'>",
		$cgi->start_form, $cgi->hidden(-name=>'save',-value=>$list);
	print "&nbsp;<br>&nbsp;<textarea cols=80 rows=20>\n";
	open( LIST, $config{$list}) || die "ERROR: could not open $config{$list}\n";
	while( defined( $line = <LIST>))
	{
		print $line;
	}
	print "</textarea>&nbsp;<br>",
		$cgi->submit(-name=>"submit", -value=>"save changes"),
		"&nbsp;<br>\n",
		$cgi->end_form;
	print "</td></tr>\n";
}
