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

DIVERTFILES="outlook2003.de_DE.properties outlook2003.en_EN.properties outlook2003.fr_FR.properties outlook2003.nl_NL.properties"

for FILE in ${DIVERTFILES}; do
    if dpkg-divert --list | grep $FILE; then
        dpkg-divert --package open-xchange --rename --remove /opt/open-xchange/importCSV/${FILE}
    fi
done

dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/filestorage.properties \
      /opt/open-xchange/etc/filestorage.properties 6.20.99 open-xchange-file-storage-config \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-file-storage-config)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/folderjson.properties \
      /opt/open-xchange/etc/folderjson.properties 6.20.99 open-xchange-folder-json \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-folder-json)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/common/i18n.properties \
      /opt/open-xchange/etc/i18n.properties 6.20.99 open-xchange-i18n \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-i18n)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/mail-push.properties \
      /opt/open-xchange/etc/mail-push.properties 6.20.99 open-xchange-push \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-push)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/messaging.properties \
      /opt/open-xchange/etc/messaging.properties 6.20.99 open-xchange-messaging \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-messaging)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/publications.properties \
      /opt/open-xchange/etc/publications.properties  6.20.99 open-xchange-publish-basic \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-publish-basic)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/push.properties \
      /opt/open-xchange/etc/push-udp.properties 6.20.99 open-xchange-push-udp \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-push-udp)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/secret.properties \
      /opt/open-xchange/etc/secret.properties 6.20.99 open-xchange-secret \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-secret)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/secrets \
      /opt/open-xchange/etc/secrets 6.20.99 open-xchange-secret \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-secret)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/threadpool.properties \
      /opt/open-xchange/etc/threadpool.properties  6.20.99 open-xchange-threadpool \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-threadpool)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/meta/ui.yml \
      /opt/open-xchange/etc/meta/ui.yml 6.20.99 open-xchange-settings-extensions \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-settings-extensions)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/settings/themes.properties \
      /opt/open-xchange/etc/settings/themes.properties 6.20.99 open-xchange-settings-extensions \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-settings-extensions)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/settings/ui.properties \
      /opt/open-xchange/etc/settings/ui.properties 6.20.99 open-xchange-settings-extensions \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-settings-extensions)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/management.properties \
      /opt/open-xchange/etc/management.properties 6.20.99 open-xchange-management \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-management)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/templating.properties \
      /opt/open-xchange/etc/templating.properties 6.20.99 open-xchange-templating \
      -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-templating)

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

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

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

setConfigFilePermissions() {
    ox_update_permissions "/var/log/open-xchange" open-xchange:root 750
    ox_update_permissions "/opt/open-xchange/osgi" open-xchange:root 750
    PROTECT="configdb.properties mail.properties management.properties oauth-provider.properties secret.properties secrets sessiond.properties"
    for FILE in $PROTECT
    do
        ox_update_permissions "/opt/open-xchange/etc/$FILE" root:open-xchange 640
    done
    ox_update_permissions "/opt/open-xchange/etc/ox-scriptconf.sh" root:root 644
}

case "$1" in
    configure)
        test -n "$2" && {
            # we are in update mode, run postFix to apply fixes
            postFix "$2"
        }
        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
