#!/bin/bash
### BEGIN INIT INFO
# Provides:          ispprotect_bandaemon
# Required-Start:    networking
# Required-Stop:     networking
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: ISPConfig3 Ban-Daemon
# Description:       ISPConfig3 Ban-Daemon
### END INIT INFO

TMPDIR=/tmp
PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/sbin:/usr/local/bin:/usr/X11R6/bin

case "$1" in
    start)
        echo -n "Starting ISPProtect BanDaemon"
        /opt/ispprotect_bandaemon/ispp_bandaemon
        echo " done"
    ;;
    stop)
        echo -n "Shutting down ISPProtect BanDaemon"
        array=(`ps ax | grep -iw '/opt/ispprotect_bandaemon' | grep -iv 'grep' | awk '{print $1}' | cut -f1 -d/ | tr '\n' ' '`)
        element_count=${#array[@]}
        index=0
        while [ "$index" -lt "$element_count" ]
        do
          kill -15 ${array[$index]}
          let "index = $index + 1"
        done
        echo " done"
    ;;
    restart)
        $0 stop  && sleep 3
        $0 start
        # Remember status and be quiet
    ;;
    reload)
        $0 stop  && sleep 3
        $0 start
        # Remember status and be quiet
    ;;
    *)
    echo "Usage: $0 {start|stop|restart|reload}"
    exit 1
esac
exit 0