#! /bin/sh
# @package      hubzero-iptables-basic
# @file         hubzero-iptables-basic.init
# @copyright    Copyright (c) 2010-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-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.
#
# description:          Start/Stop the default hubzero iptables firewall
# chkconfig:            345 09 93
#
### BEGIN INIT INFO
# Provides:          hubzero-iptables-basic
# Should-Start:      iptables
# Should-Stop:       iptables
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start/Stop the default hubzero iptables firewall
### END INIT INFO

PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="HUBzero Basic Iptables Firewall"
NAME=hubzero-iptables-basic
DAEMON=/usr/sbin/$NAME
DAEMON_ARGS=""
LOGFILE=/var/log/$NAME/$NAME.log
PIDFILE=/var/run/$NAME.pid
DEBUG=0

# Don't redirect to systemctl on systemd systems
_SYSTEMCTL_SKIP_REDIRECT=true

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

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

# Load RedHat configuration variable file if it is present
[ -r /etc/sysconfig/$NAME ] && . /etc/sysconfig/$NAME

# Always be verbose 
VERBOSE=yes
# Load Debian rcS variables (sets VERBOSE flag as appropriate)
#if [ -r /lib/init/vars.sh ]
#then
#        . /lib/init/vars.sh
#else
#        VERBOSE=yes
#fi

# Load RedHat LSB function library
if [ -r /etc/rc.d/init.d/functions ]
then
        . /etc/rc.d/init.d/functions

        log_success_msg () {
            echo -n $*; success "$*"; echo
        }
        log_failure_msg () {
            echo -n $*; failure "$*"; echo
        }
        log_warning_msg () {
            echo -n $*; warning "$*"; echo
        }
        log_daemon_msg () {
            echo -n $*
        }
fi

# Load Debian LSB function library
if [ -r /lib/lsb/init-functions ]
then
        . /lib/lsb/init-functions
fi

start() {
        [ "$VERBOSE" != no ] && log_daemon_msg "Starting" "$NAME"

        $DAEMON start
 
        if [ $? -ne 0 ]
        then
                [ "$VERBOSE" != no ] && log_failure_msg
                return 1
        fi

        if [ -d /var/lock/subsys ]
        then
                touch /var/lock/subsys/$NAME > /dev/null 2>&1

                if [ $? -ne 0 ]
                then
                        [ "$VERBOSE" != no ] && log_warning_msg "Failed to touch /var/lock/subsys/$NAME"
                        return 0
                fi
        fi

        [ "$VERBOSE" != no ] && log_success_msg

        return 0
}

stop() {
        [ "$VERBOSE" != no ] && log_daemon_msg "Stopping" "$NAME"

        $DAEMON stop

        if [ $? -eq 0 ]
        then
                if [ -d /var/lock/subsys ]
                then
                        rm -f /var/lock/subsys/$NAME
                fi

                [ "$VERBOSE" != no ] && log_success_msg

                return 0
        fi

        [ "$VERBOSE" != no ] && log_failure_msg

        return 2
}

case "$1" in
    start)
        start
        ;;

    stop)
        stop
        ;;

    condrestart)
        if [ -d /var/lock/subsys ]
        then
            if [ -r /var/lock/subsys/$NAME ]
            then
                start
            fi
        fi
        ;;

    restart)
        start
        ;;

    force-reload)
        start
        ;;

    *)
        echo "Usage: $SCRIPTNAME {start|stop|restart|condrestart}" >&2
        exit 3
        ;;
esac
