#!/bin/bash
# postinst script for open-xchange-drive
#
# 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-392
  # don't wrap in ox_scr_todo as it might have to reexecute after downgrade or
  # configs will be lost
  orig_pfile=/opt/open-xchange/etc/drive.properties
  save_pfile=${orig_pfile}.dpkg-backup
  rm_pfile=${orig_pfile}.dpkg-remove
  if [ -e ${save_pfile} ] && [ ! -e ${orig_pfile} ]
  then
    # preinst upgrade detected modifications that we want to keep
    # but as lean config now, like as if the user created the file
    echo "Keeping ${save_pfile} as ${orig_pfile} due to modifications"
    mv ${save_pfile} ${orig_pfile}
  fi
  if [ -e ${rm_pfile} ] && [ ! -e ${orig_pfile} ]
  then
    # preinst upgrade detected no modifications remove old file
    echo "Removing ${rm_pfile} as ${orig_pfile} had no modifications"
    rm -f ${rm_pfile}
  fi

  SCR=SCR-1289-DRIVE
  if ox_scr_todo ${SCR}
  then
    pfile=/opt/open-xchange/etc/drive.properties
    if [ -f "$pfile" ]
		then
	    old_key=com.openexchange.drive.events.gcm.enabled
      new_key=com.openexchange.drive.events.fcm.enabled

      if ox_exists_property ${old_key} ${pfile}
      then
        value=$(ox_read_property ${old_key} ${pfile})
        ox_remove_property ${old_key} ${pfile}
        ox_add_property ${new_key} "${value}" ${pfile}
      else
        ox_add_property ${new_key} "false" ${pfile}
      fi
	    
	    old_key=com.openexchange.drive.events.gcm.clientId
      new_key=com.openexchange.drive.events.fcm.clientId

      if ox_exists_property ${old_key} ${pfile}
      then
        value=$(ox_read_property ${old_key} ${pfile})
        ox_remove_property ${old_key} ${pfile}
        ox_add_property ${new_key} "${value}" ${pfile}
      else
        ox_add_property ${new_key} "" ${pfile}
      fi
    fi
    ox_scr_done ${SCR}
  fi

  SCR=SCR-1292
  if ox_scr_todo ${SCR}
  then
    pfile=/opt/open-xchange/etc/drive.properties
    if [ -f "$pfile" ]
		then
      ox_remove_property com.openexchange.drive.events.gcm.key "$pfile"
		fi
    ox_scr_done ${SCR}
  fi
}

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

    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
