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

    # SoftwareChange_Request-1954
    VALUE=$(ox_read_property com.openexchange.mail.filter.useUTF7FolderEncoding $PFILE)
    ox_set_property com.openexchange.mail.filter.useUTF7FolderEncoding "$VALUE" $PFILE

    # SoftwareChange_Request-3843
    prefer_gssapi=$(ox_read_property com.openexchange.mail.filter.preferGSSAPI ${PFILE})
    ox_remove_property com.openexchange.mail.filter.preferGSSAPI $PFILE
    if [ "${prefer_gssapi}" = "true" ]
    then
      ox_add_property com.openexchange.mail.filter.preferredSaslMech GSSAPI $PFILE
    else
      ox_add_property com.openexchange.mail.filter.preferredSaslMech "" $PFILE
    fi

    # SoftwareChange_Request-3987
    OLDNAMES=( SIEVE_LOGIN_TYPE SIEVE_CREDSRC SIEVE_SERVER SIEVE_PORT SCRIPT_NAME SIEVE_AUTH_ENC NON_RFC_COMPLIANT_TLS_REGEX TLS VACATION_DOMAINS )
    NEWNAMES=( com.openexchange.mail.filter.loginType com.openexchange.mail.filter.credentialSource com.openexchange.mail.filter.server com.openexchange.mail.filter.port com.openexchange.mail.filter.scriptName com.openexchange.mail.filter.authenticationEncoding com.openexchange.mail.filter.nonRFCCompliantTLSRegex com.openexchange.mail.filter.tls com.openexchange.mail.filter.vacationDomains )
    for I in $(seq 1 ${#OLDNAMES[@]})
    do
        OLDNAME=${OLDNAMES[$I-1]}
        NEWNAME=${NEWNAMES[$I-1]}
        VALUE=$(ox_read_property $OLDNAME $PFILE)
        ox_add_property $NEWNAME "$VALUE" $PFILE
        ox_remove_property $OLDNAME $PFILE
    done

    # SoftwareChange_Request-4007
    ox_add_property com.openexchange.mail.filter.authTimeout 6000 $PFILE
}

setConfigFilePermissions() {
    ox_update_permissions "/opt/open-xchange/etc/mailfilter.properties" 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


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

#DEBHELPER#

exit 0
