#!/bin/sh
#
# @package     hubzero-submit-server
# @author      Rick Kennell <kennell@purdue.edu>
# @author      Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright   Copyright (c) 2006-2013 HUBzero Foundation, LLC.
# @license     http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
#
# Copyright (c) 2006-2013 HUBzero Foundation, LLC.
#
# This file is part of: The HUBzero Platform for Scientific Collaboration
#
# The HUBzero Platform for Scientific Collaboration (HUBzero) is free
# software: you can redistribute it and/or modify it under the terms of
# the GNU Lesser General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# HUBzero is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#

#
# start/stop the submit server daemon.
#
### BEGIN INIT INFO
# Provides:          submit-server
# Required-Start:    $syslog $time tunnel_monitor job_monitor
# Required-Stop:     $syslog $time 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

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)
        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
        echo "."
        ;;
    stop)
        echo -n "Stopping submit-server:"
        echo -n " submit-server" ; /sbin/start-stop-daemon --stop --quiet --oknodo --signal INT --pidfile /var/run/submit-server.pid
        echo "."
        ;;
    restart)
        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
        echo "."
        ;;
    *)
        echo "Usage: /etc/init.d/submit-server {start|stop|restart}"
        exit 1
        ;;
esac

exit 0
