#!/bin/sh
#
# @package      hubzero-telequotad
# @file         preinst
# @copyright    Copyright (c) 2010-2020 The Regents of the University of California.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2020 The Regents of the University of California.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of The Regents of the University of California.
#

# preinst script for hubzero-telequotad
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <new-preinst> `install'
#        * <new-preinst> `install' <old-version>
#        * <new-preinst> `upgrade' <old-version>
#        * <old-preinst> `abort-upgrade' <new-version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    install|upgrade)

		# upgrading from version with broken init.d script 
		# revert it broken state after prerm fixed it so it can now be removed
		if dpkg --compare-versions "$2" le "1.0.0-1"
		then
			if [ -f /etc/init.d/telequotad ] 
			then
				sed -i 's/killall -TERM $DAEMON/killall -TERM $NAME/g' /etc/init.d/telequotad
			fi
		fi

		# upgrading from version with config file in /etc/hubzero/telequotad.conf 
		# or /etc/mw/telequotad.conf.
		if dpkg --compare-versions "$2" le "1.0.0-1"
		then
			if [ ! -f /etc/telequotad.conf ]
			then 
				if [ -f /etc/hubzero/telequotad.conf ] 
				then
					mv /etc/hubzero/telequotad.conf /etc/telequotad.conf
				elif [ -f /etc/mw/telequotad.conf ]
				then
					mv /etc/mw/telequotad.conf /etc/telequotad.conf
				fi
			fi
		fi

		# upgrade trim location from known old versions
		if [ -f /etc/telequotad.conf ] 
		then
			sed -i 's/\/usr\/lib\/hubzero\/bin\/trim/\/usr\/sbin\/trim/g' /etc/telequotad.conf
			sed -i 's/\/usr\/lib\/mw\/telequotad\/trim/\/usr\/sbin\/trim/g' /etc/telequotad.conf
			sed -i 's/\/usr\/sbin\/telequotad-trim/\/usr\/sbin\/trim/g' /etc/telequotad.conf
		fi
    ;;

    abort-upgrade)
    ;;

    *)
        echo "preinst 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
