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

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

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

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

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

    # SoftwareChange_Request-1274
    pfile=/opt/open-xchange/etc/ajp.properties
    sfile=/opt/open-xchange/etc/server.properties
    rfile=/opt/open-xchange/etc/requestwatcher.properties
    if ox_exists_property AJP_PORT $pfile; then
       oval=$(ox_read_property AJP_PORT $pfile)
       ox_set_property com.openexchange.connector.networkListenerPort "$oval" $sfile
       ox_remove_property AJP_PORT $pfile
    fi
    if ox_exists_property AJP_MAX_REQUEST_PARAMETER_COUNT $pfile; then
       oval=$(ox_read_property AJP_MAX_REQUEST_PARAMETER_COUNT $pfile)
       ox_set_property com.openexchange.connector.maxRequestParameters "$oval" $sfile
       ox_remove_property AJP_MAX_REQUEST_PARAMETER_COUNT $pfile
    fi
    if ox_exists_property AJP_JVM_ROUTE $pfile; then
       oval=$(ox_read_property AJP_JVM_ROUTE $pfile)
       ox_set_property com.openexchange.server.backendRoute "$oval" $sfile
       ox_remove_property AJP_JVM_ROUTE $pfile
    fi
    if ox_exists_property AJP_BIND_ADDR $pfile; then
       oval=$(ox_read_property AJP_BIND_ADDR $pfile)
       ox_set_property com.openexchange.connector.networkListenerHost "$oval" $sfile
       ox_remove_property AJP_BIND_ADDR $pfile
    fi
    if ox_exists_property AJP_WATCHER_ENABLED $pfile; then
       oval=$(ox_read_property AJP_WATCHER_ENABLED $pfile)
       ox_set_property com.openexchange.requestwatcher.isEnabled "$oval" $rfile
       ox_remove_property AJP_WATCHER_ENABLED $pfile
    fi
    if ox_exists_property AJP_WATCHER_PERMISSION $pfile; then
       oval=$(ox_read_property AJP_WATCHER_PERMISSION $pfile)
       ox_set_property com.openexchange.requestwatcher.restartPermission "$oval" $rfile
       ox_remove_property AJP_WATCHER_PERMISSION $pfile
    fi
    if ox_exists_property AJP_WATCHER_MAX_RUNNING_TIME $pfile; then
       oval=$(ox_read_property AJP_WATCHER_MAX_RUNNING_TIME $pfile)
       ox_set_property com.openexchange.requestwatcher.maxRequestAge "$oval" $rfile
       ox_remove_property AJP_WATCHER_MAX_RUNNING_TIME $pfile
    fi
    if ox_exists_property AJP_WATCHER_FREQUENCY $pfile; then
       oval=$(ox_read_property AJP_WATCHER_FREQUENCY $pfile)
       ox_set_property com.openexchange.requestwatcher.frequency "$oval" $rfile
       ox_remove_property AJP_WATCHER_FREQUENCY $pfile
    fi

    # SoftwareChange_Request-1120
    pfile=/opt/open-xchange/etc/ajp.properties
    if ! ox_exists_property AJP_BACKLOG $pfile; then
       ox_set_property AJP_BACKLOG 0 $pfile
    fi

    # SoftwareChange_Request-1081
    pfile=/opt/open-xchange/etc/ajp.properties
    ox_remove_property AJP_COYOTE_SOCKET_HANDLER $pfile

    # SoftwareChange_Request-1093
    pfile=/opt/open-xchange/etc/ajp.properties
    ox_remove_property AJP_CONNECTION_POOL $pfile
    ox_remove_property AJP_CONNECTION_POOL_SIZE $pfile
    ox_remove_property AJP_REQUEST_HANDLER_POOL $pfile
    ox_remove_property AJP_REQUEST_HANDLER_POOL_SIZE $pfile
    ox_remove_property AJP_MOD_JK $pfile
    ox_remove_property AJP_MAX_NUM_OF_SOCKETS $pfile
    ox_remove_property AJP_CHECK_MAGIC_BYTES_STRICT $pfile

}

case "$1" in
    configure)
        test -n "$2" && {
            # we are in update mode, run postFix to apply fixes
            postFix "$2"
        }
    ;;

    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
