#!/bin/sh
# postinst script for indimail
#
# 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>
#        * <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

prefix=/usr
mandir=/usr/share/man
logdir=/var/log/svc
servicedir=/service
sysconfdir=/etc/indimail
mysqlSocket=/var/run/mysqld/mysqld.sock
cp=/bin/cp
rm=/bin/rm
mv=/bin/mv
####################################

ID=`id -u`
if [ $ID -ne 0 ] ; then
	echo "You are not root" >&2
	exit 1
fi

upgrade=0
case "$1" in
	configure)
	if [ -n "$2" ] ; then
		upgrade=1
	fi
	;;

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

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

if [ $upgrade -eq 1 ] ; then # upgrade
  (
  echo "upgrading indimail-auth package"
  ${prefix}/sbin/svctool --servicedir=${servicedir} --refreshsvc="${servicedir}/pwdlookup"
  set +e
  if [ -d /run ] ; then
    rundir=/run/svscan
  elif [ -d /var/run ] ; then
    rundir=/var/run/svscan
  else
    rundir=${servicedir}
  fi

  grep "pwdlookup/nssd.sock" ${servicedir}/pwdlookup/variables/.options >/dev/null
  if [ $? -ne 0 ] ; then
    sed -i 's{indimail/nssd.sock{indimail/pwdlookup/nssd.sock{g' ${servicedir}/pwdlookup/variables/.options
  fi
  ${prefix}/bin/svok ${servicedir}/pwdlookup >/dev/null 2>&1
  if [ $? -eq 0 -a -f ${rundir}/pwdlookup/.down ] ; then
    rm -f ${rundir}/pwdlookup/.down
    ${prefix}/bin/svc -u ${servicedir}/pwdlookup || true
  fi
  /sbin/ldconfig
  set -e
  ) >> /var/log/indimail-auth-setup.log 2>&1
  exit 0
fi

echo "Doing Post Install"
echo ""
echo " 1. Configure ${logdir} for multilog"
echo " 2. Configure ${servicedir}"
echo " 3. Configure ${sysconfdir}/nssd.conf for nssd"
echo " 4. Configure nssd service"
echo " 5. Configure pam-multi as alternative for SMTP auth"
echo ""

(
if [ -d /run ] ; then
  nssd_sock="/run/indimail/pwdlookup/nssd.sock"
elif [ -d /var/run ] ; then
  nssd_sock="/var/run/indimail/pwdlookup/nssd.sock"
else
  nssd_sock="/tmp/pwdlookup/nssd.sock"
fi
echo "installing indimail-auth package"
echo "Creating ${logdir}"
if [ ! -d ${logdir}/pwdlookup ] ; then
  /bin/mkdir -p ${logdir}/pwdlookup
  /bin/chown -R qmaill:nofiles ${logdir}/pwdlookup
fi
${prefix}/sbin/svctool --pwdlookup=$nssd_sock --threads=5 --timeout=-1 \
  --mysqlhost=localhost --mysqluser=indimail \
  --mysqlpass=ssh-1.5- --mysqlsocket=${mysqlSocket} --servicedir=${servicedir}

# turn off automatic refresh for services during first time installation
${prefix}/sbin/svctool --servicedir=${servicedir} --autorefresh="0 /service/pwdlookup"
# save variables
${prefix}/sbin/svctool --servicedir=${servicedir} --service-name=pwdlookup \
  --export-variables=${servicedir}/pwdlookup/variables/.variables --force

# update config for pam-multi link
${prefix}/sbin/svctool --config=add-alt
) >> /var/log/indimail-auth-setup.log 2>&1

exit 0
