#!/usr/local/bin/perl5 -w
#	Read Parameter Set Values
#
require "cgi-lib.pl";
#
#	Get these from cgiaddon file
@formnamelist = (
 'setname',
 'hostfilename',
 'bgfile',
 'bgfile2',
 'textbgfile',
 'textbgfile2',
 'appletname',
 'appletpname',
 'appletpvalue',
 'fontsize',
 'tablefontsize',
 'intablefontsize',
 'titlefontsize',
 'bgcolor',
 'titlecolor',
 'tabletitlecolor',
 'intablefontcolor',
 'intablebgcolor',
 'fgcolor',
 'tablefontcolor',
 'fontcolor',
 'buttoncolor',
 'auxbgcolor',
 'auxfgcolor',
 'auxbuttoncolor',
 'imgtitlecolor',
 'imgfgcolor',
 'linkcolor',
 'vlinkcolor',
 'alinkcolor',
 'imglinkcolor',
 'imgvlinkcolor',
 'imgalinkcolor',
 'tablelinkcolor',
 'tablevlinkcolor',
 'tablealinkcolor',
 'horizimagewidth',
 'vertimagewidth',
 'titleoption',
 'textbodyoption',
 'audioraramoption',
 'timeoutvalue',
 'debug',
 );
$formdefaults{'setname'} = 'default';
$formdefaults{'hostfilename'} = 'hostdefault';
$formdefaults{'bgfile'} = 'embossed/SciTech.gif';
$formdefaults{'bgfile2'} = 'embossed/lighnpac.gif';
$formdefaults{'textbgfile'} = 'clouds/clouds.gif';
$formdefaults{'textbgfile2'} = 'textures/soft-grid.gif';
$formdefaults{'appletname'} = 'CrazyText.class,100%,75';
$formdefaults{'appletpname'} = '';
$formdefaults{'appletpvalue'} = '';
$formdefaults{'fontsize'} = '5';
$formdefaults{'tablefontsize'} = '5';
$formdefaults{'intablefontsize'} = '6';
$formdefaults{'titlefontsize'} = '8';
$formdefaults{'bgcolor'} = 'blue';
$formdefaults{'titlecolor'} = 'goldenrod';
$formdefaults{'tabletitlecolor'} = 'darkviolet';
$formdefaults{'intablefontcolor'} = 'black';
$formdefaults{'intablebgcolor'} = 'navajowhite';
$formdefaults{'fgcolor'} = 'white';
$formdefaults{'tablefontcolor'} = 'black';
$formdefaults{'fontcolor'} = 'black';
$formdefaults{'buttoncolor'} = 'salmon';
$formdefaults{'auxbgcolor'} = 'yellow';
$formdefaults{'auxfgcolor'} = 'red';
$formdefaults{'auxbuttoncolor'} = 'salmon';
$formdefaults{'imgtitlecolor'} = 'black';
$formdefaults{'imgfgcolor'} = 'black';
$formdefaults{'linkcolor'} = 'cyan';
$formdefaults{'vlinkcolor'} = 'magenta';
$formdefaults{'alinkcolor'} = 'red';
$formdefaults{'imglinkcolor'} = 'cyan';
$formdefaults{'imgvlinkcolor'} = 'magenta';
$formdefaults{'imgalinkcolor'} = 'red';
$formdefaults{'tablelinkcolor'} = 'darkgreen';
$formdefaults{'tablevlinkcolor'} = 'magenta';
$formdefaults{'tablealinkcolor'} = 'red';
$formdefaults{'horizimagewidth'} = '569';
$formdefaults{'vertimagewidth'} = '320';
$formdefaults{'titleoption'} = '0';
$formdefaults{'textbodyoption'} = '2';
$formdefaults{'audioraramoption'} = '1';
$formdefaults{'timeoutvalue'} = '50000';
$formdefaults{'debug'} = 'UN_SET';
#
$filetologto = '';
$statusofread =  &ReadParse(*formdata);
$userdir = 'defaultuser';
if( $statusofread && exists($formdata{"username"}) ) {
        $userdir = $formdata{"username"} if( defined($formdata{"username"}) );
}
$testuserdir = $userdir;
$testuserdir =~ tr/A-Z/a-z/;
if( ($testuserdir eq 'guest') || ($testuserdir =~ m%^anon%) )   {
        $userdir = 'defaultuser';
}
#
#	Base URL
$baseforparmstore = "/usr/local/archives/public/html/users/gcf/nobody/webwisdom/";
#
$today =`date`;
chomp($today);
#
$baseforparmstore .= $userdir .'/';
#
$filetologto = $baseforparmstore . 'hostdefaultlog';
$parmstoreerrorflag=0;
if(-e $filetologto )	{
	open(FILETOLOGTO,'>>'.$filetologto) || ($parmstoreerrorflag=1);
}
else	{
	open(FILETOLOGTO,'>'.$filetologto) || ($parmstoreerrorflag=1);
	print FILETOLOGTO "Log of Parameter Save Actions\n";
}
if( $parmstoreerrorflag == 1 )	{
	print "Location: http://boss.npac.syr.edu:8080/users/gcf/wisdom/ReadLogError.html\n\n";
	exit 1;
}
#
#
$fullfiletoreadfrom ='';
if( $statusofread )	{
	if( (exists($formdata{'hostfilename'})) && (defined($formdata{'hostfilename'})) )	{
		$filetoreadfrom = $formdata{'hostfilename'};
	}
	else	{
		$filetoreadfrom = 'hostdefault';
	}
	$fullfiletoreadfrom = $baseforparmstore . 'parmsets/' .  $filetoreadfrom;
	open(FILETOREADFROM,'<'.$fullfiletoreadfrom) || ($parmstoreerrorflag=1);
	if( $parmstoreerrorflag == 1 )       {
		&logerror();
		print "Location: http://boss.npac.syr.edu:8080/users/gcf/wisdom/ReadError.html\n\n";
		exit 1;
	}
	while( $line = <FILETOREADFROM> )	{
		chomp($line);
		if( $line =~ m%([^:]*):(.*)% )	{
			$name=$1;
			$value=$2;
			$name =~ tr/A-Z/a-z/;
			$parmvalues{$name}=$value;
			if(exists($formdefaults{$name}))	{
				$formdefaults{$name} = $value;
			}
		}
	}
	$formdefaults{'hostfilename'} = $filetoreadfrom;
	close(FILETOREADFROM);

        print <<EOF;
Content-Type: text/html

<html>
<head>
<title>Returned Parameter Values</title>
<script language="JavaScript">
<!--scriptbegin
function sethostfilevalues()     {
        var totalparmnumber = document.forms[0].elements.length;
        for( var i=0; i < totalparmnumber; i++)     {
                var whatwewant = document.forms[0].elements[i].value;
		var arg = document.forms[0].elements[i].name;
                top.bigboss.currentparameters[arg] = whatwewant;
        }
	totalhostfilenumber = top.bigboss.hostfilemenu.count;
        top.bigboss.hostfilemenu.menuselection =1;
        for( var i=0; i < totalhostfilenumber; i++)     {
                var whatwewant = top.bigboss.hostfilemenu.options[i+1];
                if( whatwewant == top.bigboss.currentparameters.hostfilename )
                        top.bigboss.hostfilemenu.menuselection = i+1;
        }

}
//scriptend-->
</script>
</head>
<script language="JavaScript">
<!--scriptbegin
document.writeln('<body background="' + top.bigboss.wisdomhomeurl + '../background/' + top.bigboss.currentparameters.bgfile + '"  >');
document.writeln('<font size=3 color="' + top.bigboss.currentparameters.imgfgcolor + '" >');
//scriptend-->
</script>
<H2>Parameter Values of UserName $userdir Read From File $filetoreadfrom</H2>
<form name="newparameterset" >
EOF
	foreach $name (@formnamelist)	{
		print $name,' <input name="',$name,'" type="text" value="',$formdefaults{$name},'" ><br>'."\n";
	}
	print <<EOF;
</form>
<script language="JavaScript">
<!--scriptbegin
sethostfilevalues();
//scriptend-->
</script>
</body>
</html>
EOF


	print FILETOLOGTO 'OK Read ',$today,' ',$fullfiletoreadfrom,' ',$ENV{'REMOTE_ADDR'},' ',$ENV{'REMOTE_HOST'},"\n";
}
else	{
	$parmstoreerrorflag=1;
	&logerror();
	close(FILETOLOGTO);
	print "Location: http://boss.npac.syr.edu:8080/users/gcf/wisdom/ReadError.html\n\n";
}
#
sub	logerror	{
#
	return unless $filetologto;
	print FILETOLOGTO 'Failed Read ',$today,' ',$ENV{'REMOTE_ADDR'},' ',$ENV{'REMOTE_HOST'},"\n";
#
}
1;

