#!/bin/ash
#
# DropBear SSH

. /etc/rc.d/init.d/functions

DSSKEY=/etc/dropbear/dropbear_dss_host_key
RSAKEY=/etc/dropbear/dropbear_rsa_host_key
PIDFILE=/var/run/dropbear.pid

case "$1" in
start)
	if [ ! -r "$DSSKEY" ]; then
		echo -n "Generating DSS host key: "
		dropbearkey -t dss -f "$DSSKEY" >/dev/null 2>&1
		check_status
	fi
	if [ ! -r "$RSAKEY" ]; then
		echo -n "Generating RSA host key: "
		dropbearkey -t rsa -f "$RSAKEY" >/dev/null 2>&1
		check_status
	fi
	if [ -r "$PIDFILE" ]; then
		echo "Service dropbear already running."
	else
		echo -n "Starting SSH server: "
		dropbear
		check_status
	fi
	;;
stop)
	if [ -r "$PIDFILE" ]; then
		echo -n "Stopping dropbear SSH server: "
		kill `cat "$PIDFILE"`
		check_status
	else
		echo "Service dropbear not running."
	fi
	;;
restart)
	$0 stop
	$0 start
	;;
status)
	if [ -r "$PIDFILE" ]; then
		echo "Service dropbear running (PID $(cat "$PIDFILE"))."
	else
		echo "Service dropbear not running."
	fi
	;;
*)
	echo "Usage: $0 {start|stop|restart|status}"
	exit 1
esac
