#!/bin/bash
# preinst script for open-xchange-guard
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <new-preinst> `install'
#        * <new-preinst> `install' <old-version>
#        * <new-preinst> `upgrade' <old-version>
#        * <old-preinst> `abort-upgrade' <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    install)
        touch /tmp/ox_chown_tmp_$$
        chgrp open-xchange /tmp/ox_chown_tmp_$$ 2>/dev/null || /usr/sbin/groupadd --system open-xchange
        chown open-xchange /tmp/ox_chown_tmp_$$ 2>/dev/null ||
            /usr/sbin/adduser --system --home /opt/open-xchange --disabled-password --ingroup open-xchange open-xchange
        rm -f /tmp/ox_chown_tmp_$$
    ;;
    upgrade)
        # Try to stop with Wheezy start scripts because administrator may upgrade from Wheezy to Jessie, too.
        if [ -x "/etc/init.d/open-xchange-guard" ]; then
            invoke-rc.d open-xchange-guard stop || exit $?
        fi
        # Try to stop with Jessie start scripts when administrator already upgraded to Jessie.
        if [ -d /run/systemd/system ] && [[ $(deb-systemd-invoke list-unit-files open-xchange-guard.service) ]]; then
            deb-systemd-invoke stop open-xchange-guard.service >/dev/null || true
        fi
    ;;

    abort-upgrade)
    ;;

    *)
        echo "preinst 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
