#!/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
}

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
