
$ENV{BACKGROUND} = "BGCOLOR='$ENV{SERVER_WWW_BACKGROUND}'";

#
# Set background color
#

$ENV{BACKGROUND}="BGCOLOR=\"$ENV{SERVER_WWW_BACKGROUND}\""; 

#
# Create directory structure
#

unless (-d  "$ENV{SERVER_WWW_HOME}") {
  print "$0: bad SERVER_WWW_HOME ($ENV{SERVER_WWW_HOME}): no such file or directory\n";
  exit(-1);
}
unless (-d "$ENV{SERVER_WWW_HOME}/solvers") {
  print "creating $ENV{SERVER_WWW_URL}/solvers\n"; 
  system("mkdir $ENV{SERVER_WWW_HOME}/solvers") && die; 
}
unless (-d "$ENV{SERVER_WWW_HOME}/images") {
  print "creating $ENV{SERVER_WWW_URL}/images\n"; 
  system("mkdir $ENV{SERVER_WWW_HOME}/images") && die "$0: $!"; 
}
system("cp $ENV{SERVER_LIB}/images/* $ENV{SERVER_WWW_HOME}/images/") && die;

unless (-d "$ENV{SERVER_WWW_HOME}/jobs") {
  print "creating $ENV{SERVER_WWW_URL}/jobs\n"; 
  system("mkdir $ENV{SERVER_WWW_HOME}/jobs") && die;
  system("chmod a+rwx $ENV{SERVER_WWW_HOME}/jobs") && die;
}
unless (-d "$ENV{SERVER_CGI_HOME}") {
  print "$0: bad SERVER_CGI_HOME ($ENV{SERVER_CGI_HOME}): no such file or directory\n";
  exit(-1);
}

#
# Web server needs to be able to write in this directory
#

unless (-d "$ENV{SERVER_VAR}/spool/WEB") {
  system("mkdir $ENV{SERVER_VAR}/spool/WEB") && die;
}
system("chmod a+rwx $ENV{SERVER_VAR}/spool/WEB") && die; 
system("chmod a+rwx $ENV{SERVER_VAR}/tmp") && die; 


#
# Homepage 
#

print "creating $ENV{SERVER_WWW_URL}/index.html\n";
&clobber("$ENV{SERVER_WWW_HOME}/index.html") && 
  system("$^X make-server-homepage.pl  > $ENV{SERVER_WWW_HOME}/index.html") && die "$0: can't create $ENV{SERVER_WWW_HOME}/index.html\n";

#
# help-justify page and responses
#

&clobber("$ENV{SERVER_WWW_HOME}/help-justify.html") &&  
  &justify_page("$ENV{SERVER_WWW_HOME}/help-justify.html");
sub justify_page {

    my $output          = $_[0];
    print "creating $output\n";
    
    my $TEMPLATE = `cat $ENV{SERVER_LIB}/html/help-justify.html`;
    $TEMPLATE =~ s/<SERVER_NAME>/$ENV{SERVER_NAME}/g;
    $TEMPLATE =~ s/<SERVER_COMMENTS>/$ENV{SERVER_COMMENTS}/g;
    $TEMPLATE =~ s/<BASE_HREF>/$ENV{SERVER_WWW_URL}\//g;
    $TEMPLATE =~ s/BGCOLOR/$ENV{BACKGROUND}/g;
    
    if (!($ENV{SERVER_FEEDBACK}))
    {
	
	$TEMPLATE =~ s/<!--SERVER_FEEDBACK-->/<!--SERVER_FEEDBACK/;
	$TEMPLATE =~ s/<!--END_SERVER_FEEDBACK-->/END_SERVER_FEEDBACK-->/;
    }
    
    open (OUT, ">$output") or die "$0: can't create $output: $!";
    print OUT $TEMPLATE;
    close OUT;
    1;
}

if ($ENV{SERVER_FEEDBACK}) {
    &clobber("$ENV{SERVER_WWW_HOME}/stories.html") && &create_page("$ENV{SERVER_WWW_HOME}/stories.html", "$ENV{SERVER_LIB}/html/stories.html");
}

#
# Solvers pages
#

print "creating $ENV{SERVER_WWW_URL}/server-solvers.html\n";
&clobber("$ENV{SERVER_WWW_HOME}/server-solvers.html") &&
  system("$^X make-server-solvers.pl > $ENV{SERVER_WWW_HOME}/server-solver-types.html") && die "$0: can't create $ENV{SERVER_WWW_HOME}/server-solvers.html\n";

print "creating $ENV{SERVER_WWW_URL}/server-solvers-in.html\n";
&clobber("$ENV{SERVER_WWW_HOME}/server-solvers-in.html") &&
  system("$^X make-server-solvers-in.pl > $ENV{SERVER_WWW_HOME}/server-solvers-in.html") && die "$0: can't create $ENV{SERVER_WWW_HOME}/server-solvers-in.html\n";

#
# Submission Tool Page
#

&clobber("$ENV{SERVER_WWW_HOME}/server-submit.html") && &create_page("$ENV{SERVER_WWW_HOME}/server-submit.html", "$ENV{SERVER_LIB}/html/server-submit.html");

#
# General comments 
#

&clobber("$ENV{SERVER_WWW_HOME}/server-comment.html") && &create_page("$ENV{SERVER_WWW_HOME}/server-comment.html", "$ENV{SERVER_LIB}/html/server-comment.html");

&clobber("$ENV{SERVER_CGI_HOME}/server-comment.cgi") && &mail_us("$ENV{SERVER_CGI_HOME}/server-comment.cgi", "$ENV{SERVER_LIB}/cgi/server-comment.cgi");
sub mail_us {
    my $output          = $_[0];
    my $input           = $_[1];
    print "creating $output\n";
    
    my $SERVER_COMMENTS=$ENV{SERVER_COMMENTS};
    $SERVER_COMMENTS =~ s/\@/\\\@/g;

    my $TEMPLATE = `cat $input`; 
    $TEMPLATE =~ s/<BASE_HREF>/$ENV{SERVER_WWW_URL}\//g;
    $TEMPLATE =~ s/BGCOLOR/$ENV{BACKGROUND}/g;
    $TEMPLATE =~ s/<SERVER_NAME>/$ENV{SERVER_NAME}/g;
    $TEMPLATE =~ s/<SERVER_COMMENTS>/$SERVER_COMMENTS/g;
    $TEMPLATE =~ s/<SERVER_EMAIL>/$ENV{SERVER_EMAIL}/g;
    $TEMPLATE =~ s/<SERVER_FTP_URL>/$ENV{SERVER_FTP_URL}/g;
    $TEMPLATE =~ s/<SERVER_CGI_URL>/$ENV{SERVER_CGI_URL}/g;
    $TEMPLATE =~ s/<SERVER_CGI_LIB>/$ENV{SERVER_CGI_LIB}/g;
    $TEMPLATE =~ s/<SERVER_VAR>/$ENV{SERVER_VAR}/g;
    $TEMPLATE =~ s/<WEB_PERL>/$ENV{SERVER_WEB_PERL}/g;

    open (OUT, ">$output") or die "$0: can't create $output: $!";
    print OUT $TEMPLATE;
    close OUT;
    1;
}
system("chmod +x $ENV{SERVER_CGI_HOME}/server-comment.cgi");

#
# neos-news mailing list requester 
#

&clobber("$ENV{SERVER_CGI_HOME}/list.cgi") && &create_page("$ENV{SERVER_CGI_HOME}/list.cgi", "$ENV{SERVER_LIB}/cgi/list.cgi");
system("chmod a+x $ENV{SERVER_CGI_HOME}/list.cgi");


#
# NEOS Server help requester
#

&clobber("$ENV{SERVER_CGI_HOME}/nph-server-help.cgi") && &create_page("$ENV{SERVER_CGI_HOME}/nph-server-help.cgi", "$ENV{SERVER_LIB}/cgi/nph-server-help.cgi");
system("chmod a+x $ENV{SERVER_CGI_HOME}/nph-server-help.cgi");

#
# Faq & docs pages
#

&clobber("$ENV{SERVER_WWW_HOME}/faq.html") && &create_page("$ENV{SERVER_WWW_HOME}/faq.html", "$ENV{SERVER_LIB}/html/faq.html");

&clobber("$ENV{SERVER_WWW_HOME}/admin_faq.html") && &create_page("$ENV{SERVER_WWW_HOME}/admin_faq.html", "$ENV{SERVER_LIB}/html/admin_faq.html");

&clobber("$ENV{SERVER_WWW_HOME}/neos_faq.html") && &create_page("$ENV{SERVER_WWW_HOME}/neos_faq.html", "$ENV{SERVER_LIB}/html/neos_faq.html");

&clobber("$ENV{SERVER_WWW_HOME}/collab.html") && &create_page("$ENV{SERVER_WWW_HOME}/collab.html", "$ENV{SERVER_LIB}/html/collab.html");

&clobber("$ENV{SERVER_WWW_HOME}/ackn.html") && &create_page("$ENV{SERVER_WWW_HOME}/ackn.html", "$ENV{SERVER_LIB}/html/ackn.html");


#
# Downloads of the Tcl/Tk submission tool
#

&clobber("$ENV{SERVER_WWW_HOME}/downloads.html") && &create_page("$ENV{SERVER_WWW_HOME}/downloads.html", "$ENV{SERVER_LIB}/html/downloads.html");

#
# Downloads of the JAVA submission tool
#

&clobber("$ENV{SERVER_WWW_HOME}/java-downloads.html") && &create_page("$ENV{SERVER_WWW_HOME}/java-downloads.html", "$ENV{SERVER_LIB}/html/java-downloads.html");

#
# cgi script for downloads of both submission tools
#

&clobber("$ENV{SERVER_CGI_HOME}/downloads.cgi") && &create_page("$ENV{SERVER_CGI_HOME}/downloads.cgi", "$ENV{SERVER_LIB}/cgi/downloads.cgi");
system("chmod +x $ENV{SERVER_CGI_HOME}/downloads.cgi");
system("touch $ENV{SERVER_VAR}/databases/submit_users");
system("chmod a+w $ENV{SERVER_VAR}/databases/submit_users");
system("touch $ENV{SERVER_VAR}/databases/java_users");
system("chmod a+w $ENV{SERVER_VAR}/databases/java_users");

#
# cgi scripts for checking job passwords
#

&clobber("$ENV{SERVER_CGI_HOME}/check-status.cgi") && &create_page("$ENV{SERVER_CGI_HOME}/check-status.cgi", "$ENV{SERVER_LIB}/cgi/check-status.cgi");
system("chmod +x $ENV{SERVER_CGI_HOME}/check-status.cgi");

&clobber("$ENV{SERVER_CGI_HOME}/check-pwd.cgi") && &create_page("$ENV{SERVER_CGI_HOME}/check-pwd.cgi", "$ENV{SERVER_LIB}/cgi/check-pwd.cgi");
system("chmod +x $ENV{SERVER_CGI_HOME}/check-pwd.cgi");

#
# Downloads of the NEOS Server
#

&clobber("$ENV{SERVER_WWW_HOME}/server-downloads.html") && &create_page("$ENV{SERVER_WWW_HOME}/server-downloads.html", "$ENV{SERVER_LIB}/html/server-downloads.html");

#
# cgi script for downloads of NEOS Server
#

if($ENV{SERVER_DISTRIBUTOR}) {	
    &clobber("$ENV{SERVER_CGI_HOME}/server-downloads.cgi") && &create_page("$ENV{SERVER_CGI_HOME}/server-downloads.cgi", "$ENV{SERVER_LIB}/cgi/server-downloads.cgi");
  system("chmod +x $ENV{SERVER_CGI_HOME}/server-downloads.cgi");
  system("touch $ENV{SERVER_VAR}/databases/server_users");
  system("chmod a+w $ENV{SERVER_VAR}/databases/server_users");
}

#
# CGI script for providing unique file names
#

print "creating $ENV{SERVER_CGI_HOME}/tempfile.cgi...\n";
&clobber("$ENV{SERVER_CGI_HOME}/tempfile.cgi") && &tmpnames;
sub tmpnames {
    $TEMPLATE = `cat $ENV{SERVER_LIB}/cgi/tempfile.cgi`;
    $TEMPLATE =~ s/<WEB_PERL>/$ENV{SERVER_WEB_PERL}/g;
    open(HANDLE, ">$ENV{SERVER_CGI_HOME}/tempfile.cgi") or die "$0 can't create $ENV{SERVER_CGI_HOME}/tempfile.cgi";
    print HANDLE $TEMPLATE;    
    close HANDLE;
    system("chmod +x $ENV{SERVER_CGI_HOME}/tempfile.cgi");
}


if($ENV{KESTREL_INSTALL}) {

#
# Kestrel information page
#

    &clobber("$ENV{SERVER_WWW_HOME}/kestrel.html") && &create_page("$ENV{SERVER_WWW_HOME}/kestrel.html", "$ENV{SERVER_LIB}/html/kestrel.html");


#
# Kestrel downloads
#

    &clobber("$ENV{SERVER_CGI_HOME}/kestrel-downloads.cgi") && &create_page("$ENV{SERVER_CGI_HOME}/kestrel-downloads.cgi", "$ENV{SERVER_LIB}/cgi/kestrel-downloads.cgi");
    system("chmod +x $ENV{SERVER_CGI_HOME}/kestrel-downloads.cgi");
    system("touch $ENV{SERVER_VAR}/databases/kestrel_users");
}

#
# Individual solver pages
#

$orig = $ENV{SERVER_SOLVERS};
chomp(@LIST = `cat $ENV{SERVER_VAR}/lib/solver_list`);
chomp(@LIST = (@LIST, `cat $ENV{SERVER_LIB}/admin_list`));
foreach (@LIST) {
  unless ($_ =~ /ADMIN:HELP$/ or $_ =~ /KESTREL_/i) {
    s/.*=(.*)/$1/; 
    if (/^ADMIN:.*$/) {
      $ENV{SERVER_SOLVERS} = $ENV{SERVER_ADMIN_SOLVERS};
    } else { 
      $ENV{SERVER_SOLVERS} = $orig; 
    }
    system("$^X make-solver.pl $_") && die;
  }
}

sub clobber {
  if ($ENV{NOCLOBBER} == 1) {
      print "not changing $_[0]\n";  
      return 0;
  }
  if ($ENV{CLOBBER} == 1) {
      return 1;
  }
  if (-f "$_[0]") {
    while(1) {
	print "$_[0] exists, overwrite? [y/n or a/0 (all/none)] --> ";
	$_ = <STDIN>; 
	return 1 if (/(y)/);
	return 0 if (/(n)/);
	if (/a/) {
	    $ENV{CLOBBER} = 1;
	    return 1;
	}
	if(/0/) {
	    $ENV{NOCLOBBER} = 1;
	    return 0;
	}
    }
  } else {
    return 1;
  } 
}

sub create_page {
    my $output          = $_[0];
    my $input           = $_[1];
    print "creating $output\n";
    
    my $TEMPLATE = `cat $input`; 
    $TEMPLATE =~ s/<BASE_HREF>/$ENV{SERVER_WWW_URL}\//g;
    $TEMPLATE =~ s/<BASE_DIR>/$ENV{SERVER_WWW_HOME}\//g;
    $TEMPLATE =~ s/BGCOLOR/$ENV{BACKGROUND}/g;
    $TEMPLATE =~ s/<SERVER_NAME>/$ENV{SERVER_NAME}/g;
    $TEMPLATE =~ s/<SERVER_VERSION>/$ENV{SERVER_VERSION}/g;
    $TEMPLATE =~ s/<SERVER_COMMENTS>/$ENV{SERVER_COMMENTS}/g;
    $TEMPLATE =~ s/<SERVER_EMAIL>/$ENV{SERVER_EMAIL}/g;
    $TEMPLATE =~ s/<SERVER_FTP_URL>/$ENV{SERVER_FTP_URL}/g;
    $TEMPLATE =~ s/<SERVER_CGI_URL>/$ENV{SERVER_CGI_URL}/g;
    $TEMPLATE =~ s/<SERVER_CGI_HOME>/$ENV{SERVER_CGI_HOME}/g;
    $TEMPLATE =~ s/<SERVER_CGI_LIB>/$ENV{SERVER_CGI_LIB}/g;
    $TEMPLATE =~ s/<SERVER_VAR>/$ENV{SERVER_VAR}/g;
    $TEMPLATE =~ s/<WEB_PERL>/$ENV{SERVER_WEB_PERL}/g;
    $TEMPLATE =~ s/<PERL>/$ENV{SERVER_PERL}/g;

    open (OUT, ">$output") or die "$0: can't create $output: $!";
    print OUT $TEMPLATE;
    close OUT;
    1;
}


