#!perl
#
# Copyright (c) 2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

BEGIN {
    @INC = ( '../../perl' );
}
use Cgi;
use Help;
use ES40lnf;
use Magnus;

@monthNames = ( 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
	        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' );

$| = 1;
print "Content-type: text/html\n\n";

print "<html><head><title>Virtual Servers</title></head>\n";
print '<body bgcolor="#C0C0C0" link="#0000EE" vlink="#551A8B" alink="#FF0000">', "\n";
&setReferer();
print "<form method=\"post\" action=\"virtual\">\n";
( $configDir = $ENV{'CONFIG_DIR'} ) =~ s/%s/$ENV{'SERVER_NAMES'}/;

$magnus = new Magnus( "${configDir}magnus.conf" )
    or die "Can't read magnus.conf: $!\n";
$file = $magnus->value( 'VirtualServerFile' );
( $fullFile = $configDir . $file ) =~ s/conf_bk/config/ if $file;

if ( $cgiVars{'cmd'} ) {
    &{$cgiVars{'cmd'}}( $file );
} else {	# initial screen
    if ( $file ) {
	&onOff( $file );
    } else {	# On/Off
	&onOff( 0 );
    }
}
print "</form>\n</body>\n</html>\n";

sub onOff {
    my	$default = shift;
    my	$ip;
    my	$docRoot;

    print "<table width=100% border>\n";
    print "<tr><th><h1>Virtual Servers</h1></th></tr>\n";
    print "</table>\n";
    print "<input type=\"hidden\" name=\"cmd\" value=\"changeMagnus\">\n";
    print "<table width=100%>\n";
    print "<tr><td>Use Virtual Servers:</td></tr>\n";
    print "<tr><td><input type=\"radio\" name=\"doVirtual\" value=\"1\"",
    $default ? ' checked' : '', "> ",
    "Yes</td><td>File Name: <input type=\"text\" name=\"file\" value=\"",
    $default ? $default : 'virtual.conf', "\"></td></tr>\n";
    print "<tr><td><input type=\"radio\" name=\"doVirtual\" value=\"0\"",
    $default ? '' : ' checked', "> ",
    "No</td></tr>\n";
    print "</table>\n";

    print "<table width=100% border>\n";
    print "<tr><th width=33%><input type=submit value=\"OK\"></th>\n";
    print "<th width=33%><input type=reset></th>\n";
    print "<th width=33%><input type=button value=\"Help\" onClick=\"";
    print &helpJavaScript();
    print "\"></th></tr>\n";
    print "</table>\n";

    if ( -f $fullFile ) {
	print "<input type=\"hidden\" name=\"selectIP\">\n";
	print "<h3>Warning: this file is not backed up, edit with care.</h3>\n";
	print "<h3>After changing virtual server file, use Apply button to affect server.</h3>\n";
	print "<table width=100% border>\n";
	open( F, $fullFile ) or die "Can't read $file: $!\n";
	print "<tr><td><input type=\"button\" value=\"Add Virtual Server\"",
	" onClick=\"form.cmd.value='add'; form.submit();\"></td>\n",
	"<td>IP: <input type=\"text\" name=\"ip\" size=15></td>\n",
	"<td>Doc Root: <input type=\"text\" name=\"docRoot\"></td>\n",
	"</tr>\n";
	while ( <F> ) {
	    ( $ip, $docRoot ) = split( /\s+/, $_ );
	    print "<tr><td><input type=\"button\" value=\"Edit\" ",
	    "onClick=\"form.cmd.value='edit'; form.selectIP.value='$ip'; form.submit();\"> ",
	    "<input type=\"button\" value=\"Remove\" onClick=\"",
	    "if ( confirm( 'Do you really want to remove this entry?' ) ) { form.cmd.value='remove'; form.selectIP.value='$ip'; form.submit(); }\">",
	    "</td>",
	    "<td>$ip</td><td>$docRoot</td></tr>\n";
	}
	close( F );
	print "</table>\n";
    }
}

sub changeMagnus {
    my	$fullFile;

    if ( $cgiVars{'doVirtual'} ) {
	$magnus->set( 'VirtualServerFile', $cgiVars{'file'} );
	&appendLog( "system magnus.conf: Set VirtualServerFile to $cgiVars{'file'}\n" );
	( $fullFile = "$configDir$cgiVars{'file'}" ) =~ s/conf_bk/config/;
	unless ( -f $fullFile ) {
	    open( F, ">$fullFile" ) or die "Can't create $fullFile: $!\n";
	    close( F );
	}
    } else {
	$magnus->deleteVar( 'VirtualServerFile' );
	&appendLog( "system magnus.conf: Stop using VirtualServerFile\n" );
    }
    $magnus->flush();
    print "<SCRIPT>\n";
    print "window.location='/$ENV{'SERVER_NAMES'}/bin/commit';\n";
    print "</SCRIPT>\n";
}

sub add {
    my	$file = shift;
    my	@data;

    open( F, $fullFile ) or die "Can't read $file: $!\n";
    while ( <F> ) {
	push( @data, $_ );
    }
    close( F );
    open( F, ">$fullFile" ) or die "Can't rewrite $file: $!\n";
    print F "$cgiVars{'ip'}\t$cgiVars{'docRoot'}\n", @data;
    close( F );
    &onOff( $file );
}

sub edit {
    my	$ip;
    my	$docRoot;

    print "<table width=100% border>\n";
    print "<tr><th><h1>Edit Virtual Server Entry</h1></th></tr>\n";
    print "</table>\n";
    print "<input type=\"hidden\" name=\"oldIP\" value=\"$cgiVars{'selectIP'}\">\n";
    print "<input type=\"hidden\" name=\"cmd\" value=\"completeEdit\">\n";
    print "<table>\n";
    open( F, $fullFile ) or die "Can't read $file: $!\n";
    while ( <F> ) {
	( $ip, $docRoot ) = split( /\s+/, $_ );
	if ( $ip eq $cgiVars{'selectIP'} ) {
	    print "<tr><td>IP:</td><td><input type=\"text\" size=15 name=\"newIP\" value=\"$ip\"></td></tr>\n",
	    "<tr><td>Doc Root:</td><td><input type=\"text\" size=40 name=\"docRoot\" value=\"$docRoot\"></td></tr>\n";
	    last;
	}
    }
    close( F );
    print "</table>\n";
    print "<table width=100% border>\n";
    print "<tr><th width=33%><input type=submit value=\"OK\"></th>\n";
    print "<th width=33%><input type=reset></th>\n";
    print "<th width=33%><input type=button value=\"Help\" onClick=\"";
    print &helpJavaScript();
    print "\"></th></tr>\n";
    print "</table>\n";
}

sub completeEdit {
    my	$file = shift;
    my	$ip;
    my	$docRoot;
    my	@data;

    open( F, $fullFile ) or die "Can't read $file: $!\n";
    while ( <F> ) {
	( $ip, $docRoot ) = split( /\s+/, $_ );
	if ( $ip eq $cgiVars{'oldIP'} ) {
	    push( @data, "$cgiVars{'newIP'}\t$cgiVars{'docRoot'}\n" );
	} else {
	    push( @data, $_ );
	}
    }
    close( F );
    open( F, ">$fullFile" ) or die "Can't rewrite $file: $!\n";
    print F @data;
    close( F );
    &onOff( $file );
}

sub remove {
    my	$file = shift;
    my	@data;
    my	$ip;
    my	$docRoot;

    open( F, $fullFile ) or die "Can't read $file: $!\n";
    while ( <F> ) {
	( $ip, $docRoot ) = split( /\s+/, $_ );
	push( @data, $_ ) unless $ip eq $cgiVars{'selectIP'};
    }
    close( F );
    open( F, ">$fullFile" ) or die "Can't rewrite $file: $!\n";
    print F @data;
    close( F );
    &onOff( $cgiVars{'file'} );
}

sub appendLog {
    my	$where = $ENV{'REMOTE_HOST'} ?
	$ENV{'REMOTE_HOST'} : $ENV{'REMOTE_ADDR'};
    my	$day, $year, $hour, $min, $sec, $mon;

    ( $sec, $min, $hour, $day, $mon, $year ) = localtime( time );
    open( LOG, ">>$configDir/commit" ) ||
	die "Can't log to $configDir/commit: $!\n";
    print LOG "$ENV{'REMOTE_USER'}@$where: $ENV{'SERVER_NAMES'} [$day/",
    $monthNames[$mon], "/$year:$hour:$min:$sec]\n  ", @_;
    close( LOG );
}

sub setReferer {
    print "<SCRIPT language=JAVASCRIPT>\n";
    print "document.cookie='adminReferer='+window.location.href+'; path=/";
    print $ENV{'SERVER_NAMES'}, "/';\n";
    print "</SCRIPT>\n";
}
