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

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>
#        * <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)

    ;;

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

    ;;

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

if [ ! -e /etc/zmailer/sm.conf ]; then
	cd /etc/zmailer && ln -s proto/sm.conf;
fi

if [ ! -e /etc/zmailer/scheduler.conf ]; then
	cd /etc/zmailer && ln -s proto/scheduler.conf;
fi

if [ ! -e /etc/zmailer/smtpserver.conf ]; then
	cd /etc/zmailer && ln -s proto/smtpserver.conf;
fi

if [ ! -e /etc/zmailer/router.cf ]; then
	cd /etc/zmailer && ln -s cf/TELE-FI.cf router.cf;
fi

if [ ! -e /etc/zmailer/db/fqdnaliases ]; then 
	cd /etc/zmailer/db && ln -s proto/fqdnaliases;
fi

if [ ! -e /etc/zmailer/db/localnames ]; then
	cd /etc/zmailer/db && ln -s proto/localnames;
fi

if [ ! -e /etc/zmailer/db/routes ]; then
	cd /etc/zmailer/db && ln -s proto/routes;
fi

if [ ! -e /etc/zmailer/db/smtp-policy.relay ]; then
	cd /etc/zmailer/db && ln -s proto/smtp-policy.relay;
fi

if [ ! -e /etc/zmailer/db/smtp-policy.src ]; then
	cd /etc/zmailer/db && ln -s proto/smtp-policy.src;
fi

cd

# We need to ask about /etc/mailname, if that file doesn't exist. ZMailer
# has been configured to use /etc/mailname instead of its own /etc/mail.conf
# already. (Debian Policy)
if [ ! -f /etc/mailname ]; then
	echo "The file /etc/mailname should contain the domain- or hostname";
	echo "visible on all outgoing mail and news messages. Currently,";
	echo "that file isn't present. Please enter the mail name of your";
	echo -n "system: ";
	read mailname;
	echo $mailname > /etc/mailname;
fi

# Check the localnames file
if [ ! -f /etc/zmailer/db/localnames ]; then	# it doesn't exist yet
	echo -n `awk '{print $1}' /etc/mailname` > /etc/zmailer/db/localnames;
	echo -n "		" >> /etc/zmailer/db/localnames;
	echo `awk '{print $1}' /etc/mailname` >> /etc/zmailer/db/localnames;
fi

# ZMailer should use /etc/aliases instead of the default $(MAILSHARE)/db/aliases
# (Debian Policy)
if [ -f /etc/aliases ]; then		# /etc/aliases does exist
	rm -f /etc/zmailer/db/aliases;
fi
(cd /etc/zmailer/db && ln -s ../../aliases)

# We need to check if all necessairy aliases are present
for i in postmaster postoffice MAILER-DAEMON postmast nobody abuse; do
	if ! grep -q "^$i:" /etc/aliases; then
		echo "Adding Entry for $i in /etc/aliases"
		echo -e -n "\n$i: root" >>/etc/aliases
	fi
done
echo >>/etc/aliases

# Now we can update the aliases database
echo "Updating aliases database using newaliases..."
/usr/lib/zmailer/zmailer newaliases

# Running the policy-builder
echo "In /etc/zmailer/db/smtp-policy.src, you'll find relay and spam"
echo "protection, check it out please before turning on ZMailer."
echo "After editing that file, you need to run this program:"
echo "    /usr/lib/zmailer/policy-builder.sh"
echo "I'll run it now to initialize the system."
/usr/lib/zmailer/policy-builder.sh

# Fixing permissions
chmod 750 /var/spool/postoffice/deferred
chmod 750 /var/spool/postoffice/freezer
chmod 750 /var/spool/postoffice/postman
chmod 777 /var/spool/postoffice/public 
chmod o+t /var/spool/postoffice/public
chmod 750 /var/spool/postoffice/queue
chmod 777 /var/spool/postoffice/router
chmod o+t /var/spool/postoffice/router
chmod 755 /var/spool/postoffice/transport

# Now we can disable the service in inetd.conf, if it was still enabled.
update-inetd --disable smtp

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

#DEBHELPER#

# Okay we're finished.
echo
echo "ZMailer package for Debian has been configured."
echo "I strongly suggest you read /usr/doc/zmailer/README.Debian before"
echo "trying to run the ZMailer Server."
echo
echo "You also need to edit /etc/init.d/zmailer before it will start."
echo "When done editing the configuration in the way you want it,"
echo "use   /etc/init.d/zmailer start   to start ZMailer."
echo 
echo "Also, don't forget to add a group called zmailer before running"
echo "ZMailer, using the command 'addgroup zmailer'"
echo
echo "For now, press enter to continue..."
echo
read keypress



exit 0






