
$JOBDIR = "$ENV{SERVER_VAR}/jobs";
$SOLVERDIR = "$ENV{SERVER_SOLVERS}";
$QUEUED = 0;

#
# read in the data
#

open(RESULTS, ">>job.results") or die "Could not open job.results.\n";
$| = 1; select RESULTS; $| = 1; select stdout;

print "Checking input...\n";
open(FILE, "Number") or (print RESULTS "No job number given.\n" and die);
chomp($job = <FILE>);
close FILE;
if($job < 0) {
    print RESULTS "Not a legitimate job number: $job\n";
    exit;
}
if(-s "Job_PASS") {
    open(FILE, "Job_PASS") or die "Could not open Job_PASS.\n";
    chomp($jpass = <FILE>);
    close FILE;
}
if(-s "PASSWORD") {
    open(FILE, "PASSWORD") or die "Could not open PASSWORD.\n";
    chomp($spass = <FILE>);
    close FILE;
}
unless((defined($jpass)) || (defined($spass))) {
    print RESULTS "You must know either the job or solver password.\n";
    close RESULTS;
    exit;
}

if(-d "$JOBDIR/job.$job") {
    if(-s "$JOBDIR/job.$job/job.type") {
	open(FILE, "$JOBDIR/job.$job/job.type") or die $!;
	chomp($solver = <FILE>); 
	close FILE;
	if($solver =~ /ADMIN/) {
	    print RESULTS "Administrative jobs may not be killed.\n";
	    close RESULTS;
	    exit;
	}
    }
    if(-s "$JOBDIR/job.$job/job.station") {
	open(FILE, "$JOBDIR/job.$job/job.station") or die $!;
	chomp($station = <FILE>); 
	close FILE;
    } else {
	$QUEUED = 1;
    }
} else {
    print RESULTS "No job directory job.$job found in Server.\n";
    close RESULTS;
    exit;
}

if(defined($jpass)) {
    if(-s "$JOBDIR/job.$job/job.password") {
	open(FILE, "$JOBDIR/job.$job/job.password") or die "Could not open job password file.\n";
	chomp($JPASS = <FILE>);
	close FILE;
	$jpass =~ s/\s//g;
	$JPASS =~ s/\s//g;
	unless($jpass eq $JPASS) {
	    print RESULTS "Job password incorrect.\n";
	    close RESULTS;
	    exit;
	}
    } else {
	print RESULTS "No password found for job $job\n(Job may no longer be running).\n";
	close RESULTS;
	exit;
    }
}
if(defined($spass)) {
    if(-s "$SOLVERDIR/$solver/PASSWORD") {
	open(FILE, "$SOLVERDIR/$solver/PASSWORD") or die $!;
	chomp($SPASS = <FILE>);
	close FILE;
	$spass =~ s/\s//g;
	$SPASS =~ s/\s//g;
	unless($spass eq $SPASS) {
	    print RESULTS "Solver password is incorrect.\n";
	    close RESULTS;
	    exit;
	}
    } else {
	print RESULTS "Solver $solver is no longer online.\n";
	close RESULTS;
	exit;
    }
}

if((defined($solver))) {

 if (defined($station)) {

#
# Find port
#
    open(FILE, "$ENV{SERVER_VAR}/lib/station_type_port") or die $!;
    while (<FILE>) {
	if(/^$station:$solver:(\d+)/) {
	    $port = $1;
	    last;
	}
    }
    close FILE;
    if(defined($port)) {
	print "\n\n Requesting job kill on solver station...\n";
	print RESULTS "\n\nRequesting job kill on solver station...\n";
     print RESULTS "Station $station, solver $solver\n";
	close RESULTS;
#
# Put an alarm around this call.
# Unfortunately, I don't have a good way of testing the
# alarm feature.  This appeared as a problem when multiple
# kill jobs were hanging when the solver station was still
# running but had lost NFS.  I don't know how to duplicate 
# that 'comms daemon still running but not returning from 
# kill request' situation.
#
     eval {
       local $SIG{"ALRM"} = sub {die "alarm clock"; };
       alarm(60);
       system("echo 'kill job $job -token $solver' | $ENV{SERVER_PERL} $ENV{SERVER_BIN}/client.pl -server $station:$port >>job.results");
       alarm(0);
     };
     if ($@) {
       if($@ =~ /alarm clock/) { 
         print RESULTS "\nJob kill request timed out before completion.\n";
       } else {
         print RESULTS "\nFailure in job kill request.\n";
       }
     }
     close RESULTS;  
    } else {
	print RESULTS "Solver $solver no longer registered on $station:$port.\n";
	close RESULTS;
    }
 } else {
     open(NONE, ">$ENV{SERVER_JOBS}/job.$job/DONE");
     close NONE;
     # give the scheduler some excuse to ditch the job
     open(NONE, ">$ENV{SERVER_JOBS}/job.$job/BAD_PARSE");
     close NONE;
     print RESULTS "No station found.  Job might be in queue.\n";
     print RESULTS "Will dequeue job $job, if present.\n";
     close RESULTS;
 }
} else {
    print RESULTS "Corrupt job file.  Please contact $ENV{SERVER_COMMENTS} for help.\n";
    close RESULTS;
    exit;
}

