#!/bin/bash
# postinst script for open-xchange-documents-backend
#
# 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-2052
    ox_add_property io.ox/office//module/useLocalStorage true /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-2053
    ox_add_property io.ox/office//module/templatePath /opt/open-xchange/templates/documents /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-2937
    ox_add_property io.ox/office//module/documentRestoreEnabled true /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-2947
    VALUE=$(ox_read_property com.openexchange.hazelcast.configuration.map.name /opt/open-xchange/etc/hazelcast/officeDocumentDirectory.properties)
    if [ "$VALUE" == "officeDocumentDirectory-1" ]; then
        ox_set_property com.openexchange.hazelcast.configuration.map.name officeDocumentDirectory-2 /opt/open-xchange/etc/hazelcast/officeDocumentDirectory.properties
    fi

    # SoftwareChange_Request-2948
    if dpkg --compare-versions "7.8.1-0" gt-nl "$1"; then
        VALUE=$(ox_read_property com.openexchange.hazelcast.configuration.map.maxIdleSeconds /opt/open-xchange/etc/hazelcast/officeDocumentDirectory.properties)
        if [ "900" = "$VALUE" ]; then
            ox_set_property com.openexchange.hazelcast.configuration.map.maxIdleSeconds 1800 /opt/open-xchange/etc/hazelcast/officeDocumentDirectory.properties
        fi
    fi

    # SoftwareChange_Request-3331
    ox_add_property io.ox/office//spreadsheet/maxFormulas 10000 /opt/open-xchange/etc/settings/office.properties

    # SoftwareChange_Request-3589
    ox_add_property com.openexchange.capability.guard-docs false /opt/open-xchange/etc/documents.properties

    # SoftwareChange_Request-3665
    ox_add_property com.openexchange.office.maxDocumentFileSize 100 /opt/open-xchange/etc/documents.properties

    # SCR-553
    if dpkg --compare-versions "7.10.3-0" gt-nl "$1"; then
        VALUE=$(ox_read_property com.openexchange.spellchecker.hunspell.library /opt/open-xchange/etc/hunspell.properties)
        if [ "/usr/lib/x86_64-linux-gnu/libhunspell-1.3.so.0" = "$VALUE" ]; then
            ox_set_property com.openexchange.spellchecker.hunspell.library "" /opt/open-xchange/etc/hunspell.properties
            ox_comment com.openexchange.spellchecker.hunspell.library add /opt/open-xchange/etc/hunspell.properties
        fi
        VALUE=$(ox_read_property com.openexchange.spellchecker.hunspell.dictionaries /opt/open-xchange/etc/hunspell.properties)
        if [ "/usr/share/hunspell" = "$VALUE" ]; then
            ox_comment com.openexchange.spellchecker.hunspell.dictionaries add /opt/open-xchange/etc/hunspell.properties
        fi
    fi

    SCR=SCR-574
    if ox_scr_todo ${SCR}
    then
      pfile=/opt/open-xchange/etc/documents.properties
      bl_key=com.openexchange.office.upload.blacklist
      bl_value=127.0.0.1-127.255.255.255,localhost
      wl_key=com.openexchange.office.upload.whitelist
      ox_add_property ${bl_key} ${bl_value} ${pfile}
      ox_add_property ${wl_key} "" ${pfile}
      ox_scr_done ${SCR}
    fi

    SCR=SCR-589
    if ox_scr_todo ${SCR}; then
        pfile=/opt/open-xchange/etc/documents-collaboration-client.properties
        if ! contains "com.openexchange.dcs.client.database.jdbc.additionalPropertyName" ${pfile}; then
            cat <<EOF | (cd /opt/open-xchange/etc && patch --forward --no-backup-if-mismatch --reject-file=- --fuzz=3 >/dev/null)
diff --git a/documents-collaboration-client.properties b/documents-collaboration-client.properties
index 47ab86059..30565ed3b 100644
--- a/documents-collaboration-client.properties
+++ b/documents-collaboration-client.properties
@@ -16,6 +16,19 @@
 # Default: n/a
 com.openexchange.dcs.client.database.password=

+# The JDBC database driver related com.openexchange.dcs.client.database.jdbc.*
+# properties are overrides for configuration values, already specified at other locations
+# within the current deployment.
+# The default JDBC deployment configuration values are read first and can be overridden
+# here in case a dedicated DCS database server is used by this DCS client.
+# The default DCS database server JDBC properties are taken from the following database
+# configuration files in that order: dbconnector.yaml, configdb.properties.
+# If a JDBC specific default value should be used, the 'jdbcDefault' string value needs to be applied:
+# com.openexchange.dcs.client.database.jdbc.jdbcStringProperty=jdbcDefault
+# If additional JDBC properties, that might be available but not listed here, need to be set by the
+# administrator, those entries can be freely added to this configuration using the following pattern:
+# com.openexchange.dcs.client.database.jdbc.additionalPropertyName=additionalPropertyValue
+
 # Determines, if the server hostname is to be verified in case a
 # secured connection to the documents-collaboration server is
 # requested.
EOF
        fi
        ox_add_property com.openexchange.dcs.client.database.jdbc.useSSL "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.requireSSL "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.verifyServerCertificate "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.enabledTLSProtocols "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.clientCertificateKeyStoreUrl "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.clientCertificateKeyStoreType "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.clientCertificateKeyStorePassword "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.trustCertificateKeyStoreUrl "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.trustCertificateKeyStoreType "" ${pfile}
        ox_add_property com.openexchange.dcs.client.database.jdbc.trustCertificateKeyStorePassword "" ${pfile}
        ox_scr_done ${SCR}
    fi

    SCR=SCR-590
    if ox_scr_todo ${SCR}; then
        pfile=/opt/open-xchange/etc/documents-collaboration-client.properties
        ox_add_property com.openexchange.dcs.client.username "" ${pfile}
        ox_add_property com.openexchange.dcs.client.password "" ${pfile}
        ox_scr_done ${SCR}
    fi
}

setConfigFilePermissions() {
    PROTECT=( documents-collaboration-client.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
