#!/bin/sh -e

ACTION=probe
case "$1" in
	--help|-h)
		echo "$0 [--show] [--exec]"
		exit 0
		;;
	--show)
		ACTION=show
		;;
	--exec)
		ACTION=exec
		;;
esac

# read old config
. /etc/default/oidentd

CLEAN=yes
SPOOF=no
SPOOFPRIV=no
HIDE=no
MASQ=no
NEW_OPTIONS="$OIDENT_OPTIONS"
case "$OIDENT_OPTIONS" in
	*-A*)
		SPOOFPRIV=yes
		NEW_OPTIONS="`echo $NEW_OPTIONS|sed -e 's/-A//g'`"
		;;
esac
case "$OIDENT_OPTIONS" in
	*-s*)
		test -f /etc/identd.spoof &&
		grep -v '^#' /etc/identd.spoof | grep -qv '^$' &&
		SPOOF=normal &&
		CLEAN=no &&
		NEW_OPTIONS="`echo $NEW_OPTIONS|sed -e 's/-s//g'`"
		;;
esac
case "$OIDENT_OPTIONS" in
	*-S*)
		# all users but those in identd.spoof are allowed to spoof
		SPOOF=reverse
		NEW_OPTIONS="`echo $NEW_OPTIONS|sed -e 's/-S//g'`"
		;;
esac
case "$OIDENT_OPTIONS" in
	*-N*)
		# users are allowed to hide them
		CLEAN=no
		HIDE=yes
		NEW_OPTIONS="`echo $NEW_OPTIONS|sed -e 's/-N//g'`"
		;;
esac
case "$OIDENT_OPTIONS" in
	*-m*)
		# handle new location of masquarading config file
		test -f /etc/oidentd.users &&
		grep -v '^#' /etc/oidentd.users | grep -qv '^$' &&
		case "$ACTION" in
		show)
			echo "### Please move /etc/oidentd.users to /etc/oidentd_masq.conf"
			echo
			;;
		exec)
			mv /etc/oidentd.users /etc/oidentd_masq.conf
			;;
		esac &&
		CLEAN=no
		;;
esac
case "$OIDENT_OPTIONS" in
	*-F*)
		CLEAN=no
		NEW_OPTIONS="`echo $NEW_OPTIONS|sed -e 's/-F/-f/g'`"
		;;
esac
case "$OIDENT_OPTIONS" in
	*-x*)
		CLEAN=no
		NEW_OPTIONS="`echo $NEW_OPTIONS|sed -e 's/-x/-r/g'`"
		;;
esac

if [ "$SPOOF" = reverse -o "$HIDE" = yes ]; then
	{
		[ "$SPOOF" = reverse ] &&
			echo "		allow spoof"
		[ "$SPOOFPRIV" = yes ] &&
			echo "		allow spoof_privport"
		[ "$HIDE" = yes ] &&
			echo "		allow hide"
		echo
	} | case "$ACTION" in
	show)
		echo "### Please add these lines to the default section of your /etc/oidentd.conf:"
		cat
		;;
	exec)
		ln /etc/oidentd.conf /etc/oidentd.conf.old &&
		perl -pe 'if(/^\s+default/){print;system("cat");$_=""};' /etc/oidentd.conf > /etc/oidentd.conf.new &&
		mv /etc/oidentd.conf.new /etc/oidentd.conf
		;;
	esac
fi
if [ "$SPOOF" != no ]; then
	USER=undef
	grep -v '^#' /etc/identd.spoof | grep -v '^$' | {
	# convert old to new format
	  IFS=:
	  read USER REPLY
	  while [ -n "$USER" ]; do
		echo "user \"$USER\" {"
		echo "	default {"
		if [ -z "$REPLY" ]; then
			if [ "$SPOOF" = reverse ]; then
				echo "		deny spoof"
			else
				echo "		allow spoof"
				[ "$SPOOFPRIV" = yes ] && echo "		allow spoof_privport"
			fi
		else
			echo "		force reply \"$REPLY\""
		fi
		echo "	}"
		echo "}"
		echo
		read USER REPLY
	done } | case "$ACTION" in
	show)
		echo "### Please add these lines to your /etc/oidentd.conf:"
		cat
		echo "### afterwards, you may delete /etc/identd.spoof."
		echo
		;;
	exec)
		cat >> /etc/oidentd.conf
		rm /etc/identd.spoof
		;;
	esac
fi

if [ "$OIDENT_OPTIONS" != "$NEW_OPTIONS" ]; then
	case "$ACTION" in
	show)
		echo "### Edit /etc/default/oidentd to contain:"
		echo "OIDENT_OPTIONS=\"$NEW_OPTIONS\""
		echo
		;;
	exec)
		cat /etc/default/oidentd |
		sed -e 's/^OIDENT_OPTIONS=.*$/OIDENT_OPTIONS="'$NEW_OPTIONS'"/' > /etc/default/oidentd.new
		mv /etc/default/oidentd.new /etc/default/oidentd
		;;
	esac
fi

if [ "$CLEAN" = yes ]; then
	if [ "$ACTION" = show ]; then
		echo "You already have a new oidentd config."
	fi
	exit 0
fi

case "$ACTION" in
probe)
	echo "Your new oidentd won't work with the old configuration files."
	echo "please see '/usr/sbin/oidentdconfig --show' to fix it."
	exit 1
	;;
show)
	echo "### you may run '/usr/sbin/oidentdconfig --exec' to do all this automatically"
	exit 1
	;;
exec)
	echo "your oidentd config should work with the new oidentd now,"
	echo "but please have a look at it for yourself before starting the daemon."
	exit 0
	;;
esac
