
#
# Restart the server if the last instance of "checker"
# killed everything, in which case the file $RESTART
# will exist.
#

$RESTART = "$ENV{SERVER_TMP}/$ENV{SERVER_USER}-checker-restart-$ENV{SERVER_PORT}";

if (-f "$RESTART") {

  #
  # Notify someone of the restart
  #

  chop($TIME = `date`);
  open(handle, "|Mail -s '$ENV{SERVER_NAME} restart' $ENV{SERVER_ADMIN}");
  print handle "checker: $ENV{SERVER_NAME} restarted on $TIME"; 
  close(handle);
  print "checker: restarting $ENV{SERVER_NAME} on $TIME\n"; 

  #
  # Restart server
  #

  system("rm -f $RESTART");
  system("$ENV{SERVER_HOME}/bin/restart >/dev/null");

  exit(0);
}

$verify = `echo verify | $ENV{SERVER_PERL} $ENV{SERVER_BIN}/client.pl -silent -server $ENV{SERVER_HOSTNAME}:$ENV{SERVER_PORT}`;
if ($verify ne $ENV{SERVER_NAME}) {

  #
  # Kill the server and restart on next cron
  #

  chop($TIME = `date`);
  system("echo checker: killing all $ENV{SERVER_NAME} processes on $TIME.");
  system("$ENV{SERVER_HOME}/bin/kill-server >/dev/null");
  system("touch $RESTART");
#  exec("sh -c 'kill -9 -1'");
}
