#!/bin/bash
# postinst script for open-xchange-dav
#
# 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='*'

    # SCR-785
    if ox_scr_todo SCR-785
    then
        if ! contains "macOS" /opt/open-xchange/etc/carddav.properties; then
            cat <<EOF | (cd /opt/open-xchange/etc && patch --strip=1 --forward --no-backup-if-mismatch --reject-file=- --fuzz=3 >/dev/null)
diff --git a/carddav.properties b/carddav.properties
index d8a24e74adb..041e7baa318 100644
--- a/carddav.properties
+++ b/carddav.properties
@@ -24,7 +24,8 @@ com.openexchange.carddav.exposedCollections=0
 # matches all known varieties of the Mac OS Addressbook client, that doesn't 
 # support multiple collections. Only used if 'exposedCollections' is set to 
 # '0'. The pattern is used case insensitive. 
-com.openexchange.carddav.userAgentForAggregatedCollection=.*CFNetwork.*Darwin.*|.*AddressBook.*CardDAVPlugin.*Mac_OS_X.*|.*Mac OS X.*AddressBook.*
+com.openexchange.carddav.userAgentForAggregatedCollection=.*CFNetwork.*Darwin.*|.*AddressBook.*CardDAVPlugin.*Mac_OS_X.*|.*Mac OS X.*AddressBook.*|.*macOS.*AddressBook.*
 
 # Specifies if all visible folders are used to create the aggregated 
 # collection, or if a reduced set of folders only containing the global 
EOF
        fi
        ox_scr_done SCR-785
    fi

    SCR=SCR-812
    if ox_scr_todo ${SCR}
    then
      prop_file=/opt/open-xchange/etc/carddav.properties
      old_prop_key=com.openexchange.carddav.reducedAggregatedCollection
      new_prop_key=com.openexchange.carddav.aggregatedCollectionFolders
      old_default_value="false"
      new_equivalent="reduced_synced"
      if ox_exists_property ${old_prop_key} ${prop_file}
      then
        prop_val=$(ox_read_property ${old_prop_key} ${prop_file})
        if [ "${old_default_value}" = "${prop_val}" ]
        then
          ox_remove_property ${old_prop_key} ${prop_file}
        else
          ox_remove_property ${old_prop_key} ${prop_file}
          ox_set_property ${new_prop_key} ${new_equivalent} ${prop_file}
        fi
      fi
      ox_scr_done ${SCR}
    fi

}

case "$1" in
    configure)
        test -n "$2" && {
            # we are in update mode, run postFix to apply fixes
            postFix "$2"
        }
    ;;

    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
