#!/bin/bash

# Init file for OpenSSH server daemon
#
# chkconfig: 2345 55 25
# description: OpenSSH server daemon
#
# processname: sshd
# config: /etc/ssh/ssh_host_key
# config: /etc/ssh/ssh_host_key.pub
# config: /etc/ssh/ssh_random_seed
# config: /etc/ssh/sshd_config
# pidfile: /var/run/sshd.pid

# source function library
. /etc/rc.d/init.d/functions

RETVAL=0

function start()
{
	if [ ! -f /etc/ssh/ssh_host_key -o ! -s /etc/ssh/ssh_host_key ]; then
		/usr/bin/ssh-keygen -b 1024 -f /etc/ssh/ssh_host_key -N ''
	fi
	if [ ! -f /etc/ssh/ssh_host_dsa_key -o ! -s /etc/ssh/ssh_host_dsa_key ]; then
		/usr/bin/ssh-keygen -d -f /etc/ssh/ssh_host_dsa_key -N ''
	fi

	echo -n "Starting sshd:"
	daemon /usr/sbin/sshd
	RETVAL=$?
	echo
	[ "$RETVAL" = 0 ] && touch /var/lock/subsys/sshd
}

function stop()
{
	echo -n "Stopping sshd:"
	killproc /usr/sbin/sshd
	RETVAL=$?
	echo
	[ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/sshd
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart)
        stop
        start
        ;;
  condrestart)
	if [ -f /var/lock/subsys/sshd ] ; then
        	stop
        	start
	fi
	;;
  status)
        status /usr/sbin/sshd
        ;;
  *)
	echo "Usage: sshd {start|stop|restart|condrestart|status}"
	RETVAL=1
esac
exit $RETVAL
