#!/bin/sh
REG_TO="doogie@debian.org"
pidf=/var/run/register_netscape.pid
exec 3>&1 4>&2
if [ -e $pidf ];then
echo $pidf exists!
	# Are we running?
	if [ "$(basename $(ps $(cat $pidf) 2> /dev/null | cut -b 21-|cut -f 2 -d " " | tail -n 1))" = "register_netscape" ];then
		echo "register_netscape has already gotten it's info,"
		echo "and is waiting for dpkg to finish."
		echo
		echo "Not running again."
		exit 0
	fi
fi
echo $$ > $pidf


declare -a pkg ver
declare which FILE PRINTER email listdb
declare -i numpkgs
function yesno() {
# $1 = prompt
# $2 = default(y)
	local ans def defp
	if [ "$2" ];then
		case $2 in
			Y|y)	defp="(Y/n)" def=y;;
			N|n)	defp="(y/N)" def=n;;
			*)	echo "Bad default setting!" 1>&4; exit 1;;
		esac
	else
		defp="(y/N)" def=n
	fi
	while :;do
		echo -n "$1$defp --> " 1>&3
		read ans
		case $ans in
			Y|y|N|n)	break;;
			"")		ans=$def;break;;
		esac
		echo
	done
	echo $ans | tr YN yn
}

function do_job() {
	while [ -e /var/lib/dpkg/lock ];do
		sleep 1
	done
	numpkgs=0

	(
		echo "Email: $email"
		echo "List database: $listdb"
		if [ $listdb = y ];then
			if [ -e /usr/lib/netscape/db ];then
				pkgs="$(ls /usr/lib/netscape/db)"
				for a in $pkgs;do
					echo " $(basename $a): $(cat $a)"
				done
			fi
		fi	
	) | (
		case $which in
			email)	mail -s "Automatic Netscape Registration" $REG_TO;;
			file)	cat > $FILE;;
			print)	cat | lpr -P PRINTER;;
		esac
	)
	rm $pidf
}
while :;do
	cat << _EOF_

This script will register Netscape with Debian.  Before this is done,
some information is needed.

_EOF_

	echo -n "What is your email address(none) --> "
	read email
	echo
	listdb=$(yesno "Include list of installed netscape4x packages" y)
	echo
	
	while :;do
		cat << _EOF_
There are several ways this information can be sent to Debian.  
It can be done with an email, by writing to a file, which you
can then include into an email using whatever software you
choose, or it can print directly to the printer.

1)	Send email
2)	Make text file
3)	Print directly to the printer

Q)	Quit

_EOF_
		echo -n "Which --> "
		read ans
		echo
		set -- $ans
		[ -z "$1" ] && continue
		case $1 in
			1)	which=email;break;;
			2)	echo -n "Where would you like it placed? -> "
				read FILE
				which=file
				break;;
			3)	echo -n "Where would you like it printed?(from /etc/printcap) -> "
				read PRINTER
				which=print
				break;;
			Q|q)	which=quit;break;;
		esac
	done
	[ $which = quit ] && break
	echo
	echo "Your email address: $email"
	echo -n "Include list of netscape4x packages: "
	if [ $listdb = y ];then
		echo Yes
	else
		echo No
	fi
	case $which in
		email)	echo "Send info as an email to debian.";;
		file)	echo "Place info into $FILE.";;
		print)	echo "Print info to $PRINTER."
	esac
	ans=$(yesno "Is this ok?" y)
	[ $ans = y ] && break

done
if [ $which = quit ];then

	cat << _EOM_

You have chosen to not register the installation of netscape
with Debian.  This is not an error, but it would be nice if
you would rerun register_netscape, so that the Debian personel
can report back to Netscape the number of installed copies.

_EOM_
	rm -f $pidf
	exit 0
fi

if [ -e /var/lib/dpkg/lock ];then
	cat << _EOM_

I will now fork into the background, and wait for dpkg to finish,
before doing as you requested.

_EOM_
	do_job &
else
	do_job
fi
