#!/bin/bash
# postinst script for open-xchange-mobile-api-facade
#
# 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

. /opt/open-xchange/lib/oxfunctions.sh

initialConfigure() {
    # prevent bash from expanding, see bug 13316
    GLOBIGNORE='*'

    dropin_dir=/etc/systemd/system/open-xchange-mobile-api-facade.service.d
    dropin_example=limits.conf
    examples_dir=/usr/share/doc/open-xchange-mobile-api-facade/examples
    if [ ! -f ${dropin_dir}/${dropin_example} ]
    then
      install -D -m 644 ${examples_dir}/${dropin_example} ${dropin_dir}/${dropin_example}
    fi

    # Trigger a service definition/config reload
    systemctl daemon-reload &> /dev/null || :
}

postFix() {
    local version=${1%-*}
    version=${version//[-.]/}

    # prevent bash from expanding, see bug 13316
    GLOBIGNORE='*'

    # Cleanly update preview installations, too
    dropin_dir=/etc/systemd/system/open-xchange-mobile-api-facade.service.d
    dropin_example=limits.conf
    examples_dir=/usr/share/doc/open-xchange-mobile-api-facade/examples
    if [ ! -f ${dropin_dir}/${dropin_example} ]
    then
      install -D -m 644 ${examples_dir}/${dropin_example} ${dropin_dir}/${dropin_example}
    fi

    # Trigger a service definition/config reload
    systemctl daemon-reload &> /dev/null || :
}

setConfigFilePermissions() {
    ox_update_permissions "/opt/open-xchange/mobile-api-facade/osgi" open-xchange:root 750
    ox_update_permissions "/var/log/open-xchange/mobile-api-facade" open-xchange:root 750
}

case "$1" in
    configure)
        if [ -n "$2" ]
        then
            # we are in update mode, run postFix to apply fixes
            postFix "$2"
        else
            # this is the initial install
            initialConfigure
        fi
        setConfigFilePermissions
       ;;

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

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

exit 0
