#!/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

PROTECT="facebookoauth.properties yahoooauth.properties xingoauth.properties settings/flickroauth.properties settings/tumblroauth.properties"

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

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

    # SoftwareChange_Request-1494
    pfile=/opt/open-xchange/etc/yahoooauth.properties
    if ! ox_exists_property com.openexchange.oauth.yahoo $pfile; then
       if grep -E '^com.openexchange.*REPLACE_THIS_WITH_VALUE_OBTAINED_FROM' $pfile > /dev/null; then
           ox_set_property com.openexchange.oauth.yahoo false $pfile
       else
           ox_set_property com.openexchange.oauth.yahoo true $pfile
       fi
    fi
    pfile=/opt/open-xchange/etc/facebookoauth.properties
    if ! ox_exists_property com.openexchange.oauth.facebook $pfile; then
       if grep -E '^com.openexchange.*INSERT_YOUR_API_KEY_HERE' $pfile > /dev/null; then
           ox_set_property com.openexchange.oauth.facebook false $pfile
       else
           ox_set_property com.openexchange.oauth.facebook true $pfile
       fi
    fi

    # SoftwareChange_Request-2146
    PFILE=/opt/open-xchange/etc/xingoauth.properties
    ox_add_property com.openexchange.oauth.xing.consumerKey REPLACE_THIS_WITH_YOUR_XING_PRODUCTIVE_CONSUMER_KEY $PFILE
    ox_add_property com.openexchange.oauth.xing.consumerSecret REPLACE_THIS_WITH_YOUR_XING_PRODUCTIVE_CONSUMER_SECRET $PFILE
    VALUE=$(ox_read_property com.openexchange.oauth.xing $PFILE)
    if [ "$VALUE" = "false" ]; then
        ox_set_property com.openexchange.oauth.xing true $PFILE
    fi

    # SoftwareChange_Request-2275
    if dpkg --compare-versions "7.6.1" gt-nl "$1"; then
        ox_set_property com.openexchange.oauth.google false /opt/open-xchange/etc/googleoauth.properties
        ox_set_property com.openexchange.oauth.msliveconnect false /opt/open-xchange/etc/msliveconnectoauth.properties
        ox_set_property com.openexchange.oauth.xing false /opt/open-xchange/etc/xingoauth.properties
    fi
}

setConfigFilePermissions() {
    for FILE in $PROTECT
    do
        ox_update_permissions /opt/open-xchange/etc/$FILE root:open-xchange 640
    done
}

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

dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/deferrer.properties /opt/open-xchange/etc/deferrer.properties 6.20.99 \
    open-xchange-http-deferrer -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-http-deferrer)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/oauth.properties /opt/open-xchange/etc/oauth.properties 6.20.99 -- "$@"
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/facebookoauth.properties \
    /opt/open-xchange/etc/facebookoauth.properties 6.20.99 open-xchange-oauth-facebook -- $1 \
    $(dpkg-query -W -f='${Config-Version}' open-xchange-oauth-facebook)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/twitteroauth.properties /opt/open-xchange/etc/twitteroauth.properties \
    6.20.99 open-xchange-oauth-twitter -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-oauth-twitter)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/yahoooauth.properties /opt/open-xchange/etc/yahoooauth.properties \
    6.20.99 open-xchange-oauth-yahoo -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-oauth-yahoo)

# SoftwareChange_Request-2154
dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/msnoauth.properties 7.6.1~ -- "$@"

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

#DEBHELPER#

exit 0
