#!/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
qmaildir=/var/indimail
mandir=${prefix}/share/man
shareddir=${prefix}/share/indimail
logdir=/var/log/svc
servicedir=/service
qsysconfdir=/etc/indimail
libexecdir=/usr/libexec/indimail
cp=/bin/cp
rm=/bin/rm
####################################

setperm()
{
  local user="$1"; shift
  local group="$1"; shift
  local mode="$1"; shift
  local file="$1"; shift
  /usr/sbin/dpkg-statoverride --list "$file" >/dev/null && return 0
  /usr/sbin/dpkg-statoverride --update --add "$user" "$group" "$mode" "$file"
}

fixperms()
{
  if [ $# -eq 3 ] ; then # directory
  #%dir %attr(0555,root,indimail) /var/indimail/sbin
    lin=$2
    file=$3
    if [ ! -d $file ] ; then
      return 0
    fi
  elif [ $# = 2 ] ; then # file
  #%attr(0555,root,qmail) /var/indimail/bin/rsmtp
    lin=$1
    file=$2
    if [ ! -f $file ] ; then
      return 0
    fi
  fi
  perm=`echo $lin|cut -d\( -f2 | cut -d, -f1`
  perm_f1=`echo $perm | cut -c1`
  own=`echo $lin|cut -d\( -f2 | cut -d, -f2`
  grp=`echo $lin|cut -d\( -f2 | cut -d, -f3|cut -d\) -f1`
  if [ -f $file ] ; then
    if [ $perm_f1 -eq 0 ] ; then # 4755, 0644
      /bin/chown $own:$grp $file
      /bin/chmod $perm $file
    else
      echo $file
      if [ -f /usr/sbin/dpkg-statoverride ] ; then
        setperm $own $grp $perm $file
      else
        /bin/chown $own:$grp $file
        /bin/chmod $perm $file
      fi
    fi
  elif [ -d $file ] ; then
    /bin/chown $own:$grp $file
    /bin/chmod $perm $file
  fi
}

log_it() {
  echo "$(echo  ""|tai64n|tai64nlocal)$*"
}

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#

#
# We need to do this for debian. SIGH!!
#
if [ -f ${qsysconfdir}/daemontools.perms ] ; then
	(
	/bin/grep -v "^#" ${qsysconfdir}/daemontools.perms | while read line
	do
		if [ " $line" = " " ] ; then
			continue
		fi
		fixperms $line
	done
	) > ${qsysconfdir}/daemontools-stat.override
	if [ ! -s ${qsysconfdir}/daemontools-stat.override ] ; then
		/bin/rm -f ${qsysconfdir}/daemontools-stat.override
	fi
fi

if [ -d /run ] ; then
	rundir=/run
elif [ -d /var/run ] ; then
	rundir=/var/run
else
	rundir=""
fi
if [ -d ${prefix}/lib/tmpfiles.d -a -n "${rundir}" ] ; then
  if [ ! -f ${prefix}/lib/tmpfiles.d/svscan.conf ] ; then
    echo "d ${rundir}/svscan 0755 root root" > ${prefix}/lib/tmpfiles.d/svscan.conf
  else
    set +e
    echo "d {rundir}/svscan 0755 root root" | cmp -s ${prefix}/lib/tmpfiles.d/svscan.conf 2>/dev/null
    if [ $? -ne 0 ] ; then
      echo "d ${rundir}/svscan 0755 root root" > ${prefix}/lib/tmpfiles.d/svscan.conf
    fi
    set -e
  fi
fi

if [ $upgrade -eq 1 ] ; then # upgrade
  set +e
  (
  log_it "checking if system boot scripts need upgrade"
  if [ -f /etc/init.d/svscan ] ; then
    cmp -s ${prefix}/bin/qmailctl /etc/init.d/svscan >/dev/null 2>&1 || \
      (${cp} ${prefix}/bin/qmailctl /etc/init.d/svscan && log_it "installed new /etc/init.d/svscan")
  fi
  if [ -x /bin/systemctl -a -d /lib/systemd/system ] ; then
    cmp -s ${shareddir}/boot/systemd /lib/systemd/system/svscan.service >/dev/null 2>&1 || \
      (${cp} ${shareddir}/boot/systemd /lib/systemd/system/svscan.service &&
      /bin/systemctl daemon-reload && log_it "installed new svscan service")
  elif [ -x /bin/systemctl -a -d /usr/lib/systemd/system ] ; then
    cmp -s ${shareddir}/boot/systemd /usr/lib/systemd/system/svscan.service >/dev/null 2>&1 || \
      (${cp} ${shareddir}/boot/systemd /usr/lib/systemd/system/svscan.service &&
      /bin/systemctl daemon-reload && log_it "installed new svscan service")
  elif [ -f /sbin/initctl -a -d /etc/init ] ; then
    cmp -s ${shareddir}/boot/upstart /etc/init/svscan.conf >/dev/null 2>&1 || \
      (${cp} ${shareddir}/boot/upstart /etc/init/svscan.conf && log_it "insalled new svscan.conf")
  elif [ -d /etc/event.d ] ; then
    cmp -s ${shareddir}/boot/upstart /etc/event.d/upstart >/dev/null 2>&1 || \
      (${cp} ${shareddir}/boot/upstart /etc/event.d && log_it "installed new upstart config")
  elif [ -d /System/Library/LaunchDaemons ] ; then
    cmp -s ${shareddir}/boot/indimail.plist /System/Library/LaunchDaemons >/dev/null 2>&1 || \
      (${cp} ${shareddir}/boot/indimail.plist /System/Library/LaunchDaemons && log_it "installed new indimail.plist")
  fi
  # restore svscan service if we find .svscan.down
  if [ -f ${servicedir}/.svscan.down ] ; then
    ${rm} -f ${servicedir}/.svscan.down
    log_it "Starting svscan"
    if test -f /etc/init/svscan.conf
    then
      /sbin/initctl emit qmailstart
    elif test -f /etc/event.d/svscan
    then
      /sbin/initctl emit qmailstart
    elif test -f /etc/systemd/system/multi-user.target.wants/svscan.service
    then
      /bin/systemctl start svscan
    elif test -x ${prefix}/sbin/initsvc
    then
      if [ -x /etc/init.d/svscan ] ; then
        ${prefix}/sbin/initsvc -status || /etc/init.d/svscan start
      else
        ${prefix}/sbin/initsvc -status
      fi
      ${prefix}/sbin/initsvc -on 
    else
      if [ -x /etc/init.d/svscan ] ; then
        /etc/init.d/svscan start
      fi
      /bin/grep "^#SV:" /etc/inittab |/bin/grep svscan |/bin/grep respawn >/dev/null
      if [ $? -eq 0 ] ; then
        sed -i 's{^#SV:{SV:{' /etc/inittab
        if [ $? -eq 0 ] ; then
          /sbin/init q
        fi
      fi
    fi
	${prefix}/bin/svps -r
  fi
  ) >> /var/log/daemontools-setup.log 2>&1
  exit 0
fi
(
if [ ! -d ${logdir} ] ; then
  /bin/mkdir ${logdir}
fi
/bin/chown -R qmaill:nofiles ${logdir}
if [ ! -d ${servicedir} -a ! -L ${servicedir} ] ; then
  mkdir -p ${servicedir}
fi
log_it "adding svscan startup"
${prefix}/sbin/minisvc --config=add-boot
# svscanlog service
${prefix}/sbin/minisvc --svscanlog --servicedir=${servicedir} --scanint=60 > /dev/null || true
# save variables
${prefix}/sbin/minisvc --servicedir=${servicedir} --service-name=.svscan \
  --export-variables=${servicedir}/.svscan/variables/.variables --force

if [ -x /bin/systemctl ] ; then
  /bin/systemctl enable svscan.service
fi
if [ -x ${libexecdir}/instcheck.daemontools ] ; then
  ${libexecdir}/instcheck.daemontools > /dev/null 2>&1
fi
) >> /var/log/daemontools-setup.log 2>&1
exit 0
