#!/bin/sh
# prerm script for lliurex-starboard
#
# see: dh_installdeb(1)

set -e

# summary of how this script can be called:
#        * <prerm> `remove'
#        * <old-prerm> `upgrade' <new-version>
#        * <new-prerm> `failed-upgrade' <old-version>
#        * <conflictor's-prerm> `remove' `in-favour' <package> <new-version>
#        * <deconfigured's-prerm> `deconfigure' `in-favour'
#          <package-being-installed> <version> `removing'
#          <conflicting-package> <version>
# for details, see https://www.debian.org/doc/debian-policy/ or
# the debian-policy package


case "$1" in
    remove|upgrade|deconfigure)
                rm -f /usr/local/bin/StarBoard
                for x in HitachiSolutions lsadrv StarBoardSoftware; do 
                    rm -rf /usr/local/$x
                done
		rm -rf /usr/local/lib/StarBoardSoftware
                dkms remove starboard-lsadrv/1.0 --all || true
                dkms remove starboard-coach/1.0 --all || true
		if [ -f ]
		then
			rm -rf /var/log/hitachi
		fi

		mkdir -p /var/log/hitachi/

		if [ -d /var/log/hitachi/ ]
		then
			logfile="/var/log/hitachi/$$.uninstall"
		else
			logfile="/tmp/hitach.starboard.uninstall.$$"
		fi

		date=`date`
		echo "package StarBoardSoftware:Uninstalled at $date" >>$logfile
		echo "PARAMETERS $* "  >>$logfile

		# process check target file
		CHK_PROCESS="\
		StarBoard.sh \
		DBDrvGui \
		LsCamWiz \
		LTabMonA \
		StarBoardWizard \
		SBMainte \
		"

		if [ "$1" =  remove ] || [ "$1" = upgrade ]
		then
			CHK_RESULT=0
			for check_process in ${CHK_PROCESS}
			do
				CHK_COUNT=`ps -e | grep ${check_process} | wc -l`
				if [ $CHK_COUNT -gt 0 ]
				then
				    echo "${check_process} is running!"
				    CHK_RESULT=1
				fi
			done
			if [ $CHK_RESULT != 0 ]
			then
				exit 1
			fi

			if [ -f /usr/local/StarBoardSoftware/uninstall.sh ]
			then
				/usr/local/StarBoardSoftware/uninstall.sh >>$logfile 2>&1
			fi
		elif [ "$1" = failed-upgrade ]
		then
			exit 1
		fi

    ;;

    failed-upgrade)
    ;;

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