#!/bin/sh

sshdeinit

random_ssh_crypt_dir_name() {
	random_ssh_crypt_dir="$LOGNAME-ssh-`dd if=/dev/urandom bs=8 count=1 2>/dev/null | od -tx | head -1 | awk '{ print $2$3 }'`"
}

ssh-add -l > /dev/null 2>&1 
if [ $? -ne 0 ]; then
	echo "`basename $0`: ssh-agent doesn't seem to be running.  thank you drive through." >&2
	exit 1
fi

if [ -d $HOME/.ssh-crypt ]; then
	random_ssh_crypt_dir_name
	cattach $HOME/.ssh-crypt $random_ssh_crypt_dir
	if [ $? -ne 0 ]; then
		echo "`basename $0`: could not run cattach.  please try again.  thank you drive through." >&2
		exit 1
	fi

	rm -f $HOME/.ssh
	
	ln -s /crypt/$random_ssh_crypt_dir $HOME/.ssh
fi

for pub in $HOME/.ssh/*.pub
do
	priv="`echo $pub | sed 's/\.pub$//'`"
	if [ -f $priv ]
	then
		until ssh-add $priv
		do
			true	# nothing
		done
	fi
done
