#
#
clear
echo "****************** DISCLAIMER *****************"
echo
echo "WARNING: This configuration provides only minimal security and assumes"
echo "the physical security of the machinee running Trinux. Like most people,"
echo "I have increased ease of use at the expense of overall security."
echo
echo "Setting up GNU Privacy Guard... See http://www.gnupg.org for more "
echo "information. "
echo
echo "Checking for existing gpg configuration"
if [ -d /.gnupg ]
then 
	echo ".gnupg already contains:"
	ls /.gnupg
	echo
	echo -n "Erase? "
	read prompt
	if [ "$prompt" = "y" ]
	then
		rm -r /.gnupg
	else
		echo "Preserving existing confg.. no guarantees what will happen"
	fi
	
else
	echo "Creating /.gnupg"
	mkdir /.gnupg
fi


echo "Now we will generate a public/private keypair for this Trinux host."
echo  
echo "If gnupg will be used non-interactively (i.e. for mailing log files,"
echo "etc.) do not enter a passphrase. When the keys are being generated, the"
echo "the best way to create the most entropy is to switch to another virtual"
echo "terminal (via ALT-Left/Right) and type random characters. Depending on"
echo "your CPU, this might take a while, so be patient. On an AMD 486-133"
echo "it took 2-3 minutes and I opened up a tcpdump session, nmap scanned"
echo "myself and did a bunch of ls -alR / to generate system activity."
echo

gpg --gen-key

echo
echo "In order to encrypt messages for someone else, you will need to add"
echo "*their* public key to the keyring that was just created. "
echo
echo "On a UNIX system this key can be created with the following command:"
echo "     gpg -a --export username > username.asc"
echo 
echo "I assume you can use your Windows PGP keys as well, but I haven't done it"
echo  
echo -n "Add a key to your key ring? "
read prompt

while [ "$prompt" = "y" ]
do
	echo -n "Where is the keyfile located, (l)ocal or (r)emote? "
	read where

	if [ "$where" = "l" ]
	then
		echo -n "Enter filename: "
		read filename

	else # load key from network

		echo -n "What protocol do you want to use, (h)ttp, (s)ssh? "			
		read proto
		
		if [ "$proto" = "s" ]
		then
			echo "Using SSH to retrieve key file..."
			echo -n "Server: "
			read server
			echo -n "Username: "
			read username
			echo -n "Filename: "
			read filename

			echo "The following command will be used:"
			echo "    scp $username@$server:~/$filename"
			echo 
			echo -n "Does this look ok? "
			read ok; [ "$ok" = "n" ] && exit 1 	
			
			scp $username@$server:~/$filename $filename
		else
			echo "Using HTTP to retrieve key file..."
			echo "Enter URL (including filename): "
			read url
			wget $url -O newkey.pgp
			filename="newkey.pgp"
		fi # ssh

	fi  # local or remote
	
echo "Adding $filename to your keyring"
gpg --import $filename
echo
echo -n "Add another key to your key ring? "
read prompt

gpg --list-keys
done


echo "Tarring up ./gnupg..."
tar cvf - /.gnupg | gzip > gpgkeys.tgz

echo "Enter floppy"
echo
read
echo "Saving keys to floppy"
echo

fmount
cp gpgkeys.tgz /floppy
ls -al /floppy
fumount

echo
echo "You can now use gpgmail to send encrypted files"
gpgmail

