#!/bin/sh

set -e

# 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>
#        * <postinst> `abort-remove'
#        * <deconfigured's-postinst> `abort-deconfigure' `in-favour'
#          <failed-install-package> <version> `removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

. /usr/share/debconf/confmodule

CONFIG_FILE=/etc/email-reminder.conf

case "$1" in
    configure)
        # Create a restricted user to run send-reminders with
        adduser --system --disabled-login --shell /bin/false --no-create-home --home /var/spool/email-reminder --gecos 'Email-reminder mailer,,,' email-reminder || true

        # The config file can include the SMTP password so hide it
        chmod 640 $CONFIG_FILE
        chown root:email-reminder $CONFIG_FILE

        # Update the debconf-managed section of the config file
        DEBCONF_START="### BEGIN DEBCONF SECTION - DO NOT EDIT ###"
        DEBCONF_END="### END DEBCONF SECTION ###"
        USER_SECTION="### Add your custom settings below this line ###"

        # Check if the debconf section already exists
        if grep -q "^${DEBCONF_START}" $CONFIG_FILE 2>/dev/null; then
            # Remove everything between the markers (inclusive) and save the rest
            sed -i "/^${DEBCONF_START}/,/^${DEBCONF_END}/d" $CONFIG_FILE
            # Also remove any trailing user section marker if present
            sed -i "/^${USER_SECTION}/d" $CONFIG_FILE
        fi

        # Append the debconf-managed section
        cat >> $CONFIG_FILE <<EOF

${DEBCONF_START}
# WARNING: This section is automatically generated from debconf settings.
# DO NOT edit this section directly or your changes will be lost.
# To change these values, run: dpkg-reconfigure email-reminder
EOF

        db_get email-reminder/send_reminders
        echo "send_reminders = ${RET}" >> $CONFIG_FILE
        db_get email-reminder/smtp_server
        echo "smtp_server = ${RET}" >> $CONFIG_FILE
        db_get email-reminder/smtp_username
        echo "smtp_username = ${RET}" >> $CONFIG_FILE
        db_get email-reminder/smtp_password
        echo "smtp_password = ${RET}" >> $CONFIG_FILE
        db_reset email-reminder/smtp_password
        db_get email-reminder/smtp_ssl
        echo "smtp_ssl = ${RET}" >> $CONFIG_FILE
        db_get email-reminder/mail_from
        echo "mail_from = ${RET}" >> $CONFIG_FILE

        cat >> $CONFIG_FILE <<EOF
${DEBCONF_END}

${USER_SECTION}
EOF

        # Create the spool directory for that user
        mkdir -p /var/spool/email-reminder
        chown email-reminder:root /var/spool/email-reminder/
        chmod 750 /var/spool/email-reminder/
    ;;

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

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

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

#DEBHELPER#

exit 0
