#! /bin/sh
# @package      hubzero-mw2-front-proxy
# @file         /etc/init.d/front-proxy
# @copyright    Copyright (c) 2016-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Packaging and tweaks of original work by Richard L. Kennell
#
# Copyright (c) 2016-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.
#

### BEGIN INIT INFO
# Provides:          front-proxy
# Required-Start:    $local_fs $network $remote_fs
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Front-end web proxy
# Description:       front-proxy runs on the front-end web server to redirect
#                    recognized types of AJAX and WebSocket traffic to the
#                    exec-proxy on an execution host.
### END INIT INFO

# To activate this init script, use the following commands:
# Debian: update-rc.d front-proxy defaults
# RedHat: chkconfig --add front-proxy

# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
NAME=front-proxy
DESC="Front-end proxy"
DAEMONNAME=${NAME}.py
DAEMON=/usr/bin/${DAEMONNAME}
DAEMON_ARGS=""
PIDFILE=/var/run/${NAME}.pid
SCRIPTNAME=/etc/init.d/${NAME}

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# including init-functions is needed for compatibility with systemd
# but on RHEL 6 gives error "No such file or directory"
[ -f /lib/lsb/init-functions ] && . /lib/lsb/init-functions

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

#
# Print things in red or green.  Assume an ANSI terminal.
#
red() {
        tty -s && tput setaf 1 && echo -n $@ && tput sgr0 || echo "$@"
}
green() {
        tty -s && tput setaf 2 && echo -n $@ && tput sgr0 || echo "$@"
}

success() {
        echo "[ $(green ok) ] $@"
        exit 0
}

fail() {
        echo "[$(red FAIL)] $@"
        exit 1
}

#
# Check the status of the proxy.
#
do_status() {
	# Return
	#   0 if daemon is running
	#   1 if daemon is not running
        [ -f $PIDFILE ] && kill -0 $(cat $PIDFILE 2>/dev/null) 2>/dev/null && return 0
        rm -f $PIDFILE
        return 1
}

#
# Function that starts the daemon/service
#
do_start()
{
	# Return
	#   0 if daemon has been started
	#   1 if daemon was already running
	#   2 if daemon could not be started
        do_status && return 1
        # make sure the front-proxy activity doesn't slow down more critical processes
        nice $DAEMON || return 2
        return 0
}

#
# Function that stops the daemon/service
#
do_stop()
{
	# Return
	#   0 if daemon has been stopped
	#   1 if daemon was already stopped
	#   2 if daemon could not be stopped
	#   other if a failure occurred
        do_status || return 1
        PID=$(cat $PIDFILE)
        kill -TERM $PID || return 1
        do_status || return 0
        sleep 1
        do_status || return 0
        kill -KILL $PID || return 0
        rm -f $PIDFILE
	return 0
}

#
# Function that sends a SIGHUP to the daemon/service
#
do_reload() {
	#
	# If the daemon can reload its configuration without
	# restarting (for example, when it is sent a SIGHUP),
	# then implement that here.
	#
        :
}

case "$1" in
  start)
	do_start
	case "$?" in
		0) success "Started $DESC $NAME" ;;
                1) fail "$DESC $NAME is already running" ;;
		2) fail "$DESC $NAME could not be started" ;;
	esac
	;;
  stop)
	do_stop
	case "$?" in
		0) success "Stopped $DESC $NAME" ;;
                1) success "$DESC $NAME already stopped" ;;
		2) fail "Unable to stop $DESC $NAME" ;;
	esac
	;;
  status)
	do_status && success 'Proxy is running' || fail 'Proxy not running'
	;;
  #reload|force-reload)
	#
	# If do_reload() is not implemented then leave this commented out
	# and leave 'force-reload' as an alias for 'restart'.
	#
	#echo -n "Reloading $DESC" "$NAME"
	#do_reload
	#$? && success || fail
	#;;
  restart|force-reload)
	#
	# If the "reload" option is implemented then remove the
	# 'force-reload' alias
	#
	do_stop
	case "$?" in
	  0|1)
		do_start
		case "$?" in
			0) success "Restarted $DESC $NAME" ;;
			1) fail "$DESC $NAME still running" ;;
			*) fail "Failed to start $DESC $NAME" ;;
		esac
		;;
	  *)
		# Failed to stop
		fail "Failed to stop $DESC $NAME"
		;;
	esac
	;;
  *)
	#echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
	exit 3
	;;
esac

:
