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

COMMONCONFFILES="excludedupdatetasks.properties foldercache.properties transport.properties"

for FILE in ${COMMONCONFFILES}; do
    dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/common/${FILE} \
    	/opt/open-xchange/etc/${FILE} 6.20.99 -- "$@"
done

GWCONFFILES="ajp.properties attachment.properties cache.ccf calendar.properties configdb.properties contact.properties event.properties file-logging.properties HTMLEntities.properties imap.properties importerExporter.xml import.properties infostore.properties javamail.properties ldap.properties login.properties mailcache.ccf mail.properties mime.types noipcheck.cnf notification.properties ox-scriptconf.sh participant.properties passwordchange.properties server.properties sessioncache.ccf sessiond.properties smtp.properties system.properties user.properties whitelist.properties folder-reserved-names"

for FILE in ${GWCONFFILES}; do
    dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/groupware/${FILE} \
    	/opt/open-xchange/etc/${FILE} 6.20.99 -- "$@"
done

dpkg-maintscript-helper mv_conffile /etc/default/open-xchange-groupware \
    /etc/default/open-xchange 6.20.99 -- "$@"

# SoftwareChange_Request-1094
dpkg-maintscript-helper rm_conffile \
    /opt/open-xchange/etc/groupware/mailjsoncache.properties -- "$@"

# SoftwareChange_Request-1091
dpkg-maintscript-helper rm_conffile \
    /opt/open-xchange/etc/groupware/TidyConfiguration.properties -- "$@"
dpkg-maintscript-helper rm_conffile \
    /opt/open-xchange/etc/groupware/TidyMessages.properties -- "$@"

dpkg-maintscript-helper rm_conffile /etc/init.d/open-xchange-groupware -- "$@"

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

pfile=/opt/open-xchange/etc/ox-scriptconf.sh
if grep COMMONPROPERTIESDIR $pfile >/dev/null; then
    ox_remove_property COMMONPROPERTIESDIR $pfile
    # without original values, we're lost...
    if [ -e ${pfile}.dpkg-new ]; then
       CHECKPROPS="LIBPATH PROPERTIESDIR LOGGINGPROPERTIES OSGIPATH"
       grep JAVA_OXCMD_OPTS $pfile > /dev/null || CHECKPROPS="$CHECKPROPS JAVA_OXCMD_OPTS" && true
       for prop in $CHECKPROPS; do
           oval=$(ox_read_property $prop ${pfile}.dpkg-new)
           if [ -n "$oval" ]; then
              ox_set_property $prop "$oval" $pfile
           fi
       done
    fi
fi

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

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

    # SoftwareChange_Request-1142
    pfile=/opt/open-xchange/etc/imap.properties
    if ! ox_exists_property com.openexchange.imap.umlautFilterThreshold $pfile; then
        ox_set_property com.openexchange.imap.umlautFilterThreshold 50 $pfile
    fi

    # SoftwareChange_Request-1124
    pfile=/opt/open-xchange/etc/server.properties
    if ! ox_exists_property com.openexchange.ajax.response.includeStackTraceOnError $pfile; then
        ox_set_property com.openexchange.ajax.response.includeStackTraceOnError false $pfile
    fi

    # SoftwareChange_Request-1117
    pfile=/opt/open-xchange/etc/server.properties
    if ! ox_exists_property com.openexchange.webdav.disabled $pfile; then
        ox_set_property com.openexchange.webdav.disabled false $pfile
    fi

    # SoftwareChange_Request-1105
    pfile=/opt/open-xchange/etc/cache.ccf
    ptmp=${pfile}.$$
    if grep -E "^jcs.region.OXIMAPConCache" $pfile > /dev/null; then
        grep -vE "^jcs.region.OXIMAPConCache" $pfile > $ptmp
        if [ -s $ptmp ]; then
            cp $ptmp $pfile
        fi
        rm -f $ptmp
    fi

    # SoftwareChange_Request-1091
    pfile=/opt/open-xchange/etc/contact.properties
    if ! ox_exists_property contactldap.configuration.path $pfile; then
	ox_remove_property contactldap.configuration.path $pfile
    fi

    # SoftwareChange_Request-1101
    pfile=/opt/open-xchange/etc/configdb.properties
    if ox_exists_property writeOnly $pfile; then
        ox_remove_property writeOnly $pfile
    fi
    # SoftwareChange_Request-1091
    if ox_exists_property useSeparateWrite $pfile; then
	ox_remove_property useSeparateWrite $pfile
    fi 

    # SoftwareChange_Request-1091
    pfile=/opt/open-xchange/etc/system.properties
    for prop in Calendar Infostore Attachment Notification ServletMappingDir CONFIGPATH \
	AJPPROPERTIES IMPORTEREXPORTER LDAPPROPERTIES EVENTPROPERTIES PUSHPROPERTIES \
	UPDATETASKSCFG HTMLEntities MailCacheConfig TidyMessages TidyConfiguration Whitelist; do
	if ox_exists_property $prop $pfile; then
	   ox_remove_property $prop $pfile
	fi
    done
    if grep -E '^com.openexchange.caching.configfile.*/' $pfile >/dev/null; then
        ox_set_property com.openexchange.caching.configfile cache.ccf $pfile
    fi
    if ox_exists_property MimeTypeFile $pfile; then
        ox_set_property MimeTypeFileName mime.types $pfile
        ox_remove_property MimeTypeFile $pfile
    fi
    pfile=/opt/open-xchange/etc/import.properties
    if ox_exists_property com.openexchange.import.mapper.path $pfile; then
	ox_remove_property com.openexchange.import.mapper.path $pfile
    fi
    pfile=/opt/open-xchange/etc/mail.properties
    if ox_exists_property com.openexchange.mail.JavaMailProperties $pfile; then
	ox_remove_property com.openexchange.mail.JavaMailProperties $pfile
    fi
    pfile=/opt/open-xchange/etc/sessiond.properties
    if ox_exists_property com.openexchange.sessiond.sessionCacheConfig $pfile; then
	ox_remove_property com.openexchange.sessiond.sessionCacheConfig $pfile
    fi

    # SoftwareChange_Request-1024
    pfile=/opt/open-xchange/etc/server.properties
    if ! ox_exists_property com.openexchange.IPMaskV4 $pfile; then
        ox_set_property com.openexchange.IPMaskV4 "" $pfile
    fi
    if ! ox_exists_property com.openexchange.IPMaskV6 $pfile; then
        ox_set_property com.openexchange.IPMaskV6 "" $pfile
    fi

    # SoftwareChange_Request-1027
    pfile=/opt/open-xchange/etc/server.properties
    if ! ox_exists_property com.openexchange.dispatcher.prefix $pfile; then
        ox_set_property com.openexchange.dispatcher.prefix "/ajax/" $pfile
    fi
}

setConfigFilePermissions() {
    ox_update_permissions "/var/spool/open-xchange/uploads" open-xchange:root 750
    ox_update_permissions "/var/log/open-xchange" open-xchange:root 750
}

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
