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

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

    # SoftwareChange_Request 3105
    # Installing open-xchange-guard created a backup so we are able to take
    # over the previous config params to the new location.
    old_core_file_back=/opt/open-xchange/etc/guard.properties.move
    file_properties=/opt/open-xchange/etc/guard-file.properties
    key="com.openexchange.guard.storage.file.uploadDirectory" 

    if [ -e $old_core_file_back ] && [ -e $file_properties ]; then
        #if property is uncommented
        if $(ox_exists_property ${key} ${old_core_file_back}); then
            value=$(ox_read_property ${key} ${old_core_file_back})
            #and property has a value
            if [ -n "${value}" ]; then
                ox_comment ${key} remove ${file_properties}
                ox_set_property ${key} "${value}" ${file_properties}
            fi
        fi
        # after open-xchange-guard and open-xchange-storage-* are installed we
        # can clean up as properties have been moved
        rm ${old_core_file_back}
    fi

}

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

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

}

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

    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
