#! /bin/sh
#
# Name: ssh-delayed
# Author: SpinetiX S.A.
# Copyright: 2007 SpinetiX S.A.
# License: 2007 (c) SpinetiX S.A. This file is licensed
#          under the terms of the GNU General Public License version 2.
#          This program is licensed "as is" without any warranty of any
#          kind, whether express or implied.
#
# Copyright 1999-2003 MontaVista Software, Inc.
# Copyright 2002, 2003, 2004 Sony Corporation
# Copyright 2002, 2003, 2004 Matsushita Electric Industrial Co., Ltd.
#
### BEGIN INIT INFO
# Required-Start:
# Required-Stop:
# Should-Start:
# Should-Stop:
# Default-Start:
# Default-Stop:
# Short-Description: starts ssh but later than normal
# Description: starts the ssh daemon but later than the normal startup script
### END INIT INFO
# chkconfig: 235 98 02

# Init script information
INIT_NAME=ssh-delayed
DAEMON1=/etc/init.d/ssh
SENTINEL=/etc/ssh/generating_keys
KEY1=/etc/ssh/ssh_host_key
KEY2=/etc/ssh/ssh_host_rsa_key
KEY3=/etc/ssh/ssh_host_dsa_key

# Verify ssh init script file exists
[ "$1" = "status" ] && NFOUND=4 || NFOUND=5
test -f $DAEMON1 || exit $NFOUND

case "$1" in
    start|restart)

    # Protect against shutdown while generating keys
    use_sentinel=no
    if [ -f "$SENTINEL" ]; then
	rm -f /etc/ssh/ssh_host*key*
    fi
    if [ ! -s "$KEY1" -o ! -s "$KEY2" -o ! -s "$KEY3" ]; then
	use_sentinel=yes
    fi
    if [ "$use_sentinel" = "yes" ]; then
	touch "$SENTINEL"
	$DAEMON1 "$@"
	RET=$?
	rm -f "$SENTINEL"
	exit $RET
    fi
    ;;
esac

exec $DAEMON1 "$@"
