#!/bin/sh
#
# $FreeBSD: ports/net/pen/files/pen.sh.in,v 1.1 2006/09/24 14:34:36 clement Exp $
#

# PROVIDE: pen
# REQUIRE: NETWORKING SERVERS sshd
# KEYWORD: shutdown

#
# Add the following line to /etc/rc.conf to enable nanobsd-save-sshkeys:
#
# nanobsd_save_sshkeys_enable="YES"
#

. /etc/rc.subr

name=nanobsd_save_sshkeys
rcvar=`set_rcvar`

nanobsd_save_sshkeys_enable=${nanobsd_save_sshkeys_enable:-"NO"}

start_cmd="save_sshkeys"
stop_cmd="save_sshkeys"

save_sshkeys() {
	MOUNTED_RW=0
	trap "umount /cfg" 1 2 15 EXIT
	mount -ro noatime /cfg
        for FILE in /etc/ssh/ssh_host_*; do
		cmp -s $FILE /cfg/ssh/`basename ${FILE}`
		if [ $? -ne 0 ]; then
			if [ ${MOUNTED_RW} -eq 0 ]; then
			 	mount -uwo noatime /cfg
				mkdir -p /cfg/ssh
				MOUNTED_RW=1
			fi
			cp $FILE /cfg/ssh/`basename ${FILE}`
		fi
	done

	umount /cfg
	trap 1 2 15 EXIT
}


load_rc_config $name
run_rc_command "$1"

