#!/bin/bash
#
# 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

CONFFILES="AdminDaemon.properties Group.properties ModuleAccessDefinitions.properties mpasswd Resource.properties Sql.properties"

for FILE in $CONFFILES; do
    dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/admindaemon/$FILE /opt/open-xchange/etc/$FILE 6.20.99 -- "$@"
done
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/admindaemon/User.properties /opt/open-xchange/etc/AdminUser.properties 6.20.99 \
    -- "$@"

ofile=/opt/open-xchange/etc/admindaemon/RMI.properties
pfile=/opt/open-xchange/etc/rmi.properties
if [ -e $ofile ]; then
    oval=$(ox_read_property RMI_PORT $ofile)
    if [ -n "$oval" ]; then
       ox_set_property com.openexchange.rmi.port $oval $pfile
    fi
fi

dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/admindaemon/RMI.properties -- "$@"
dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/admindaemon/ox-admin-scriptconf.sh -- "$@"

ofile=/opt/open-xchange/etc/admindaemon/ox-admin-scriptconf.sh.dpkg-bak
pfile=/opt/open-xchange/etc/ox-scriptconf.sh
if [ -e $ofile ]; then
    oval=$(ox_read_property JAVA_OXCMD_OPTS $ofile)
    if [ -n "$oval" ]; then
       ox_set_property JAVA_OXCMD_OPTS "$oval" $pfile
    else
       ox_set_property JAVA_OXCMD_OPTS "-Djava.net.preferIPv4Stack=true" $pfile
    fi
    rm -f $ofile
fi

dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/admindaemon/plugin/hosting.properties \
    /opt/open-xchange/etc/plugin/hosting.properties 6.20.99 open-xchange-admin-plugin-hosting -- $1 \
    $(dpkg-query -W -f='${Config-Version}' open-xchange-admin-plugin-hosting)

for FILE in cache.ccf configdb.properties file-logging.properties management.properties logging.properties system.properties; do
    dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/admindaemon/$FILE -- "$@"
done

dpkg-maintscript-helper rm_conffile /etc/default/open-xchange-admin -- "$@"
dpkg-maintscript-helper rm_conffile /etc/init.d/open-xchange-admin -- "$@"

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

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

    # SoftwareChange_Request-1118
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/AdminDaemon.properties
    ox_remove_property TOOL_STORAGE $pfile

    ofile=/opt/open-xchange/etc/AdminDaemon.properties
    pfile=/opt/open-xchange/etc/rmi.properties
    if ox_exists_property BIND_ADDRESS $ofile; then
        oval=$(ox_read_property BIND_ADDRESS $ofile)
        if [ -n "$oval" ]; then
           ox_set_property com.openexchange.rmi.host $oval $pfile
        fi
        ox_remove_property BIND_ADDRESS $ofile
    fi

    # SoftwareChange_Request-1091
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/AdminUser.properties
    ox_remove_property CREATE_HOMEDIRECTORY $pfile
    ox_remove_property HOME_DIR_ROOT $pfile
    pfile=/opt/open-xchange/etc/AdminDaemon.properties
    ox_remove_property USER_PROP $pfile
    ox_remove_property GROUP_PROP $pfile
    ox_remove_property RESOURCE_PROP $pfile
    ox_remove_property RMI_PROP $pfile
    ox_remove_property SQL_PROP $pfile
    ox_remove_property MASTER_AUTH_FILE $pfile
    ox_remove_property ACCESS_COMBINATIONS_FILE $pfile

    # SoftwareChange_Request-1100
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/AdminDaemon.properties
    ox_remove_property SERVER_NAME $pfile
}

setConfigFilePermissions() {
    ox_update_permissions "/opt/open-xchange/etc/mpasswd" root:open-xchange 640
}

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

dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/Sql.properties 7.3.99 -- "$@"

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0
