#!/bin/sh
#
# @package     hubzero-submit-server
# @copyright   Copyright (c) 2006-2020 The Regents of the University of California.
# @license     http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2006-2020 The Regents of the University of California.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

#
# start/stop the submit server daemon.
#
### BEGIN INIT INFO
# Provides:          submit-server
# Required-Start:    $syslog $time $remote_fs tunnel_monitor job_monitor
# Required-Stop:     $syslog $time $remote_fs tunnel_monitor job_monitor
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Starts/stops the submit-server daemon.
# Description:       Starts and stops the submit-server daemon so that
#                    submit clients can launch jobs elsewhere.
### END INIT INFO

# Source function library.
if [ -f /etc/init.d/functions ]; then
   . /etc/init.d/functions
fi

if ! [ -x /usr/sbin/submit-server.py ]; then
   exit 1
fi

chkrunning() {
  pid=""
  if [ -f /var/run/submit-server.pid ]
  then
    pid=$(cat /var/run/submit-server.pid)
  fi
  if [ "x${pid}x" != "xx" ] ; then
    list=$(pidof python)
    for x in $list ; do
      if [ $pid = $x ] ; then
        echo "ERROR: submit server appears to be running already."
        exit 1
      fi
    done
  fi
}

case "$1" in
   start)
      if [ -f /sbin/start-stop-daemon ]; then
         chkrunning
         echo -n "Starting submit-server:"
         echo -n " submit-server" ; /sbin/start-stop-daemon --start --quiet --pidfile /var/run/submit-server.pid --exec /usr/sbin/submit-server.py
      else
         echo -n " submit-server" ; daemon --check submit-server --pidfile /var/run/submit-server.pid /usr/sbin/submit-server.py
      fi
      echo "."
      ;;
    stop)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Stopping submit-server:"
         echo -n " submit-server" ; /sbin/start-stop-daemon --stop --quiet --oknodo --signal INT --pidfile /var/run/submit-server.pid
      else
         echo -n " submit-server" ; killproc -p /var/run/submit-server.pid /usr/sbin/submit-server.py -TERM
      fi
      echo "."
      ;;
 restart)
      if [ -f /sbin/start-stop-daemon ]; then
         echo -n "Restarting submit-server:"
         echo -n " submit-server"
         /sbin/start-stop-daemon --stop --quiet --oknodo --signal INT --pidfile /var/run/submit-server.pid
         /sbin/start-stop-daemon --start --quiet --pidfile /var/run/submit-server.pid --exec /usr/sbin/submit-server.py
      else
         echo -n " submit-server" ; killproc -p /var/run/submit-server.pid /usr/sbin/submit-server.py -TERM
         echo -n " submit-server" ; daemon --check submit-server --pidfile /var/run/submit-server.pid /usr/sbin/submit-server.py
      fi
      echo "."
      ;;
       *)
      echo "Usage: /etc/init.d/submit-server {start|stop|restart}"
      exit 1
      ;;
esac

exit 0
