mkdir /.ssh 2> /dev/null
cp /etc/tux/pkg/opensshd/keys /.ssh/authorized_keys 2> /dev/null
mkdir -p /usr/local/etc 2> /dev/null
cd /usr/local/etc
touch sshd_config

if ps | grep sshd > /dev/null
then
	echo '-- Terminating existing ssh sessions'
	killall sshd 2> /dev/null
fi

# generate host keys

#if [ -f /etc/tux/pkg/opensshd/identity.tar ]
#then
#	echo "Found identity.tar"
#	cp /etc/tux/pkg/opensshd/identity.tar /.ssh/
#	cd /.ssh
#	tar xf identity.tar
#	# rm identity.tar
#else
#	if [ -f /etc/tux/config/headless ]
#	then
#		echo "Headless box, skipping root ssh key generation"
#	else		
#		echo '  Generating public/private keypair(s) for root]"
#		ssh-keygen
#
#		echo "Adding RSA keys to /etc/tux/. Be sure to saveconfig"
#		echo "so they will get copied to the boot floppy"
#		cd /.ssh
#		tar cf /etc/tux/pkg/opensshd/identity.tar identity*
#	fi
# fi

if [ -f /usr/local/etc/ssh_host_key ]
then
	echo '-- Found existing ssh host keys'
else
	cd /usr/local/etc/
	echo '-- Generating public/private keypair(s) for this host'
	echo
	ssh-keygen -b 1024 -f ssh_host_key -N ""
	ssh-keygen -d -f ssh_host_dsa_key -N ""
	echo
	echo '*** Be sure to run save-sshkeys so they will get copied to the '
	echo '*** boot floppy, otherwise new keys to be generated each time.'
	echo
fi

[ -d /.ssh ] || mkdir /.ssh

if [ -f /etc/tux/pkg/opensshd/kserver ]
then
	echo -n '-- Retrieving authorized_keys from '
	KEYSERVER=`cat /etc/tux/pkg/opensshd/kserver` 
	echo "$KEYSERVER"

	if [ "$KEYSERVER" ]
	then
		cd /.ssh
		rm authorized_keys 2> /dev/null
		snarf $KEYSERVER - > authorized_keys
		chmod 700 authorized_keys
		cd /
	else
		echo "   Unable to contact: $KEYSERVER"
	fi
fi


echo '-- Starting sshd'
sleep 1
#/usr/local/sbin/sshd
strace /usr/local/sbin/sshd 2> /tmp/sshd_strace
sleep 1

cd /
