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

CONFFILES="AdminDaemon.properties Group.properties ModuleAccessDefinitions.properties mpasswd Resource.properties Sql.properties"

for FILE in $CONFFILES; do
    dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/admindaemon/$FILE /opt/open-xchange/etc/$FILE 6.20.99 -- "$@"
done
dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/admindaemon/User.properties /opt/open-xchange/etc/AdminUser.properties 6.20.99 \
    -- "$@"

ofile=/opt/open-xchange/etc/admindaemon/RMI.properties
pfile=/opt/open-xchange/etc/rmi.properties
if [ -e $ofile ]; then
    oval=$(ox_read_property RMI_PORT $ofile)
    if [ -n "$oval" ]; then
       ox_set_property com.openexchange.rmi.port $oval $pfile
    fi
fi

dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/admindaemon/RMI.properties -- "$@"
dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/admindaemon/ox-admin-scriptconf.sh -- "$@"

ofile=/opt/open-xchange/etc/admindaemon/ox-admin-scriptconf.sh.dpkg-bak
pfile=/opt/open-xchange/etc/ox-scriptconf.sh
if [ -e $ofile ]; then
    oval=$(ox_read_property JAVA_OXCMD_OPTS $ofile)
    if [ -n "$oval" ]; then
       ox_set_property JAVA_OXCMD_OPTS "$oval" $pfile
    else
       ox_set_property JAVA_OXCMD_OPTS "-Djava.net.preferIPv4Stack=true" $pfile
    fi
    rm -f $ofile
fi

dpkg-maintscript-helper mv_conffile /opt/open-xchange/etc/admindaemon/plugin/hosting.properties \
    /opt/open-xchange/etc/plugin/hosting.properties 6.20.99 open-xchange-admin-plugin-hosting -- $1 \
    $(dpkg-query -W -f='${Config-Version}' open-xchange-admin-plugin-hosting)

for FILE in cache.ccf configdb.properties file-logging.properties management.properties logging.properties system.properties; do
    dpkg-maintscript-helper rm_conffile /opt/open-xchange/etc/admindaemon/$FILE -- "$@"
done

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

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

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

    # SoftwareChange_Request-1118
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/AdminDaemon.properties
    ox_remove_property TOOL_STORAGE $pfile

    ofile=/opt/open-xchange/etc/AdminDaemon.properties
    pfile=/opt/open-xchange/etc/rmi.properties
    if ox_exists_property BIND_ADDRESS $ofile; then
        oval=$(ox_read_property BIND_ADDRESS $ofile)
        if [ -n "$oval" ]; then
           ox_set_property com.openexchange.rmi.host $oval $pfile
        fi
        ox_remove_property BIND_ADDRESS $ofile
    fi

    # SoftwareChange_Request-1091
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/AdminUser.properties
    ox_remove_property CREATE_HOMEDIRECTORY $pfile
    ox_remove_property HOME_DIR_ROOT $pfile
    pfile=/opt/open-xchange/etc/AdminDaemon.properties
    ox_remove_property USER_PROP $pfile
    ox_remove_property GROUP_PROP $pfile
    ox_remove_property RESOURCE_PROP $pfile
    ox_remove_property RMI_PROP $pfile
    ox_remove_property SQL_PROP $pfile
    ox_remove_property MASTER_AUTH_FILE $pfile
    ox_remove_property ACCESS_COMBINATIONS_FILE $pfile

    # SoftwareChange_Request-1100
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/AdminDaemon.properties
    ox_remove_property SERVER_NAME $pfile

    # SoftwareChange_Request-1846
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/AdminUser.properties
    ox_add_property SENT_MAILFOLDER_EN_GB SentMail $pfile
    ox_add_property TRASH_MAILFOLDER_EN_GB Trash $pfile
    ox_add_property DRAFTS_MAILFOLDER_EN_GB Drafts $pfile
    ox_add_property SPAM_MAILFOLDER_EN_GB Spam $pfile
    ox_add_property CONFIRMED_SPAM_MAILFOLDER_EN_GB confirmed-spam $pfile
    ox_add_property CONFIRMED_HAM_MAILFOLDER_EN_GB confirmed-ham $pfile

    # SoftwareChange_Request-1831
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/ModuleAccessDefinitions.properties

	if grep -E "projects" $pfile > /dev/null; then
	   ptmp=${pfile}.$$

	   sed -e 's;projects *,;;g' -e 's;, *projects;;g' $pfile > $ptmp

	   if [ -s $ptmp ]; then
	      cp $ptmp $pfile
	   fi
	   rm -f $ptmp
	fi

    # SoftwareChange_Request-2074
    # -----------------------------------------------------------------------
    pfile=/opt/open-xchange/etc/ModuleAccessDefinitions.properties
    for key in rssbookmarks rssportal forum pinboardwrite; do
        if grep -E $key $pfile > /dev/null; then
            ptmp=${pfile}.$$

            sed -e "s;$key *,;;g" -e "s;, *$key;;g" $pfile > $ptmp

            if [ -s $ptmp ]; then
                cp $ptmp $pfile
            fi
            rm -f $ptmp
        fi
    done

    # SoftwareChange_Request-2197
    ox_add_property SCHEMA_MOVE_MAINTENANCE_REASON 1431655765 /opt/open-xchange/etc/plugin/hosting.properties

    # SoftwareChange_Request-2285
    TMPFILE=$(mktemp)
    while read LINE; do
        case "$LINE" in
            \#*|*:crypt:*|*:bcrypt:*)
                # ignore commented and already converted lines
                echo $LINE
                ;;
            *)
                IFS=":"
                PARTS=( $LINE )
                unset IFS
                # only modify matching lines
                if [ -n "${PARTS[0]}" ] && [ -n "${PARTS[1]}" ]
                then
                    echo ${PARTS[0]}:crypt:${PARTS[1]}
                else 
                    echo $LINE
                fi  
                ;;  
        esac
    done < /opt/open-xchange/etc/mpasswd >$TMPFILE
    cat $TMPFILE > /opt/open-xchange/etc/mpasswd
    rm $TMPFILE

    # SoftwareChange_Request-2323
    VALUE=$(ox_read_property SENT_MAILFOLDER_EN_GB /opt/open-xchange/etc/AdminUser.properties)
    if [ "SentMail" == "$VALUE" ]; then
        ox_set_property SENT_MAILFOLDER_EN_GB "Sent Mail" /opt/open-xchange/etc/AdminUser.properties
    fi

    # SoftwareChange_Request-2382
    ox_add_property MASTER_ACCOUNT_OVERRIDE false /opt/open-xchange/etc/AdminDaemon.properties

    # SoftwareChange_Request-2535
    ox_add_property drive globaladdressbookdisabled,infostore,deniedportal /opt/open-xchange/etc/ModuleAccessDefinitions.properties

    # SoftwareChange_Request-2699
    ox_add_property ALLOW_CHANGING_QUOTA_IF_NO_FILESTORE_SET false /opt/open-xchange/etc/AdminUser.properties
}

setConfigFilePermissions() {
    ox_update_permissions "/opt/open-xchange/etc/mpasswd" root:open-xchange 640
}

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 rm_conffile /opt/open-xchange/etc/Sql.properties 7.3.99 -- "$@"

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

#DEBHELPER#

exit 0
