require "flush.pl";

$SERVER_HOME = $ENV{SERVER_HOME}; 
$LIBRARY     = $ENV{SERVER_SOLVERS};

#
# Software Type
#

if (-s "TYPE") {
  chomp($TYPE = `cat TYPE`);
  $TYPE = "\U$TYPE";
} else {
  &fail("Software Type missing");
}

#
# Solver Title
#

if (-s "SOLVER") {
  chomp($SOLVER = `cat SOLVER`);
  $SOLVER = "\U$SOLVER";
  $SOLVER =~ s/\s//g;
} else {                   
  &fail("Solver Identifier missing");
}

$TOKEN = "$TYPE:$SOLVER";
$DIR   = "$LIBRARY/$TOKEN";

#
# Correct password for token
#

if (-s "PASSWORD") {
  chomp($PASSWORD = `cat PASSWORD`);
  if (-e "$DIR") 
  {
    chomp($PWORD = `cat $DIR/PASSWORD`); 
    if ($PASSWORD ne $PWORD) 
    { 
      &fail("incorrect password for \"$TOKEN\"");
    } 
    else {
      $UPDATE = 1;
    }
  }  
  else {
    &fail("\"$TOKEN\" is not registered with the $ENV{SERVER_NAME}");
  }
} 
else {
  &fail("Password missing");
}

#
# Get status
#

print "Updating solver status...\n"; &flush(STDOUT);
chomp($status=`cat status`);

if ($status =~ /^enable/) {
  &enable;
} elsif ($status =~ /^disable/) {
  &disable;
} elsif ($status =~ /^delete/) {
  &delete;
}

#
# Write to job.results
#

open (CONFIRM, ">>job.results");
write CONFIRM; 


if ($ENV{SERVER_WWW_READY}) {

  #
  # Update Web area
  #

  if ($update) {      
    print "Updating WEB/CGI area:\n\n"; &flush(STDOUT);
    system("cd $ENV{SERVER_BIN}; echo a | $ENV{SERVER_PERL} make-server-homepage.pl > $ENV{SERVER_WWW_HOME}/index.html")
    && die "$0: can't create $ENV{SERVER_WWW_HOME}/index.html\n";
    system("cd $ENV{SERVER_BIN}; $ENV{SERVER_PERL} make-server-solvers.pl   > $ENV{SERVER_WWW_HOME}/server-solvers.html")
    && die "$0: can't create $ENV{SERVER_WWW_HOME}/server-solvers.html\n";
    system("cd $ENV{SERVER_BIN}; $ENV{SERVER_PERL} 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";
  }
}

#
# Output format
#

format CONFIRM =

  Software token  : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$TOKEN
  Software status : @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$STATUS
.

sub fail {
  open (HANDLE, ">>job.results");
  print HANDLE "ERROR: $_[0].\n";
  close(HANDLE);
  exit; 
}

sub enable {
  $list = `cat $ENV{SERVER_VAR}/lib/solver_list`;
  if ($list =~ /\b$TOKEN\b/) {
    &fail("\"$TOKEN\" is already enabled");
  } else {
    @list = `cat $ENV{SERVER_VAR}/lib/solver_list`;
    chomp($TITLE=`cat $LIBRARY/$TOKEN/TITLE`);
    @list = ("$TITLE=$TOKEN\n", @list);
    @list = sort(@list);
    open(handle, ">$ENV{SERVER_VAR}/lib/solver_list.tmp"); 
    foreach (@list) {
      print handle $_; 
    }
    close(handle);
    system("mv $ENV{SERVER_VAR}/lib/solver_list.tmp $ENV{SERVER_VAR}/lib/solver_list");
    $STATUS = "enabled";
    $update = 1;
  }
}

#
# Disable software - but still leave all of its registration info.
#

sub disable {
  $list = `cat $ENV{SERVER_VAR}/lib/solver_list`;
  unless ($list =~ /\b$TOKEN\b/) {
    &fail("\"$TOKEN\" is already disabled");
  } else {
    $list =~ s/.*=$TOKEN\n//; 
    open(handle, ">$ENV{SERVER_VAR}/lib/solver_list.tmp"); 
    print handle $list;
    close(handle);
    system("mv $ENV{SERVER_VAR}/lib/solver_list.tmp $ENV{SERVER_VAR}/lib/solver_list");
    $STATUS = "disabled";
    $update = 1;
  }
}

#
# Delete entry in solver library
#

sub delete {
  $list = `cat $ENV{SERVER_VAR}/lib/solver_list`;
  if ($list =~ s/.*=$TOKEN\n//) {
    $update = 1;
  }

  #
  # tell scheduler to update station_type_port
  #

  chomp(@stations = `cat $LIBRARY/$TOKEN/STATIONS`);
  foreach (@stations) {
    open(handle, ">$ENV{SERVER_TMP}/$$"); 
    print handle "$_:$TOKEN\n";
    close(handle);
    system("mv $ENV{SERVER_TMP}/$$ $ENV{SERVER_VAR}/spool/PORTS/port_delete.$$");
  } 
  system("touch $ENV{SERVER_VAR}/spool/PORTS/UPDATE");
  open(handle, ">$ENV{SERVER_TMP}/$$"); 
  print handle $list;
  close(handle);
  system("mv $ENV{SERVER_TMP}/$$ $ENV{SERVER_VAR}/lib/solver_list");
  system("rm -f $LIBRARY/$TOKEN/*");;
  system("rmdir $LIBRARY/$TOKEN");;

  #
  # Delete Web pages/forms 
  # 

  if ($ENV{SERVER_WWW_READY}) {
    system("/bin/rm -f $ENV{SERVER_WWW_HOME}/solvers/$TOKEN/*");
    system("/bin/rmdir $ENV{SERVER_WWW_HOME}/solvers/$TOKEN");
    system("/bin/rm -f $ENV{SERVER_CGI_HOME}/$TOKEN/*");
    system("/bin/rmdir $ENV{SERVER_CGI_HOME}/$TOKEN");
  }

  $STATUS = "deleted";
}

