#!/bin/sh

### BEGIN INIT INFO
# Provides:            sshd
# Default-Start:       2 3 4 5
# Default-Stop:
# Short-Description:   OpenBSD Secure Shell server
### END INIT INFO

. /lib/lsb/init-functions

DAEMON=/usr/sbin/sshd

check_privsep_dir() {
    # Create the PrivSep empty dir if necessary
    if [ ! -d /var/run/sshd ]; then
        mkdir /var/run/sshd
        chmod 0755 /var/run/sshd
    fi
}

if ! test -f $DAEMON; then
    log_info_msg "Failed to locate ssh daemon!"
    log_failure_msg2
    exit 1;
fi

case ${1} in
    start)
        check_privsep_dir
        log_info_msg "Starting OpenBSD Secure Shell server"
        $DAEMON
        evaluate_retval
        ;;

    stop)
        if ! test -f /var/run/sshd.pid; then
            log_info_msg "No sshd running"
            log_failure_msg2
            exit 1
            exit
        fi

        log_info_msg "Stopping OpenBSD Secure Shell server"
        kill -TERM $(cat /var/run/sshd.pid)
        evaluate_retval
        ;;
    restart)
        log_info_msg "Restarting OpenBSD Secure Shell server"
        kill -TERM $(cat /var/run/sshd.pid)
        $DAEMON
        evaluate_retval
        ;;
    *)
        echo "Usage: ${0} {start|stop|restart}"
        exit 1
        ;;
esac

exit 0
