#!/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 [ ! -s /etc/ssh/ssh_host_key ]; then
                /usr/bin/ssh-keygen -b 1024 -f /etc/ssh/ssh_host_key -N ""
        fi
        if [ ! -s /etc/ssh/ssh_host_dsa_key ]; then
                /usr/bin/ssh-keygen -d -f /etc/ssh/ssh_host_dsa_key -N ""
        fi

        action "Starting sshd:" /usr/sbin/sshd
        RETVAL=$?
        [ "$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
        ;;
  reload)
        killproc /usr/sbin/sshd -HUP
        ;;
  condrestart)
        if [ -f /var/lock/subsys/sshd ] ; then
                stop
                start
        fi
        ;;
  status)
        status /usr/sbin/sshd
        ;;
  *)
        echo "Usage: sshd {start|stop|restart|reload|condrestart|status}"
        RETVAL=1
esac
exit $RETVAL
