#! /bin/sh
#
### BEGIN INIT INFO
# Provides: shellinabox
# Required-Start: $webservice
# Required-Stop: $webservice
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: Start the shellinabox daemon
### END INIT INFO

PID_FILE="/var/run/shellinaboxd.pid"

. /etc/rc.status
rc_reset

case "$1" in
    start)
        echo -n "starting shellinabox daemon..." 
        /usr/local/bin/shellinaboxd -v -t -s "/yast-lan/:SSH" -c /opt/webservice/config/apache -u 0 --localhost-only --background=$PID_FILE > /var/log/shellinaboxd.log 2>&1
        rc_status -v
        ;; 
    stop)
        echo -n "killing shellinabox daemon..."
        killall shellinaboxd
        rm -f /var/run/shellinaboxd*.pid 
        rc_status -v
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
        $0 stop
        $0 start

        # Remember status and be quiet
        rc_status
        ;;
    status)
        echo -n "Checking the status of shellinabox "
        if [ -e "$PID_FILE" ]
        then
            PID=`cat $PID_FILE`
            PROCS=`ps -eo pid | grep $PID | grep -v grep | wc -l`
            if [ "$PROCS" -gt "0" ]
            then
                rc_failed 0
            else
                rc_failed 3
            fi
        else
            rc_failed 3
        fi
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 1
        ;;
esac
rc_exit 
