#!/bin/sh
### BEGIN INIT INFO
# Provides:            sshd
# Required-Start:      $network $syslog
# Required-Stop:       $network $syslog
# Should-Start:
# Should-Stop:
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   OpenBSD Secure Shell server
### END INIT INFO

. /lib/lsb/init-functions

DAEMON=/usr/sbin/sshd
PIDFILE=/var/run/sshd.pid
PRIVSEPDIR=/var/run/sshd

case "$1" in
  start)
    log_init_msg "Starting OpenBSD Secure Shell server"

    if [ ! -d "$PRIVSEPDIR" ]; then
      mkdir "$PRIVSEPDIR" || error=1
    fi

    chmod 0755 "$PRIVSEPDIR" || error=1
    start_daemon "$DAEMON" || error=1

    [ -z "$error" ] && log_success_msg || log_failure_msg

    exit $error
    ;;
  stop)
    pid=`pidofproc -p "$PIDFILE" "$DAEMON"`

    if [ -n "$pid" ]; then
      log_init_msg "Stopping OpenBSD Secure Shell server"

      killproc "$DAEMON" -TERM && log_success_msg || log_failure_msg
    else
      log_init_msg "OpenBSD Secure Shell server not running" && log_success_msg
    fi
    ;;
  reload|restart)
    log_init_msg "Restarting OpenBSD Secure Shell server"

    killproc -p "$PIDFILE" "$DAEMON" -HUP && log_success_msg || log_failure_msg
    ;;
  status)
    pid=$(pidofproc -p "$PIDFILE" "$DAEMON")

    if [ "$?" -ne 0 ]; then
      echo "OpenBSD Secure Shell server not running"
    else
      echo "OpenBSD Secure Shell server running with PID: $pid"
    fi
    ;;
  *)
    echo "Usage: $0 [start|stop|reload|restart|status]"
    exit 1
    ;;
esac

exit 0
