#!/bin/sh
#
# @package      hubzero-mw-service
# @file         preinst
# @author       Nicholas J. Kisseberth <nkissebe@purdue.edu>
# @copyright    Copyright (c) 2010-2015 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2010-2015 HUBzero Foundation, LLC.
#
# 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 HUBzero Foundation, LLC.
#

# preinst script for hubzero-mw-service
#
# 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)
		if dpkg --compare-versions "$2" ge "1.1.0-1"
		then
			break
		fi

		# upgrading from versions with support files in /etc/hubzero/
		if dpkg --compare-versions "$2" ge "0.9.1-1"
		then
			if [ ! -d /etc/mw-service ]
			then
				mkdir /etc/mw-service
				if [ -d /etc/mw-service ]
				then
					chown root:root /etc/mw-service
					chmod 0775 /etc/mw-service
				fi
			fi
			if [ ! -f /etc/mw-service/mw-service.conf ]
			then 
				if [ -f /etc/hubzero/maxwell.conf ] 
				then
					cp /etc/hubzero/maxwell.conf /etc/mw-service/mw-service.conf
				fi
			fi
			if [ ! -f /etc/mw-service/quota.conf ]
			then 
				if [ -f /etc/hubzero/quota.conf ] 
				then
					cp /etc/hubzero/quota.conf /etc/mw-service/quota.conf
				fi
			fi
			if [ ! -f /etc/mw-service/xvnc.pem ]
			then 
				if [ -f /etc/hubzero/xvnc.pem ] 
				then
					cp /etc/hubzero/xvnc.pem /etc/mw-service/xvnc.pem
				fi
			fi
			if [ ! -f /etc/mw-service/notify.key ]
			then 
				if [ -f /etc/hubzero/notify.key ] 
				then
					cp /etc/hubzero/notify.key /etc/mw-service/notify.key
				fi
			fi
		fi

		# upgrading from versions with support files in /etc/mw/
		if dpkg --compare-versions "$2" ge "0.8.0-1"
		then
			if [ ! -d /etc/mw-service ]
			then
				mkdir /etc/mw-service
				if [ -d /etc/mw-service ]
				then
					chown root:root /etc/mw-service
					chmod 0775 /etc/mw-service
				fi
			fi
			if [ ! -f /etc/mw-service/mw-service.conf ]
			then 
				if [ -f /etc/mw/maxwell.conf ] 
				then
					cp /etc/mw/maxwell.conf /etc/mw-service/mw-service.conf
				fi
			fi
			if [ ! -f /etc/mw-service/quota.conf ]
			then 
				if [ -f /etc/mw/quota.conf ] 
				then
					cp /etc/mw/quota.conf /etc/mw-service/quota.conf
				fi
			fi
			if [ ! -f /etc/mw-service/xvnc.pem ]
			then 
				if [ -f /etc/mw/xvnc.pem ] 
				then
					cp /etc/mw/xvnc.pem /etc/mw-service/xvnc.pem
				fi
			fi
			if [ ! -f /etc/mw-service/notify.key ]
			then 
				if [ -f /etc/mw/notify.key ] 
				then
					cp /etc/mw/notify.key /etc/mw-service/notify.key
				fi
			fi
		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
