#!/bin/sh
# chkconfig: 2345 50 50
### BEGIN INIT INFO
# Provides:          dropbear
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start dropbear at boot time
# Description:       Enable service provided by dropbear.
# X-Start-Before:    
# X-Stop-After:      
# X-Timesys-Start-Number:  50
# X-Timesys-Stop-Number:  50
### END INIT INFO

#
# Starts dropbear sshd.
#

# Make sure the dropbearkey progam exists
[ -f /usr/bin/dropbearkey ] || exit 0

# Check for the Dropbear RSA key
if [ ! -f /etc/dropbear/dropbear_rsa_host_key ] ; then
	echo Generating RSA Key...
	mkdir -p /etc/dropbear
	/usr/bin/dropbearkey -t rsa -f /etc/dropbear/dropbear_rsa_host_key
fi

# Check for the Dropbear ecdsa key
if [ ! -f /etc/dropbear/dropbear_ecdsa_host_key ] ; then
	echo Generating ecdsa Key...
	mkdir -p /etc/dropbear
	/usr/bin/dropbearkey -t ecdsa -f /etc/dropbear/dropbear_ecdsa_host_key
fi

# Check for the Dropbear ed25519 key
if [ ! -f /etc/dropbear/dropbear_ed25519_host_key ] ; then
	echo Generating ed25519 Key...
	mkdir -p /etc/dropbear
	/usr/bin/dropbearkey -t ed25519 -f /etc/dropbear/dropbear_ed25519_host_key
fi





umask 077

start() {
 	echo -n "Starting dropbear sshd: "
	/usr/sbin/dropbear
	echo "OK"
}
stop() {
	echo -n "Stopping sshd: "
	cat /var/run/dropbear.pid | xargs kill
	echo "OK"
}
restart() {
	stop
	start
}

case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  restart|reload)
  	restart
	;;
  *)
	echo $"Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?

