#!/bin/bash
# postinst script for open-xchange-imap
#
# 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='*'

    PFILE=/opt/open-xchange/etc/imap.properties

    # SoftwareChange_Request-2820
    ox_add_property com.openexchange.imap.allowSORTDISPLAY false $PFILE

    # SoftwareChange_Request-3134
    ox_add_property com.openexchange.imap.fallbackOnFailedSORT false $PFILE

    # SoftwareChange_Request-3343
    ox_add_property com.openexchange.imap.rootSubfoldersAllowed "" $PFILE

    # SoftwareChange_Request-3345
    ox_add_property com.openexchange.imap.auditLog.enabled false $PFILE

    # SoftwareChange_Request-3413
    ox_add_property com.openexchange.imap.initWithSpecialUse false $PFILE

    # SoftwareChange_Request-3447
    if dpkg --compare-versions "7.8.3-0" gt-nl "$2"; then
        VALUE=$(ox_read_property com.openexchange.imap.imapSearch $PFILE)
        if [ "imap" = "$VALUE" ]; then
            ox_set_property com.openexchange.imap.imapSearch force-imap $PFILE
        fi
    fi

    # SoftwareChange_Request-3636
    if dpkg --compare-versions "7.8.3-0" gt-nl "$2"; then
        VALUE=$(ox_read_property com.openexchange.imap.ssl.protocols $PFILE)
        if [ "SSLv3 TLSv1" = "$VALUE" ]; then
            ox_set_property com.openexchange.imap.ssl.protocols "" $PFILE
        fi
    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
