#! /bin/bash
#
### BEGIN INIT INFO
# Provides:          fwbooster
# Required-Start:    $local_fs $network $named $portmap $remote_fs $syslog $time
# Required-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: FileWave Booster
# Description:       FileWave Booster.
#                    The script launches fwbooster, the
#                    FileWave booster, as a daemon. It will
#                    open ports 20003, 20004, 20014, 20018, 20026
#                    for TCP traffic.
### END INIT INFO

PATH=/bin:/sbin:/usr/bin:/usr/sbin
NAME="FileWave booster"
DAEMON=/usr/local/sbin/fwbooster
PIDFILE=/private/var/run/fwbooster.pid
FW_HOME=/usr/local/filewave

SUPERVISORD="$FW_HOME/python/bin/supervisord"
SUPERVISORD_PID="/private/var/run/supervisord-booster.pid"
SUPERVISORCTL="$FW_HOME/python/bin/supervisorctl"
SUPERVISOR_OPTIONS="-c /usr/local/etc/filewave/supervisor/supervisord-booster.conf"
lockfile="/var/lock/subsys/supervisord-booster"

. /lib/lsb/init-functions
set +e # again, because init-functions disables it

$(type -t log_daemon_msg &>/dev/null) && fcndefined="yes" || fcndefined="no"
if [ "$fcndefined" = "no" ]; then
    log_daemon_msg ()
    {
        echo "$@"
    }
fi

$(type -t log_end_msg &>/dev/null) && fcndefined="yes" || fcndefined="no"
if [ "$fcndefined" = "no" ]; then
    log_end_msg ()
    {
        return 0
    }
fi

boosterIsRunning()
{
    echo `ps ax | grep -i "/usr/local/sbin/fwbooster" | grep -v 'grep' | wc -l`
}

function openfirewall()
{
    #FileWave Booster Ports
    echo "Opening Firewall ports (using firewall-cmd if firewalld is active or iptables as fallback)"
    for port in 20003 20004 20014 20018 20026 20030; do
        if [ -e /usr/bin/firewall-cmd ] && systemctl is-active --quiet firewalld; then
            /usr/bin/firewall-cmd --add-port $port/tcp --permanent &>/dev/null || true
        else
            iptables -t filter -I INPUT -p tcp --dport $port -m state --state NEW,ESTABLISHED -j ACCEPT &>/dev/null || true
        fi

    done
    /usr/bin/firewall-cmd --reload &>/dev/null || true
}

function closefirewall()
{
    #FileWave Booster Ports
    echo "Closing Firewall ports (using firewall-cmd if firewalld is active or iptables as fallback)"
    for port in 20003 20004 20014 20018 20026 20030; do
        if [ -e /usr/bin/firewall-cmd ] && systemctl is-active --quiet firewalld; then
            /usr/bin/firewall-cmd --remove-port $port/tcp --permanent &>/dev/null || true
        else
            iptables -t filter -D INPUT -p tcp --dport $port -m state --state NEW,ESTABLISHED -j ACCEPT &>/dev/null || true
        fi
    done
    /usr/bin/firewall-cmd --reload &>/dev/null || true
}

start()
{
    # makes it possible to get core files
    #DAEMON_COREFILE_LIMIT=unlimited
    #ulimit -c unlimited
    #echo '/tmp/cores/core.%e.%p.%h.%t' > /proc/sys/kernel/core_pattern

    # start the booster
    log_daemon_msg "Starting $NAME"
    [ -e /usr/bin/firewall-cmd ] || openfirewall
    $SUPERVISORCTL $SUPERVISOR_OPTIONS start all

}

stop()
{
    log_daemon_msg "Stopping $NAME"

    $SUPERVISORCTL $SUPERVISOR_OPTIONS stop all

    [ -e /usr/bin/firewall-cmd ] || closefirewall
    log_end_msg 0
    log_success_msg "Stopped $NAME"
}

restart()
{
    stop
    start
}

force-reload()
{
    restart
}

status()
{
    $SUPERVISORCTL $SUPERVISOR_OPTIONS status
}

version()
{
    /usr/local/sbin/fwbooster -V
}


case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    force-reload)
        force_reload
        ;;
    openfirewall)
        openfirewall
        ;;
    closefirewall)
        closefirewall
        ;;
    startNats)   $SUPERVISORCTL $SUPERVISOR_OPTIONS start nats ;;
    stopNats)    $SUPERVISORCTL $SUPERVISOR_OPTIONS stop nats ;;
    restartNats) $SUPERVISORCTL $SUPERVISOR_OPTIONS restart nats ;;
     status)
        status
        ;;
    version)
        version
        ;;
    *)
       echo $"Usage: $0 {start|stop|restart|force-reload|status|openfirewall|closefirewall|startNats|stopNats|restartNats|version}"
       exit 1
esac

exit 0
