#!/bin/bash
# postinst script for open-xchange-oauth
#
# 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-2410
    if [ -e /opt/open-xchange/etc/twitter.properties ]; then
        VALUE=$(ox_read_property com.openexchange.twitter.consumerKey /opt/open-xchange/etc/twitter.properties)
        if [ "" != "$VALUE" ]; then
            ox_add_property com.openexchange.oauth.twitter.apiKey "$VALUE" /opt/open-xchange/etc/twitteroauth.properties
            ox_remove_property com.openexchange.twitter.consumerKey /opt/open-xchange/etc/twitter.properties
        fi
        VALUE=$(ox_read_property com.openexchange.twitter.consumerSecret /opt/open-xchange/etc/twitter.properties)
        if [ "" != "$VALUE" ]; then
            ox_add_property com.openexchange.oauth.twitter.apiSecret "$VALUE" /opt/open-xchange/etc/twitteroauth.properties
            ox_remove_property com.openexchange.twitter.consumerSecret /opt/open-xchange/etc/twitter.properties
        fi
    else
        ox_add_property com.openexchange.oauth.twitter.apiKey INSERT_YOUR_CONSUMER_KEY_HERE /opt/open-xchange/etc/twitteroauth.properties
        ox_add_property com.openexchange.oauth.twitter.apiSecret INSERT_YOUR_CONSUMER_SECRET_HERE /opt/open-xchange/etc/twitteroauth.properties
    fi

    # SoftwareChange_Request-2532
    VALUE=$(ox_read_property com.openexchange.oauth.google.redirectUrl /opt/open-xchange/etc/googleoauth.properties)
    if dpkg --compare-versions "7.8.0" gt-nl "$1"; then
        ox_set_property com.openexchange.oauth.google.redirectUrl "$VALUE" /opt/open-xchange/etc/googleoauth.properties
    fi

    # SoftwareChange_Request-3506
    ox_add_property com.openexchange.oauth.dropbox.redirectUrl REPLACE_WITH_REDIRECT_URL /opt/open-xchange/etc/dropboxoauth.properties
    ox_add_property com.openexchange.oauth.dropbox.productName REPLACE_WITH_YOUR_REGISTERED_DROPBOX_APP /opt/open-xchange/etc/dropboxoauth.properties

    # SoftwareChange_Request-3556
    ox_add_property com.openexchange.oauth.yahoo.redirectUrl REPLACE_WITH_REDIRECT_URL /opt/open-xchange/etc/yahoooauth.properties
    ox_add_property com.openexchange.oauth.yahoo.productName REPLACE_WITH_YOUR_REGISTERED_YAHOO_APP /opt/open-xchange/etc/yahoooauth.properties
}

setConfigFilePermissions() {
    PROTECT="boxcomoauth.properties dropboxoauth.properties googleoauth.properties msliveconnectoauth.properties twitteroauth.properties xingoauth.properties yahoooauth.properties settings/flickroauth.properties settings/tumblroauth.properties"
    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

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

#DEBHELPER#

exit 0
