#!/bin/bash
# postinst script for open-xchange-usm
#
# 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/usm.properties /opt/open-xchange/etc/usm.properties 6.20.99 -- "$@"
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/usm_json.properties /opt/open-xchange/etc/usm_json.properties 6.20.99 -- "$@"

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

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

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

    # SoftwareChange_Request-1441
    pfile=/opt/open-xchange/etc/usm.properties
    if ! ox_exists_property com.openexchange.usm.max_objects_per_folder $pfile; then
        ox_set_property com.openexchange.usm.max_objects_per_folder 65535 $pfile
    fi

    # SoftwareChange_Request-1244
    pfile=/opt/open-xchange/etc/usm.properties
    if ! ox_exists_property com.openexchange.usm.cache.session.timeout $pfile; then
        ox_set_property com.openexchange.usm.cache.session.timeout 43200 $pfile
    fi
    if ! ox_exists_property com.openexchange.usm.cache.syncstate.timeout $pfile; then
        ox_set_property com.openexchange.usm.cache.syncstate.timeout 3600 $pfile
    fi

    # SoftwareChange_Request-1574
    pfile=/opt/open-xchange/etc/usm.properties
    if ox_exists_property com.openexchange.usm.ox.post_result_pattern $pfile; then
        ox_remove_property com.openexchange.usm.ox.post_result_pattern $pfile
    fi
    if ox_exists_property com.openexchange.usm.ignore_client_data_at_slow_sync $pfile; then
        ox_remove_property com.openexchange.usm.ignore_client_data_at_slow_sync $pfile
    fi

    # SoftwareChange_Request-1628
    PFILE=/opt/open-xchange/etc/usm.properties
    NEWPROPS=( com.openexchange.usm.session_cache_check_interval com.openexchange.usm.session_cache_check_interval_log_info com.openexchange.usm.cache.session.timeout com.openexchange.usm.cache.syncstate.timeout com.openexchange.usm.force_new_sync_key_on_empty_sync com.openexchange.usm.max_objects_per_folder com.openexchange.usm.max_attachment_count_per_object com.openexchange.usm.max_attachment_size_per_object )
    DEFAULTS=( 60000 300000 43200 3600 false 65535 100 52428800 )
    for I in $(seq 1 ${#NEWPROPS[@]}); do
        NEWPROP=${NEWPROPS[$I-1]}
        DEFAULT=${DEFAULTS[$I-1]}
        if ! ox_exists_property $NEWPROP $PFILE; then
            ox_set_property $NEWPROP "$DEFAULT" $PFILE
        fi
    done
    PFILE=/opt/open-xchange/etc/usm_json.properties
    PROPS=( com.openexchange.usm.json.access_check.test_interval com.openexchange.usm.json.access_check.max_accesses com.openexchange.usm.json.access_check.refusal_interval com.openexchange.usm.json.sync.inbox_polling_interval )
    OLDDEFAULTS=( 120000 40 900000 60000 )
    NEWDEFAULTS=( 0 0 0 600000 )
    for I in $(seq 1 ${#PROPS[@]}); do
        PROP=${PROPS[$I-1]}
        VALUE=$(ox_read_property $PROP $PFILE)
        if [ -z "$VALUE" -o "${OLDDEFAULTS[$I-1]}" == "$VALUE" ]; then
            VALUE="${NEWDEFAULTS[$I-1]}"
        fi
        ox_set_property $PROP "$VALUE" $PFILE
    done

    # SoftwareChange_Request-1632
    PFILE=/opt/open-xchange/etc/usm.properties
    if ! ox_exists_property com.openexchange.usm.pass_client_host_header $pfile; then
        ox_set_property com.openexchange.usm.pass_client_host_header true $pfile
    fi

    # SoftwareChange_Request-1633
    PFILE=/opt/open-xchange/etc/usm.properties
    if ! ox_exists_property com.openexchange.usm.temp_files.timeout $pfile; then
        ox_set_property com.openexchange.usm.temp_files.timeout 7200000 $pfile
    fi
}

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
