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

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

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

    # SoftwareChange_Request-2052
    ox_add_property io.ox/office//module/useLocalStorage true /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-2053
    ox_add_property io.ox/office//module/templatePath /opt/open-xchange/templates/documents /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-2334
    ox_add_property io.ox/office//module/standalone false /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-2610
    ox_add_property io.ox/office//module/renamedisabled false /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-2937
    ox_add_property io.ox/office//module/documentRestoreEnabled true /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-2947
    VALUE=$(ox_read_property com.openexchange.hazelcast.configuration.map.name /opt/open-xchange/etc/hazelcast/officeDocumentDirectory.properties)
    if [ "$VALUE" == "officeDocumentDirectory-1" ]; then
        ox_set_property com.openexchange.hazelcast.configuration.map.name officeDocumentDirectory-2 /opt/open-xchange/etc/hazelcast/officeDocumentDirectory.properties
    fi

    # SoftwareChange_Request-2948
    if dpkg --compare-versions "7.8.1-0" gt-nl "$2"; then
        VALUE=$(ox_read_property com.openexchange.hazelcast.configuration.map.maxIdleSeconds /opt/open-xchange/etc/hazelcast/officeDocumentDirectory.properties)
        if [ "900" = "$VALUE" ]; then
            ox_set_property com.openexchange.hazelcast.configuration.map.maxIdleSeconds 1800 /opt/open-xchange/etc/hazelcast/officeDocumentDirectory.properties
        fi
    fi

    # SoftwareChange_Request-3331
    ox_add_property io.ox/office//spreadsheet/maxFormulas 10000 /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-3589
    ox_add_property com.openexchange.capability.guard-docs false /opt/open-xchange/etc/documents.properties

    # SoftwareChange_Request-3665
    ox_add_property com.openexchange.office.maxDocumentFileSize 100 /opt/open-xchange/etc/documents.properties


}

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
