#!/bin/sh -e

# config script for teapop
#
# nwp@lemon-computing.com, 07/2001
#

# AIMS: 1) postinst will have to check for $CONFFILE_TEA itself
#       2) if $CONFFILE_TEA does not exist, set debconf
#          variable teapop/localmail to indicate what we
#          should put in teapop.passwd
#       3) set debconf variable teapop/runmode to identd
#          or daemon.
#       4) postinst can check for $CONFFILE_DEB itself
#       5) set inetd-active to indicate whether or not inetd entry
#          should be active


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

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

db_subst teapop/already-passwd CONFFILE_TEA ${CONFFILE_TEA}
db_subst teapop/runmode CONFFILE_DEB ${CONFFILE_DEB}

# Whaddya want?

# only ask about things that go in teapop.passwd if they
# don't already have one...
db_set teapop/already-passwd "false"
if [ -e ${CONFFILE_TEA} ]; then
    # don't ask, but tell user where to find config
    db_input low teapop/already-passwd || true
    db_set teapop/already-passwd "true"
else
    # ask (currently only "serve local mail?")
    db_input high teapop/localmail || true
fi

# Do they already have a defaults file?
if [ -f $CONFFILE_DEB ]; then
    # defaults file present, see what it says (remember it may fail, hence || true)
    RUNMODE=`egrep "^ *RUNMODE.*="  ${CONFFILE_DEB} | sed -e 's/.*=[[:space:]]*//'` || true
    case "${RUNMODE}" in
	standalone|disabled)
	    ;;
	*)
	    # default is inetd
	    # setting it here will remove garbage from settings in defaults
	    # file.
	    #
	    RUNMODE="inetd"

	    # we don't know when we're being run, so don't go checking
	    # the current inetd state on an upgrade.
	    
	    # and if they haven't set RUNMODE in the default file, they
	    # have done it wrong.
	    ;;
    esac

    # This makes the admin changes (if any) in defaults file
    # authoritative.
    #
    db_set teapop/runmode ${RUNMODE}
fi

db_go

# We do this because one of the following holds:
# * defaults file not present -- assume fresh install;
# * we're reconfiguring and have just set this from defaults
#   file to give correct default;
# * on upgrade, we've just set this from defaults file and
#   they probably won't see it. If they do, who cares?
#
db_input medium teapop/runmode || true

# Postinst can now trust the runmode setting, and set
# it in the defaults file accordingly.

db_go
