#!/bin/bash
# postinst script for open-xchange-core
#
# 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

UPDATE=/opt/open-xchange/appsuite/share/update-themes.sh
if [ -x $UPDATE ]; then $UPDATE --later; fi

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

postFix() {
    # e.g 7.8.4-26 -> 784
    local version=${1%-*}
    version=${version//[-.]/}

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

    SCR=SCR-606
    if ox_scr_todo ${SCR}
    then
        pfile=/opt/open-xchange/etc/settings/open-xchange-dynamic-theme.properties
        VALUE=$(ox_read_property io.ox/dynamic-theme//logoWidth $pfile)
        if [ "auto" = "${VALUE//[[:space:]]/}" ]
        then
            cat <<EOF | (cd /opt/open-xchange/etc/settings && patch --strip=1 --forward --no-backup-if-mismatch --reject-file=- --fuzz=3 >/dev/null)
diff --git a/open-xchange-dynamic-theme.properties b/open-xchange-dynamic-theme.properties
index b8c0130..3ae4d73 100644
--- a/open-xchange-dynamic-theme.properties
+++ b/open-xchange-dynamic-theme.properties
@@ -25,15 +25,17 @@ io.ox/dynamic-theme//headerLogo=
 # URL of the logo in the top left corner of the top bar.
 io.ox/dynamic-theme//logoURL=

-# Optional width of the logo as number of pixels or any CSS length unit.
+# Width of the logo as number of pixels or any CSS length unit.
 # For best display on high-resolution screens, it is recommended to use
 # a bigger image and specify a smaller size here.
-# Default: auto
-io.ox/dynamic-theme//logoWidth=auto
+# Set to "auto" to use the native width of the image.
+# Default: 60
+io.ox/dynamic-theme//logoWidth=60

 # Optional height of the logo as number of pixels or any CSS length unit.
 # The maximum value is 64. For best display on high-resolution screens,
 # it is recommended to use a bigger image and specify a smaller size here.
+# Set to "auto" to use the native height of the image.
 # Default: auto
 io.ox/dynamic-theme//logoHeight=auto
EOF
        ox_set_property io.ox/dynamic-theme//logoWidth 60 $pfile
        fi
        ox_scr_done ${SCR}
    fi

    # OXUIB-98
    if contains "[“”]" /opt/open-xchange/etc/settings/open-xchange-dynamic-theme.properties; then
        sed -i 's/[“”]/"/g' /opt/open-xchange/etc/settings/open-xchange-dynamic-theme.properties
    fi
}

case "$1" in
    configure)
        if [ -n "$2" ]
        then
            # we are in update mode, run postFix to apply fixes
            postFix "$2"
        fi
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac
