#!/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="crawlers/gmx.com.yml crawlers/gmx.de.yml crawlers/GoogleCalendar.yml crawlers/GoogleMail.yml 'crawlers/Sun Calendar.yml' 'crawlers/Sun Contacts.yml' 'crawlers/Sun Tasks.yml' crawlers/web.de.yml crawlers/XING.yml crawlers/yahoo.com.yml 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/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/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)

#SoftwareChange_Request-1847
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/crawlers/XING.yml /opt/open-xchange/etc/crawlers/xing.yml 7.4.2~ -- "$@"

#SoftwareChange_Request-2145
#not in postfix() as dpkg-maintscript-helper needs $@ which isn't satisfied by postfix()
dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/crawlers/GoogleCalendar.yml 7.6.1~ -- "$@"
dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/crawlers/GoogleMail.yml 7.6.1~ -- "$@"

# SoftwareChange_Request-2147
dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/crawlers/xing.yml 7.6.1~ -- "$@"

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

. /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
    # obsoleted by SoftwareChange_Request-1710
    # 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

    # SoftwareChange_Request-1501
    # updated by SoftwareChange_Request-1710
    FILES=( crawler.properties crawler.properties crawler.properties crawler.properties crawler.properties crawler.properties crawler.properties crawler.properties crawler.properties crawler.properties microformatSubscription.properties microformatSubscription.properties yahoosubscribe.properties )
    NEWPROPS=( com.openexchange.subscribe.crawler.googlemail.autorunInterval com.openexchange.subscribe.xing.autorunInterval com.openexchange.subscribe.crawler.webde.autorunInterval com.openexchange.subscribe.crawler.google.calendar.autorunInterval com.openexchange.subscribe.crawler.t-online.de.autorunInterval com.openexchange.subscribe.crawler.gmx.de.autorunInterval com.openexchange.subscribe.crawler.msn.de.autorunInterval com.openexchange.subscribe.crawler.suncontacts.autorunInterval com.openexchange.subscribe.crawler.suncalendar.autorunInterval com.openexchange.subscribe.crawler.suntasks.autorunInterval com.openexchange.subscribe.microformats.contacts.http.autorunInterval com.openexchange.subscribe.microformats.infostore.http.autorunInterval com.openexchange.subscribe.socialplugin.yahoo.autorunInterval )
    for I in $(seq 1 ${#NEWPROPS[@]}); do
        NEWPROP=${NEWPROPS[$I-1]}
        PFILE=/opt/open-xchange/etc/${FILES[$I-1]}
        if ! ox_exists_property $NEWPROP $PFILE; then
            ox_set_property $NEWPROP 1d $PFILE
        fi
    done

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

    #SoftwareChange_Request-1623
    PFILE=/opt/open-xchange/etc/crawler.properties
    NEWPROPS=( com.openexchange.subscribe.crawler.gmx.de com.openexchange.subscribe.crawler.yahoocom com.openexchange.subscribe.crawler.web.de )
    for I in $(seq 1 ${#NEWPROPS[@]}); do
        NEWPROP=${NEWPROPS[$I-1]}
        if ! ox_exists_property $NEWPROP $PFILE; then
            ox_set_property $NEWPROP true $PFILE
        fi
    done

    #SoftwareChange_Request-1710
    pfile=/opt/open-xchange/etc/crawler.properties
    for prop in com.openexchange.subscribe.crawler.gmx.com com.openexchange.subscribe.crawler.gmx; do
        if ox_exists_property ${prop}.autorunInterval $pfile; then
            ox_remove_property ${prop}.autorunInterval $pfile
        fi
        if ox_exists_property $prop $pfile; then
            ox_remove_property $prop $pfile
        fi
    done

    #SoftwareChange_Request-1800
    pfile=/opt/open-xchange/etc/crawler.properties
    for prop in com.openexchange.subscribe.crawler.updatepath com.openexchange.subscribe.crawler.updatedfile com.openexchange.subscribe.crawler.updateinterval com.openexchange.subscribe.crawler.enableautoupdate com.openexchange.subscribe.crawler.onlyupdatealreadyinstalled; do
        if ox_exists_property $prop $pfile; then
            ox_remove_property $prop $pfile
        fi
    done

    #SoftwareChange_Request-2145
    pfile=/opt/open-xchange/etc/crawler.properties
    for prop in com.openexchange.subscribe.crawler.googlemail com.openexchange.subscribe.crawler.google.calendar com.openexchange.subscribe.crawler.googlemail.autorunInterval com.openexchange.subscribe.crawler.google.calendar.autorunInterval; do
        if ox_exists_property $prop $pfile; then
            ox_remove_property $prop $pfile
        fi
    done

    # SoftwareChange_Request-2147
    pfile=/opt/open-xchange/etc/crawler.properties
    for prop in com.openexchange.subscribe.xing com.openexchange.subscribe.xing.autorunInterval; do
        if ox_exists_property $prop $pfile; then
            ox_remove_property $prop $pfile
        fi
    done
    
    # SoftwareChange_Request-2470
    ox_add_property com.openexchange.subscribe.microformats.createModifyEnabled false /opt/open-xchange/etc/microformatSubscription.properties

    # SoftwareChange_Request-2670
    dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/crawlers/t-online.yml 7.8.0~ -- "$@"
    ox_remove_property com.openexchange.subscribe.crawler.t-online.de $pfile
    ox_remove_property com.openexchange.subscribe.crawler.t-online.de.autorunInterval $pfile
    
}

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
