#!/bin/sh

case $1 in
	'start')
		echo -n "Remounting / as read-write ... "
		/bin/mount / -o remount,rw
		echo "Done."
		if [ -f /voyage.1st ] ; then
				echo "First-time installation "
		        echo -n "Re-generating host ssh keys ... "
				rm -f /etc/ssh/ssh_host_rsa_key
				ssh-keygen -q -t rsa -f /etc/ssh/ssh_host_rsa_key -N '' || { echo "Fatal Error: Failed to generate RSA keypair" >&2; exit; }
				rm -f /etc/ssh/ssh_host_dsa_key
				ssh-keygen -q -t dsa -f /etc/ssh/ssh_host_dsa_key -N '' || { echo "Fatal Error: Failed to generate DSA keypair" >&2; exit; }
				
				depmod -ae
				
				rm -f /voyage.1st
				echo "Done."		
		fi
		
		echo -n "Removing /etc/nologin ... "
		/etc/init.d/rmnologin
		echo "Done."
		echo -n "Remounting / as read-only ... "
		/bin/mount / -o remount,ro
		echo "Done."		
		;;
	'stop')
		if [ -f /etc/voyage.conf ] ; then
        	. /etc/voyage.conf
        	/usr/local/sbin/remountrw
           	for DIR in $VOYAGE_SYSTEM_SYNCDIRS
           	do
               	echo -n "Synchronizing $DIR ... "
				cp -Rp $DIR/* /ro$DIR/
				echo "Done."
			done
			/usr/local/sbin/remountro           	
		fi
		;;
	*)
	    ;;
esac

