#!/bin/sh
# /etc/init.d/dropbear : Start, stop and restart SSH server, at
# boot time or with the command line.

NAME=Dropbear
DESC="SSH server"
DAEMON=/usr/bin/dropbear
OPTIONS="-w -g -b /etc/dropbear/banner"
PIDFILE=/var/run/dropbear.pid

case "$1" in
  start)
    # We need rsa and dss host key file to start dropbear.
    if [ ! -f /etc/dropbear/dropbear_rsa_host_key ] ; then
      echo "Generating $NAME rsa key... "
      dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
    fi
    if [ ! -f /etc/dropbear/dropbear_dss_host_key ] ; then
      echo "Generating $NAME dss key... "
      dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key
    fi
    if [ -f $PIDFILE ] ; then
      echo "$NAME already running."
      exit 1
    fi
    echo "Starting $DESC: $NAME... "
    $DAEMON $OPTIONS
    ;;
  stop)
    if [ ! -f $PIDFILE ] ; then
      echo "$NAME is not running."
      exit 1
    fi
    echo "Stopping $DESC: $NAME... "
    kill `cat $PIDFILE`
    ;;
  restart)
    if [ ! -f $PIDFILE ] ; then
      echo "$NAME is not running."
      exit 1
    fi
    echo "Restarting $DESC: $NAME... "
    kill `cat $PIDFILE`
    sleep 2
    $DAEMON $OPTIONS
    ;;
  status)
    if [ -f $PIDFILE ]; then
	echo "$NAME is running."
	exit 0
    else
    	echo "$NAME not running."
    	exit 1
    fi
  ;;
  *)
    echo ""
    echo -e "\033[1mUsage:\033[0m /etc/init.d/`basename $0` [start|stop|restart|status]"
    echo ""
    exit 1
    ;;
esac

exit 0
