#!/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/guard/sbin/oxfunctions.sh

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

    # prevent bash from expanding, see bug 13316
    GLOBIGNORE='*'
    
    # SoftwareChange_Request-2182
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/guard/etc/guard.properties
    if ox_exists_property com.openexchange.guard.logType $pfile; then
        ox_remove_property com.openexchange.guard.logType $pfile
    fi
    
    # SoftwareChange_Request-2183
    # -----------------------------------------------------------------------
    prefix="com.openexchange.guard."
    declare -A defaults=([useSSL]='true' [SSLPort]='8443' [SSLKeyStore]='//keystore location//' [SSLKeyName]='//alias name here//' [SSLKeyPass]='//ssl password//');
    declare -A props
    declare -a propKeys=('SSLPort' 'SSLKeyStore' 'SSLKeyName' 'SSLKeyPass');

    prop="com.openexchange.guard.useSSL"
    if ox_exists_property $prop $pfile; then
        useSSL=$(ox_read_property $prop $pfile)
    else
        ox_comment $prop remove $pfile
    fi

    for prop in ${propKeys[@]}; do
        p=$prefix$prop
        if ox_exists_property $p $pfile; then
            VALUE=$(ox_read_property $p $pfile)
            props[$prop]=$VALUE
        else
            ox_comment $p remove $pfile
        fi
        ox_remove_property $p $pfile
    done

    sed -i 's/com.openexchange.guard.useSSL:.*/# com.openexchange.backend_SSL=true/g' $pfile

    prop="com.openexchange.guard.useSSL"
    commented=false
    if [[ -z "$useSSL" ]]; then
        useSSL="true"
        commented=""
    fi
    ox_set_property $prop "$useSSL" $pfile
    if [[ -z "$commented" ]]; then
        ox_comment $prop add $pfile
    fi

    for prop in ${propKeys[@]}; do
        p=$prefix$prop
        commented=false
        value=${props[$prop]}
        if [[ -z "$value" ]]; then
            value=${defaults[$prop]}
            commented=""
        fi
        ox_set_property $p "$value" $pfile
        if [[ -z "$commented" ]]; then
            ox_comment $p add $pfile
        fi
    done
}

case "$1" in
    configure)
        chmod 750 /var/log/open-xchange/guard
        chown open-xchange:root /var/log/open-xchange/guard
        chmod 750 /var/spool/open-xchange/guard/uploads
        chown open-xchange:root /var/spool/open-xchange/guard/uploads
        chmod 640 /opt/open-xchange/guard/etc/guard.properties
        chown root:open-xchange /opt/open-xchange/guard/etc/guard.properties
        
        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
