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

CRAWLERCONFFILES="crawler.properties"

for FILE in ${CRAWLERCONFFILES}; do
    dpkg-maintscript-helper mv_conffile "/opt/open-xchange/etc/groupware/${FILE}" "/opt/open-xchange/etc/${FILE}" 6.20.99 \
        open-xchange-subscribe-crawler -- $1 $(dpkg-query -W -f='${Config-Version}' open-xchange-subscribe-crawler)
done

dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/facebooksubscribe.properties \
    /opt/open-xchange/etc/facebooksubscribe.properties 6.20.99 open-xchange-subscribe-facebook -- $1 \
    $(dpkg-query -W -f='${Config-Version}' open-xchange-subscribe-facebook)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/linkedinsubscribe.properties \
    /opt/open-xchange/etc/linkedinsubscribe.properties 6.20.99 open-xchange-subscribe-linkedin -- $1 \
    $(dpkg-query -W -f='${Config-Version}' open-xchange-subscribe-linkedin)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/microformatSubscription.properties \
    /opt/open-xchange/etc/microformatSubscription.properties 6.20.99 open-xchange-subscribe-microformats -- $1 \
    $(dpkg-query -W -f='${Config-Version}' open-xchange-subscribe-microformats)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/msnsubscribe.properties \
    /opt/open-xchange/etc/msnsubscribe.properties 6.20.99 open-xchange-subscribe-msn -- $1 \
    $(dpkg-query -W -f='${Config-Version}' open-xchange-subscribe-msn)
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/yahoosubscribe.properties \
    /opt/open-xchange/etc/yahoosubscribe.properties 6.20.99 open-xchange-subscribe-yahoo -- $1 \
    $(dpkg-query -W -f='${Config-Version}' open-xchange-subscribe-yahoo)

. /opt/open-xchange/lib/oxfunctions.sh

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

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

    #SoftwareChange_Request-1318
    pfile=/opt/open-xchange/etc/microformatSubscription.properties
    if ! ox_exists_property com.openexchange.subscribe.microformats.allowedHosts $pfile; then
        ox_set_property com.openexchange.subscribe.microformats.allowedHosts '' $pfile
    fi

    #SoftwareChange_Request-1284
    pfile=/opt/open-xchange/etc/crawler.properties
    for prop in com.openexchange.subscribe.crawler.yahoocom \
		com.openexchange.subscribe.xing; do
        if ox_exists_property $prop $pfile; then
            ox_remove_property $prop $pfile
        fi
    done

    #SoftwareChange_Request-1091
    pfile=/opt/open-xchange/etc/crawler.properties
    if grep -E '^com.openexchange.subscribe.crawler.path.*/' $pfile >/dev/null; then
        ox_set_property com.openexchange.subscribe.crawler.path crawlers $pfile
    fi

    #SoftwareChange_Request-1099
    pfile=/opt/open-xchange/etc/crawler.properties
    if ! ox_exists_property com.openexchange.subscribe.crawler.gmx.com $pfile; then
        ox_set_property com.openexchange.subscribe.crawler.gmx.com true $pfile
    fi
}

setConfigFilePermissions() {
    find /opt/open-xchange/etc/crawlers -name "*.yml" -print0 | while read -d $'\0' i; do
        ox_update_permissions "$i" open-xchange:root 644
    done
    ox_update_permissions "/opt/open-xchange/etc/crawlers" open-xchange:root 755
}

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
