require "flush.pl";
system("clear");


#
# Read in saved configuration
#

if (-f "../server/lib/config-data.pl") {
  require "../server/lib/config-data.pl";
}

$ENV{SERVER_VERSION} = "4.0";

print "\nServer Interfaces: (0=no, 1=yes)\n\n"; 

print "Do you want WWW access to your server?\n\n";
&get_value(SERVER_WWW_READY, 0);
print "\nDo you want E-mail access to your server?\n\n";
&get_value(SERVER_EMAIL_READY, 0);
print "\nWould you like to customize your server?\n\n";
&get_value(SERVER_CUSTOMIZE, 0);
if ($ENV{SERVER_CUSTOMIZE}) {
    print "\nAre you installing the Kestrel Server along with this Server?\n\n";
    &get_value(KESTREL_INSTALL, 0);

    print "\nDo you want Analog analysis of your web server?\n\n(If so you are responsible for installing or \ngaining use of Analog on your web server as well as \ndesigning Analog output to meet your requirements.)\n\n";
    &get_value(SERVER_ANALOG, 0);

    print "\nWould you like to add a page of user feedback received?\n\n";
    &get_value(SERVER_FEEDBACK, 0);

    print "\nWould you like the Server to automatically distinguish\nsolver groups according to given features?\n\n";
    &get_value(SERVER_SOLVER_GROUPS, 0);
} else {
    $ENV{KESTREL_INSTALL}     = 0;
    $ENV{SERVER_ANALOG}      = 0;
    $ENV{SERVER_FEEDBACK}    = 0;
    $ENV{SERVER_SOLVER_GROUPS} = 0;
}

#
# Check for perl 5
#


print "\nServer Requirements:\n\n"; 

print "checking for perl5..."; 
$PERL = $ENV{SERVER_PERL};
chomp($PERL = `which $^X`) unless ($PERL);
$done = 0;
do {
  while (!((-f "$PERL") && (-x "$PERL") && ($PERL =~ /^\//))) {
    print "Enter complete path to perl5 --> ";
    chomp($PERL = <STDIN>);
  }
  $version = `$PERL -v`;
  if($version =~ /version (\S+)/) {
    $version = $1;
  } else {
    if($version =~ / v(\d+)/) {
      $version = $1;
    }
  }
  if ($version >= 5.0) {
    $done = 1;
  } else {
    print "\n$0: $PERL is perl version $version\n";
    $PERL = "";
  }
} while (!$done);
$ENV{SERVER_PERL} = $PERL;
print "ok\n"; 

#
# Check for gzip/tar/nserv/mh
#

print "checking for gzip..."; 
chomp($gzip   = `which gzip`); 
unless (-x "$gzip") {
  print "\ngzip must be in your path\n";
  exit(1);
} 
print "ok\n";

print "checking for tar..."; 
chomp($tar    = `which tar`); 
unless (-x "$tar") {
  print "\ntar must be in your path\n";
  exit(1);
}
print "ok\n";

print "checking for ping..."; 
chomp($ping = `which ping`); 
unless (-x "$ping") {
  print "\nping must be in your path\n";
  exit(1);
}
print "ok\n";

if ($ENV{SERVER_EMAIL_READY}) {
  print "checking for mh inc..."; 
  chomp($inc    = `which inc`); 
  unless (-x "$inc") {
    print "\ninc must be in your path\n";
    exit(1);
  }
  print "ok\n";
}

#
# Build environment 
#

print "\nServer Environment:\n\n"; 

print "Enter the absolute hostname of your machine (e.g., fate.mcs.anl.gov)\n\n";
do {
  &get_value(SERVER_HOSTNAME, $ENV{HOST});
} until ($ENV{SERVER_HOSTNAME} =~ /\./);

print "\nWhat user is installing the Server?\n\n";
&get_value(SERVER_USER, $ENV{USER});
$dir = $ENV{PWD};
$dir =~ s/\/config$//g;

print "\nWhat is the home of the Server?\n\n";
&get_value(SERVER_HOME, $dir);
$ENV{SERVER_BIN} = "$ENV{SERVER_HOME}/server/bin";
$ENV{SERVER_USER_BIN} = "$ENV{SERVER_HOME}/bin";
$ENV{SERVER_LIB} = "$ENV{SERVER_HOME}/server/lib";
$ENV{SERVER_ADMIN_SOLVERS} = "$ENV{SERVER_LIB}/admin_solvers";

print "\nWhere should variable-length files be placed?\n\n";
&get_value(SERVER_VAR, "$dir/server/var");

unless(-d "$ENV{SERVER_VAR}") { 
    print "!!Directory $ENV{SERVER_VAR} does not exist!!\n\n";  
    exit(1);
}

$ENV{SERVER_SOLVERS} = "$ENV{SERVER_VAR}/lib/solvers";
$ENV{SERVER_TMP} = "$ENV{SERVER_VAR}/tmp";
$ENV{SERVER_SPOOL} = "$ENV{SERVER_VAR}/spool";
$ENV{SERVER_JOBS} = "$ENV{SERVER_VAR}/jobs";

if(open(MAKE, "$ENV{SERVER_HOME}/config/Makefile.server")) {
    @make = <MAKE>;
    close MAKE;
    if(open(MAKE, ">$ENV{SERVER_HOME}/config/Makefile")) {
	foreach $bit (@make) {
	    $bit =~ s/<SERVER_VAR>/$ENV{SERVER_VAR}/;
	    print MAKE $bit;
	}
	close MAKE;
    }
}

print "\nWho is the Server administrator?\n\n";
&get_value(SERVER_ADMIN, "nobody\@$ENV{SERVER_HOSTNAME}"); 

print "\nWhere should user comments be sent?\n\n";
&get_value(SERVER_COMMENTS, $ENV{SERVER_ADMIN}); 

print "\nWhat is the name of your Server?\n\n";
&get_value(SERVER_NAME, "Test");

print "\nWhat is the port number for the Server?\n\n";
&get_value(SERVER_PORT, 3333); 

if ($ENV{SERVER_EMAIL_READY}) {
  print "\nWhat is the email address for email submissions?\n\n";
  &get_value(SERVER_EMAIL, "$ENV{SERVER_USER}\@$ENV{SERVER_HOSTNAME}");
  @email_base = split /@/, $ENV{SERVER_EMAIL};

  print "\nWhat is the name of your system POP server?\n\n";
  &get_value(SERVER_POP_SERVER, "pop.$email_base[1]");
}

if ($ENV{SERVER_WWW_READY}) {
  print "\nWhere should the Server web pages be placed?\n\n";
  &get_value(SERVER_WWW_HOME, "/home/www/$ENV{SERVER_USER}");
 
 print "\nWhat is the base URL to your web area?\n\n";
  &get_value(SERVER_WWW_URL,  "http://$ENV{SERVER_HOSTNAME}/home/$ENV{SERVER_USER}");

  print "\nWhere should the Server cgi scripts be placed?\n\n";
  &get_value(SERVER_CGI_HOME, "/home/httpd/cgi-bin/$ENV{SERVER_USER}");

  print "\nWhat is the base URL to the cgi area?\n\n";
  &get_value(SERVER_CGI_URL,  "http://$ENV{SERVER_HOSTNAME}/cgi-bin/$ENV{SERVER_USER}"); 

  system("cp $ENV{SERVER_BIN}/cgi-lib.pl $ENV{SERVER_CGI_HOME}/");
  $ENV{SERVER_CGI_LIB} = "$ENV{SERVER_CGI_HOME}/cgi-lib.pl";

  print "\nWhat is the absolute path to perl on your Web server machine?\n(if different from your NEOS Server machine)\n\n";
  &get_value(SERVER_WEB_PERL, "$PERL");

  print "\nSelect the background color for the WWW pages\n\n";
  &get_value(SERVER_WWW_BACKGROUND, "#ffffff");
  $ENV{BACKGROUND} = "BGCOLOR='$ENV{SERVER_WWW_BACKGROUND}'";
}

    
if ($ENV{SERVER_ANALOG}) {
    print "\nGive the absolute path to your Analog directory.\n\n";
    &get_value(SERVER_ANALOG_HOME, "");
    
    if(!(-x "$ENV{SERVER_ANALOG_HOME}"))
    {
	die "\nAnalog home directory $ENV{SERVER_ANALOG_HOME} cannot be fully accessed by this user.\n";
    }
}
if ($ENV{KESTREL_INSTALL}) {
    print "\nWhere is your Kestrel home directory?\n\n";
    &get_value(KESTREL_HOME, "");
    
    print "\nWhere is your Kestrel directory containing state information?\n\n";
    &get_value(KESTREL_STATE, "");
    
    print "\nWhat version does your Kestrel Server support for AMPL?\n\n";
    &get_value(KESTREL_VERSION_AMPL, "Kestrel/Version0.1");

    print "\nWhat version does your Kestrel Server support for GAMS?\n\n";
    &get_value(KESTREL_VERSION_GAMS, "Kestrel/GAMS0.2");
}			    



print "\nWhere are the downloadable user interfaces (client tools) located?:\n\n";
&get_value(SERVER_FTP_URL,  "ftp://ftp.mcs.anl.gov/pub/neos/Server");

print "\nDo you plan to distribute the NEOS Server\nsoftware from that directory?\n(Are you authorized to do so?):\n\n";
&get_value(SERVER_DISTRIBUTOR, 0);

#
# Get absolute path to perl,
#

if ($^X =~ /^\//) {
  # absolute path
  $perl = $^X; 
} elsif ($^X =~ /\//) {
  # relative path
  $^X =~ /(.+\/)+(.+)/;
  $perl = "$ENV{PWD}/$2";
} else {
  # no path
  chop($perl = `which $^X`);
  if ($perl =~ /^\//) {
  } else {
    $perl =~ /(.+\/)+(.+)/;
    $perl = "$ENV{PWD}/$2";
  }
}

#
# Save environment for later configurations
#

open(HANDLE, ">../server/lib/config-data.pl");
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    $value = $ENV{$_};
    $value =~ s/\@/\\\@/g;
    print HANDLE "\$ENV{$_} = \"$value\";\n";
  }
}
print HANDLE "1;\n";
close(HANDLE);

print "\nBuilding Server:\n\n"; 

#
# Build SERVER_VAR directory structure
#

unless(-d "$ENV{SERVER_VAR}/databases") { mkdir "$ENV{SERVER_VAR}/databases", 0755;}
unless(-d "$ENV{SERVER_VAR}/jobs") { mkdir "$ENV{SERVER_VAR}/jobs", 0755;}
unless(-d "$ENV{SERVER_VAR}/lib") { mkdir "$ENV{SERVER_VAR}/lib", 0755;}
unless(-d "$ENV{SERVER_VAR}/logs") { mkdir "$ENV{SERVER_VAR}/logs", 0755;}
unless(-d "$ENV{SERVER_VAR}/proc") { mkdir "$ENV{SERVER_VAR}/proc", 0755;}
unless(-d "$ENV{SERVER_VAR}/spool") { mkdir "$ENV{SERVER_VAR}/spool", 0755;}
unless(-d "$ENV{SERVER_VAR}/tmp") { mkdir "$ENV{SERVER_VAR}/tmp", 0755;}
unless(-d "$ENV{SERVER_VAR}/lib/stations") { mkdir "$ENV{SERVER_VAR}/lib/stations", 0755;}
unless(-d "$ENV{SERVER_VAR}/lib/types") { mkdir "$ENV{SERVER_VAR}/lib/types", 0755;}


#
# Build checker script 
#

print "building checker script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/checker");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
  }
}
print HANDLE "PATH=\"$ENV{PATH}\"; export PATH\n";
print HANDLE "exec nice $ENV{SERVER_PERL} $ENV{SERVER_BIN}/checker.pl\n";
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/checker");
print "ok\n";

#
# Build monitor script 
#

print "building monitor script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/monitor");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
  }
}
print HANDLE "exec wish -f $ENV{SERVER_BIN}/monitor.tk\n";
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/monitor");
print "ok\n";

#
# Build restart script 
#

print "building restart script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/restart");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
  }
}
print HANDLE "$ENV{SERVER_USER_BIN}/kill-server \$\@\n";
print HANDLE "exec $ENV{SERVER_PERL} $ENV{SERVER_BIN}/restart.pl \$\@\n";
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/restart");
print "ok\n"; 

#
# Build kestrel start script
#

print "building kestrel start script...";
open(HANDLE, ">$ENV{SERVER_USER_BIN}/kestrel-start");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
      print HANDLE "$_=\"$ENV{$_}\"; export $_\n";  
  }			      
}
print HANDLE "exec $ENV{SERVER_PERL} $ENV{SERVER_BIN}/kestrel-start.pl &\n";
close HANDLE;
system("chmod +x $ENV{SERVER_USER_BIN}/kestrel-start");
print "ok\n";

#
# Build kestrel kill script
#

print "building kestrel kill script...";
open(HANDLE, ">$ENV{SERVER_USER_BIN}/kestrel-kill");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
      print HANDLE "$_=\"$ENV{$_}\"; export $_\n";  
  }			      
}
print HANDLE "exec $ENV{SERVER_PERL} $ENV{SERVER_BIN}/kestrel-kill.pl\n";
close HANDLE;
system("chmod +x $ENV{SERVER_USER_BIN}/kestrel-kill");
print "ok\n";

#
# Build kestrel checker script
#

print "building kestrel checker script...";
open(HANDLE, ">$ENV{SERVER_USER_BIN}/kestrel-checker");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
      print HANDLE "$_=\"$ENV{$_}\"; export $_\n";  
  }			      
}
print HANDLE "exec nice $ENV{SERVER_PERL} $ENV{SERVER_BIN}/kestrel-checker.pl\n";
close HANDLE;
system("chmod +x $ENV{SERVER_USER_BIN}/kestrel-checker");
print "ok\n";

#
# Build stats script
#

if ($ENV{SERVER_CUSTOMIZE}) {

    if ($ENV{SERVER_ANALOG}) {
	print "building stats script...";
	open(HANDLE, ">$ENV{SERVER_USER_BIN}/stats");
	print HANDLE "#!/bin/sh\n";
	foreach (keys %ENV) {
	    if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
		print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
	    }
	}
	print HANDLE "nice $ENV{SERVER_PERL} $ENV{SERVER_BIN}/stats.pl  \$\@\n";
	close(HANDLE);
	system("chmod +x $ENV{SERVER_USER_BIN}/stats");
	print "ok\n"; 
    }
}

#
# Build kill script 
#

print "building kill-server script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/kill-server");
print HANDLE "#!/bin/sh\n";
print HANDLE "echo killing receiver...\n";
print HANDLE "kill -9 \`cat $ENV{SERVER_VAR}/proc/receiver.pid 2>/dev/null\` 2>/dev/null\n";
print HANDLE "echo killing scheduler...\n";
print HANDLE "kill -9 \`cat $ENV{SERVER_VAR}/proc/scheduler.pid 2>/dev/null\` 2>/dev/null\n";
print HANDLE "echo killing socket-server...\n";
print HANDLE "kill -9 \`cat $ENV{SERVER_VAR}/proc/socket-server.pid 2>/dev/null\` 2>/dev/null\n";
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/kill-server");
print "ok\n"; 

#
# Build cleaner script 
#

print "building cleaner script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/cleaner");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
  }
}
print HANDLE "exec nice $ENV{SERVER_PERL} $ENV{SERVER_BIN}/cleaner.pl \$\@\n";
print "ok\n"; 
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/cleaner");

#
# Build report/dailyreport/weeklyreport/monthlyreport scripts
#

print "building report script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/report");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
  }
}
print HANDLE "exec nice $ENV{SERVER_PERL} $ENV{SERVER_BIN}/report.pl \$\@\n";
print "ok\n"; 
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/report");

print "building dailyreport script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/dailyreport");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
  }
}
print HANDLE "exec nice $ENV{SERVER_PERL} $ENV{SERVER_BIN}/dailyreport.pl \$\@\n";
print "ok\n"; 
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/dailyreport");

print "building weeklyreport script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/weeklyreport");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
  }
}
print HANDLE "exec nice $ENV{SERVER_PERL} $ENV{SERVER_BIN}/weeklyreport.pl \$\@\n";
print "ok\n"; 
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/weeklyreport");

print "building monthlyreport script..."; 
open(HANDLE, ">$ENV{SERVER_USER_BIN}/monthlyreport");
print HANDLE "#!/bin/sh\n";
foreach (keys %ENV) {
  if ($_ =~ /^SERVER_/ or $_ =~ /^KESTREL/) {
    print HANDLE "$_=\"$ENV{$_}\"; export $_\n";
  }
}
print HANDLE "exec nice $ENV{SERVER_PERL} $ENV{SERVER_BIN}/monthlyreport.pl \$\@\n";
print "ok\n"; 
close(HANDLE);
system("chmod +x $ENV{SERVER_USER_BIN}/monthlyreport");


#
# Compile a few things
#

if($ENV{SERVER_EMAIL_READY}) {
    print "compiling some... \n"; 
    system("cd ../server/src/; make");
}

#
# Get ADMINISTRATIVE Solvers
#

if(open(IN, "$ENV{SERVER_LIB}/admin_list")) {
    chomp(@adm = <IN>);
    close IN;
    @admin_solvers = ();
    foreach $entry (@adm) {
	if($entry =~ /=(.\S+)\s*$/) {
	    $admin = $1;
	    $admin =~ s/\s//g;
	    @admin_solvers = (@admin_solvers, $admin);
	}			 
    }
} else {
    print "No administrative solver list found!\n";
    sleep(2);
}

#
# CONTACT for the HELP & ADMIN solvers
#

foreach $dir (@admin_solvers) {
    system("echo $ENV{SERVER_ADMIN} > $ENV{SERVER_ADMIN_SOLVERS}/$dir/CONTACT");
}

#
# Default solver_list and solver_tree
#

system("touch $ENV{SERVER_VAR}/lib/solver_list");
unless (-f "$ENV{SERVER_VAR}/lib/solver_tree") {
  system("echo Miscellaneous=misc > $ENV{SERVER_VAR}/lib/solver_tree");
}
if($ENV{SERVER_SOLVER_GROUPS}) {
  unless(-f "$ENV{SERVER_VAR}/lib/feature_tree") {
    open(OUT, ">$ENV{SERVER_VAR}/lib/feature_tree") or die "Unable to open $ENV{SERVER_VAR}/lib/feature_tree for writing.\n";
    print OUT "Not classified=OTHER\n";
    close OUT;
  }
}

#
# Make crontab file
#

print "generating crontab for backup and reports..."; 
chomp($MAIL = `which Mail`);
unless (-x "$MAIL") {
  print "\"Mail\" must be in your PATH environment variable.\n";
  exit(1);
}

#
# construct a path environment with the necessary executables
#

$cronpath = "/bin:/usr/bin";
$pmail = $MAIL;
$pmail =~ s/\/Mail$//;
if ($cronpath !~ m#$pmail#) {
    $cronpath .= ":$pmail";
}
chomp($psendmail = `which sendmail`);
$psendmail =~ s/\/sendmail$//;
if ($cronpath !~ m#$psendmail#) {
    $cronpath .= ":$psendmail";
}
chomp($pgzip = `which gzip`);
$pgzip =~ s/\/gzip$//;
if ($cronpath !~ m#$pgzip#) {
    $cronpath .= ":$pgzip";
}
chomp($ptar = `which tar`);
$ptar =~ s#/tar$##;
if ($cronpath !~ m#$ptar#) {
    $cronpath .= ":$ptar";
}
$cronpath .= ":.";

open(HANDLE, ">crontabfile");
print HANDLE "# Entry for checker (checks server every 15 minutes)\n";
print HANDLE "1,16,31,46 * * * * PATH=$cronpath; export PATH; ($ENV{SERVER_PERL} $ENV{SERVER_USER_BIN}/checker >> $ENV{SERVER_VAR}/logs/checker 2>&1)\n";

#
# Default saves jobs, comment out regular cleaner entry and
#         uncomment -nosave cleaner entry to delete jobs
#

print HANDLE "# Entry for cleaner (saves old jobs every 30 minutes)\n";
print HANDLE "2,32 * * * * PATH=$cronpath; export PATH; ( nice $ENV{SERVER_PERL} $ENV{SERVER_USER_BIN}/cleaner >> $ENV{SERVER_VAR}/logs/cleaner ) 2>&1\n";
print HANDLE "# Entry for cleaner (removes old jobs every hour)\n";
print HANDLE "#0 * * * * PATH=$cronpath; export PATH; ( nice $ENV{SERVER_PERL} $ENV{SERVER_USER_BIN}/cleaner -nosave >> $ENV{SERVER_VAR}/logs/cleaner ) 2>&1\n";
print HANDLE "# Entry for monthly report (runs on the first of the month at 01:11 a.m.)\n";
print HANDLE "#11 01 1 * * ( nice $ENV{SERVER_PERL} $ENV{SERVER_USER_BIN}/monthlyreport | $MAIL -s \"Monthly Report\" $ENV{SERVER_ADMIN} ) 2>/dev/null\n";
print HANDLE "# Entry for weekly report (runs every sunday night at 11:59 p.m.)\n";
print HANDLE "58 23 * * 0 PATH=$cronpath; export PATH; ( nice $ENV{SERVER_PERL} $ENV{SERVER_USER_BIN}/weeklyreport | $MAIL -s \"Weekly Report\" $ENV{SERVER_ADMIN}, $ENV{SERVER_COMMENTS} ) 2>/dev/null\n";
print HANDLE "# Entry for daily report (runs every night at 11:59 p.m.)\n";
print HANDLE "59 23 * * * PATH=$cronpath; export PATH; ( nice $ENV{SERVER_PERL} $ENV{SERVER_USER_BIN}/dailyreport  | $MAIL -s \"Daily Report\" $ENV{SERVER_ADMIN} ) 2>/dev/null\n#\n";

if ($ENV{SERVER_CUSTOMIZE}) {
    if ($ENV{SERVER_ANALOG}) {
	print HANDLE "# Entry for Web statistics analyzer\n";
	print HANDLE "20 5 * * * PATH=$cronpath; export PATH; (nice $ENV{SERVER_PERL} $ENV{SERVER_USER_BIN}/stats ) 2>/dev/null\n#\n";
    }
    if ($ENV{KESTREL_INSTALL}) {
	print HANDLE "# Entry for kestrel checker\n";
	print HANDLE "3,18,33,48 * * * * PATH=$cronpath; export PATH; LD_LIBRARY_PATH=$ENV{LD_LIBRARY_PATH}; export LD_LIBRARY_PATH; ($ENV{SERVER_USER_BIN}/kestrel-checker >>$ENV{SERVER_VAR}/logs/kestrel) 2>>$ENV{SERVER_VAR}/logs/kestrel\n#\n";
    }
}

close(HANDLE);
print "ok\n"; 

#
# Flush old e-mail
#

if ($ENV{SERVER_EMAIL_READY}) {
  print "\nFlushing $ENV{SERVER_USER} e-mail to directory spam:\n"; 
  system("inc -host $ENV{SERVER_POP_SERVER} -rpop +spam 2>/dev/null") or print "\n**The above e-mail will not be processed by the Server.**\n";
}



#
# Customize administrative solver files
#
#

foreach $dir (@admin_solvers) {
    $DIR = "$ENV{SERVER_ADMIN_SOLVERS}/$dir/*.server";
    @files = glob($DIR);
    foreach $file (@files) {
	$file =~ s/\.server//;
	$TEMPLATE = `cat ${file}.server`;
	$TEMPLATE =~ s/<SERVER_NAME>/$ENV{SERVER_NAME}/g;
	$TEMPLATE =~ s/<SERVER_COMMENTS>/$ENV{SERVER_COMMENTS}/g;
	$TEMPLATE =~ s/<SERVER_WWW_URL>/$ENV{SERVER_WWW_URL}/g;
	print "creating $file ...\n";
	open (UNIQUE, ">$file");
	print UNIQUE $TEMPLATE;
	close UNIQUE;
    }
}

if(-f "$ENV{SERVER_ADMIN_SOLVERS}/ADMIN:ADDSOLVER/config.server") {
    system("cp $ENV{SERVER_ADMIN_SOLVERS}/ADMIN:ADDSOLVER/config.server $ENV{SERVER_ADMIN_SOLVERS}/ADMIN:ADDSOLVER/config");
    if($ENV{"SERVER_SOLVER_GROUPS"}) {
      open(READ, "$ENV{SERVER_ADMIN_SOLVERS}/ADMIN:ADDSOLVER/config.server");
      open(WRITE, ">$ENV{SERVER_ADMIN_SOLVERS}/ADMIN:ADDSOLVER/config");
      while(<READ>) {
        if(/SOLVER\:NULL\:TOKEN/) {
          print WRITE "Solver Group::GROUP:NULL:GROUP\n";
        }
        print WRITE;
      }
      close READ;
      close WRITE;
      open(IN, "$ENV{SERVER_VAR}/lib/feature_tree");
      @inputs = <IN>;
      close IN;
      open(OUT, ">>$ENV{SERVER_ADMIN_SOLVERS}/ADMIN:ADDSOLVER/config");
      foreach $input (@inputs) {
        $input =~ /(.*)=(.*)\s*$/;
        $description = $1;
        $file = $2;
        print OUT "\n$description:BINARY-OFF:$file:NULL:$file.in";
      }
      print OUT "\nOther features::OTHER_FEATURES:NULL:OTHER.in";
      close OUT;
    }
}

#
# Generate web pages!
#

WEB: 

if ($ENV{SERVER_WWW_READY}) {
  print "\nCreating WEB/CGI area:\n"; 
  system("cd  $ENV{SERVER_BIN}; $ENV{SERVER_PERL} make-all.pl") and  die $_;

  if ($ENV{SERVER_CUSTOMIZE}) {
      if ($ENV{SERVER_ANALOG}) {
	  if(!(-r "$ENV{SERVER_WWW_HOME}/month_stats.html")) {
	      system("cd  $ENV{SERVER_USER_BIN}; stats") and die $_;
	  }
      }
  }
}


#
# Instructions
#

print "\n\n";
print "**********************************\n"; 
print "* INSTALLATION COMPLETE <RETURN> *\n"; 
print "**********************************\n\n"; 
<STDIN>;
system("clear");
print "*******************************************************************\n";
print "  To start the Server run:\n";
print "  $ENV{SERVER_USER_BIN}/restart\n\n";
print "  To monitor the Server run:\n";
print "  $ENV{SERVER_USER_BIN}/monitor \n\n";
print "  NOTE: Run \"crontab crontabfile\" in the current directory\n";
print "        to restart your Server via cron if it crashes. \n";
print "        THIS WILL REPLACE YOUR OLD CRONTAB IF YOU HAVE ONE.\n\n"; 
if ($ENV{SERVER_WWW_READY}) {
  print "  NOTE: Give these Web server read/write permission:\n"; 
  print "        $ENV{SERVER_WWW_HOME}/jobs\n";
  print "        $ENV{SERVER_VAR}/spool/WEB\n";
  print "        $ENV{SERVER_VAR}/databases\n";
  print "        $ENV{SERVER_VAR}/tmp\n\n";
}
if($ENV{SERVER_FEEDBACK})
{
  print "  NOTE: Place your HTML-formatted user feedback in \n        $ENV{SERVER_LIB}/html/stories.html.\n\n";
}
if($ENV{SERVER_SOLVER_GROUPS}) {
  print "  NOTE: Edit this file to describe solver features:\n        $ENV{SERVER_VAR}/lib/feature_tree\n\n";
}  
print "  NOTE: You can re-configure your Server at any time by re-running\n";
print "        \"make\" in this directory and then restarting the Server.\n";
print "*******************************************************************\n";
print "\n";

#
# Subroutines
#

sub get_value {
  unless (length($ENV{$_[0]})>0) {
    $ENV{$_[0]} = $_[1];
  } 
  print "$_[0] [$ENV{$_[0]}] --> ";
  chop($value = <STDIN>);
  if ($value =~ /\S+/) {
    $ENV{$_[0]} = $value;
  }
}










