#!/bin/sh
#130205 sfs

NAME=dropbear
DESC="Dropbear SSH server"

if test ! -e /etc/dropbear/dropbear_rsa_host_key; then
  if test -f /etc/ssh/ssh_host_rsa_key; then
    echo "Converting existing OpenSSH RSA host key to Dropbear format."
    dropbearconvert openssh dropbear \
      /etc/ssh/ssh_host_rsa_key /etc/dropbear/dropbear_rsa_host_key
  else
    echo "Generating Dropbear RSA key. Please wait."
    dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
  fi
fi
if test ! -e /etc/dropbear/dropbear_dss_host_key; then
  if test -f /etc/ssh/ssh_host_dsa_key; then
    echo "Converting existing OpenSSH RSA host key to Dropbear format."
    dropbearconvert openssh dropbear \
      /etc/ssh/ssh_host_dsa_key /etc/dropbear/dropbear_dss_host_key
  else
    echo "Generating Dropbear DSS key. Please wait."
    dropbearkey -t dss -f /etc/dropbear/dropbear_dss_host_key
  fi
fi

case "$1" in
  start)
	#test "$NO_START" = "0" || cancel 'NO_START is not set to zero.'
	echo -n "Starting $DESC: "
	#start-stop-daemon --start --quiet --pidfile /var/run/"$NAME".pid \
	#  --exec "$DAEMON" -- -d "$DROPBEAR_DSSKEY" -r "$DROPBEAR_RSAKEY" \
	#    -p "$DROPBEAR_PORT" -W "$DROPBEAR_RECEIVE_WINDOW" $DROPBEAR_EXTRA_ARGS
	#echo "$NAME."
	(`which sudo` $NAME &) && echo started
	;;
  stop)
	echo -n "Stopping $DESC: "
	#start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/"$NAME".pid
	#echo "$NAME."
	`which sudo` killall $NAME && echo stopped
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop}" >&2
	exit 1
	;;
esac

exit 0
