#! /bin/sh
# postinst script for teapop
#
# see: dh_installdeb(1)

set -e

CONFFILE_TEA=/etc/teapop/teapop.passwd
CONFFILE_DEB=/etc/default/teapop
PACKAGE=teapop
INITSCRIPT_NAME=${PACKAGE}

# Source debconf library.
. /usr/share/debconf/confmodule

# summary of how this script can be called:
#        * <postinst> `configure' <most-recently-configured-version>
#        * <old-postinst> `abort-upgrade' <new version>
#        * <conflictor's-postinst> `abort-remove' `in-favour' <package>
#          <new-version>
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see /usr/doc/packaging-manual/
#
# quoting from the policy:
#     Any necessary prompting should almost always be confined to the
#     post-installation script, and should be protected with a conditional
#     so that unnecessary prompting doesn't happen if a package's
#     installation fails and the `postinst' is called with `abort-upgrade',
#     `abort-remove' or `abort-deconfigure'.

case "$1" in
    configure)
	# Is teapop.passwd already installed?
	if [ ! -e ${CONFFILE_TEA} ]; then
	    # no, so get ready to install ours

#	    echo "Creating teapop configuration in ${CONFFILE_TEA}..."

	    # Do we serve local users' mail?
	    db_get teapop/localmail
	    REPLACE="#"
	    if [ "$RET" = "true" ]; then
		REPLACE=""
	    fi
	    # make teapop.passwd.in into teapop.passwd
	    CONFFILE_TEA_IN=`echo ${CONFFILE_TEA} | sed -e "s/\/etc/\/usr\/share/"`.in
	    sed -e "s/###POSTINST###/$REPLACE/" < ${CONFFILE_TEA_IN} > ${CONFFILE_TEA} || echo "ERROR: Could not edit ${CONFFILE_TEA_IN} -> ${CONFFILE_TEA}"
	fi

	# Do we need to create the defaults file?
	#
	CREATE_CONF="false"
	if [ ! -f ${CONFFILE_DEB} ]; then
	    CREATE_CONF="true"

#	    echo "Creating configuration for Debian package in ${CONFFILE_DEB}..."

	    cat > ${CONFFILE_DEB} <<EOF
# *One* line in this file should be of the form 'RUNMODE=<runmode>'
# where <runmode> is one of:
#
#  inetd - if you want it to run from inetd (default);
#  standalone - if you want teapop to run standalone/as a daemon;
#  disabled - if you don't want it to run at all.
#
# You also need to edit your inetd.conf accordingly.
# It is probably easier to reconfigure this using 'dpkg-reconfigure'.
#
EOF

	fi


	# inetd or standalone?
	#
	db_get teapop/runmode
	RUNMODE=${RET}
	case "$RUNMODE" in
	    standalone|disabled)
		#
		# want to run standalone (or disabled)...
		# is it set up for inetd at the moment?
		#

#		echo "Setting up inetd.conf for standalone/disabled"
#		echo "operation of teapop..."

		GREP=0
		grep "^pop3.*/teapop" /etc/inetd.conf >/dev/null 2>&1 && GREP=1
		if [ $GREP -ne 1 ]; then
		    #
		    # no it's not
		    #
#		    echo "  ...removing any old POP3 server entries..."
		    update-inetd --group MAIL --remove pop3
		    #
		    # add a disabled line in case they change their mind
		    #
#		    echo "  ...adding (disabled) entry for teapop..."
		    update-inetd --group MAIL --add \
			'#<off>#pop3\t\tstream\ttcp\tnowait\troot\t/usr/sbin/tcpd\t/usr/sbin/teapop'
		else
		    #
		    # yes it is... disable it
		    #
#		    echo "  ...disabling teapop entry..."
		    update-inetd --group MAIL --pattern teapop --disable pop3
		fi
		
#		echo "  ...done with inetd.conf."

		if [ "z${RUNMODE}" = "zdisabled" ]; then
#		    echo "Stopping teapop daemon..."
		    /etc/init.d/${INITSCRIPT_NAME} stop
		fi

#		echo "Setting RUNMODE in /etc/defaults/teapop to '${RUNMODE}'..."
		if [ ${CREATE_CONF} = "true" ]; then
		    echo "RUNMODE=${RUNMODE}" >> ${CONFFILE_DEB}
		else
		    PERLCMD='s/^(\s*RUNMODE\s*=\s*?)\w*(.*)$/$1'${RUNMODE}'$2/'
		    perl -pi -e "${PERLCMD}" ${CONFFILE_DEB}
		fi

	    ;; # end standalone/disabled case

	    *)
		#
		# want to use inetd...
		# we can assume that /etc/inetd.conf exists as inetd comes
		# in netbase and we depend on that.
	    
#		echo "Making sure teapop daemon is not running..."
		/etc/init.d/${INITSCRIPT_NAME} stop

#		echo "Setting up inetd.conf for inetd operation of teapop..."

		# xinetd? don't bother too much - just make same update-inetd calls and
		# manage inetd.conf as normal, as xinetd debconfiscation tries to mirror
		# inetd.conf on updates

		# inetd
		#
		# is it already set up for teapop (enabled or not, and possibly tweaked)...
		#
		# can't use $? 'cos of set -e (exits before we get to look at it)
#		echo "  ...checking for teapop entry in inetd.conf..."
		GREP=0
		egrep "^[[:space:]]*(#.*)?[[:space:]]*pop3.*/teapop" /etc/inetd.conf \
		    >/dev/null 2>&1 && GREP=1
		if [ $GREP -ne 1 ]; then
		    #
		    # no it's not -- remove any entries for pop3 daemons that we might
		    # be replacing...
		    #
#		    echo "  ...removing any old POP3 server entries..."
		    update-inetd --group MAIL --remove pop3
		    	
		    # ...and add ours
		    #
#		    echo "  ...adding entry for teapop..."
		    update-inetd --group MAIL --add \
			'pop3\t\tstream\ttcp\tnowait\troot\t/usr/sbin/tcpd\t/usr/sbin/teapop'
		else
		    # yes teapop is already in inetd.conf
		    # enable?
#		    echo "  ...enabling existing entry for teapop..."
		    update-inetd --group MAIL --pattern teapop --enable pop3
		fi

#		echo "  ...done with inetd.conf."
#		echo "Setting RUNMODE in /etc/defaults/teapop to '${RUNMODE}'..."

		if [ ${CREATE_CONF} = "true" ]; then
		    echo "RUNMODE=inetd" >> ${CONFFILE_DEB}
		else
		    PERLCMD='s/^(\s*RUNMODE\s*=\s*?)\w*(.*)$/$1'${RUNMODE}'$2/'
		    perl -pi -e "${PERLCMD}" ${CONFFILE_DEB}
		fi

	    ;; # end of inetd case
	esac
#	echo "All done."
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)

    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

# try to stop it from hanging (don't want to prat about trying to
# work out what's left which FD open).
#
db_stop

exit 0
