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

set -e
prefix=/usr
rm=/bin/rm
chown=/bin/chown
logdir=/var/log/svc
servicedir=/service
libexecdir=${prefix}/libexec

# 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

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 [ ! -x ${prefix}/sbin/minisvc ] ; then
  exit 0
fi
if [ $upgrade -eq 1 ] ; then # upgrade
  # refresh lcd-daemon services
  for j in lcd-daemon notify-desktop
  do
    ${prefix}/sbin/minisvc --servicedir=${servicedir} --refreshsvc="${servicedir}/$j" --silent
  done
  # start lcd-daemon, notify-desktop services after upgrade
  if [ -d /run ] ; then
    rundir=/run/svscan
  elif [ -d /var/run ] ; then
    rundir=/var/run/svscan
  else
    rundir=${servicedir}
  fi
  set +e
  for j in lcd-daemon notify-desktop
  do
    ${prefix}/bin/svok ${servicedir}/$j >/dev/null 2>&1
    if [ $? -eq 0 -a -f ${rundir}/$j/.down ] ; then
      ${prefix}/bin/svc -u ${servicedir}/$j
    fi
    rm -f ${rundir}/$j/.down
  done
  set -e
  for j in lcd-daemon notify-desktop
  do
    ${prefix}/sbin/minisvc --servicedir=${servicedir} --service-name=$j \
      --export-variables=${servicedir}/$j/variables/.variables --force --silent
  done
  exit 0
fi

for j in lcd-daemon notify-desktop
do
  # create lcd-daemon, notify-desktop service in down state
  ${libexecdir}/lcd-daemon/create_service --servicedir=${servicedir} --"$j"
  ${prefix}/sbin/minisvc --servicedir=${servicedir} --service-name=$j \
      --export-variables=${servicedir}/$j/variables/.variables --force --silent
  touch ${servicedir}/$j/down
done
if [ -x /usr/bin/svps ] ; then
  svps -h >/dev/null 2>&1
fi
exit 0
