# %W% %G% %U% - %Q%
#ident "%Z%unixvm:%M% %I%"

# Copyright (c) 2000 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
# 
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF VERITAS SOFTWARE.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF VERITAS SOFTWARE.
# 
#               RESTRICTED RIGHTS LEGEND
# USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT IS
# SUBJECT TO RESTRICTIONS AS SET FORTH IN SUBPARAGRAPH
# (C) (1) (ii) OF THE RIGHTS IN TECHNICAL DATA AND
# COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013.
#               VERITAS SOFTWARE
# 1600 PLYMOUTH STREET, MOUNTAIN VIEW, CA 94043


#
# Perform preremove processing for the VxVM package.
#

FAILURE=1	# fatal error

DRVSUFFIX=`uname -s`_`uname -r`

tab='	'

VOLBUSY="\
ERROR: The Volume Daemon (vxconfigd) could not be stopped.  The
${NAME} package can not be removed.  Kill the vxconfigd process
and try again."

VOLISROOT="\
ERROR: The root device is configured as a volume. The ${NAME}
package can not be removed.  Please run the command:
	/etc/vx/bin/vxunroot
before removing the package."

VOLOPEN="\
ERROR: The following volumes are still open:

	"

VOLOPENASK="

Please stop these volumes before removing this package."

message()
{
	echo ""
	echo "$@"
	echo ""
}


#
# check if / or swap or /usr is encapsulated
# if so then do not allow for VM to be uninstalled
#
if [ -z "$BASEDIR" -o "$BASEDIR" = / ]
then
	echo "
	Checking for system volumes:"
 
	okay=yes
	echo "${tab}swap${tab}... \c"
	swapvol="`swap -l | sed -n 's%^/dev/vx/dsk/\([^         ]*\) .*%\1%p'`"
	if [ -n "$swapvol" ]
	then
        	case $swapvol in
        	*"$nl"*)        echo "defined on volumes:" ${swapvol}.;;
        	*)              echo "defined on volume $swapvol.";;
        	esac
        	okay=no
	fi
	for dir in / /usr /opt /var
	do
        	dirvol=`mount | awk '
                	\$1 == "'"$dir"'" {
                        	if ($3 ~ /^\/dev\/vx\/dsk\//) {
                                	print $3
                        	} else {
                                	print ""
                        	}
                        	exit
                	}'`
        	if [ -n "$dirvol" ]
        	then
                	dirvol="`expr "$dirvol" : '.*/\([^/]*\)'`"
                	echo "${tab}${dir}${tab}... defined on volume ${dirvol}."
                	okay=no
        	fi
	done
	if [ "$okay" != yes ]
	then
        	echo "
The root (/) file system and/or other system file systems are encapsulated
by VxVM.  Continuing with the VxVM package removal will leave the system
in an unbootable state.  The Volume Manager package will not be removed.
 
The above mentioned volumes have to be converted back into regular disk 
partitions before the VxVM package can be removed.  Use the VxVM vxunroot 
(vxunroot(1m)) utility to convert the above file systems from VxVM volumes 
to regular partitions.

If this is part of an upgrade process, use the VxVM upgrade_start script,
and follow the upgrade instructions in the Volume Manager VxVM Installation 
Guide.
"
 
        	exit ${FAILURE} 
	else
		echo "\n"
	fi

	#
	# Check to see if volumes are open
	#
	if [ -x $BASEDIR/usr/sbin/vxprint ]
	then
		OPENVOLS=`$BASEDIR/usr/sbin/vxprint -QAqne v_open 2>/dev/null`
		if [ $? -eq 0 -a ! -z "$OPENVOLS" ]
		then
			echo "${VOLOPEN}" ${OPENVOLS} "$VOLOPENASK"
			exit ${FAILURE}
		fi
	fi

	#
	# Stop vxconfigd if it is running.
	#
	if [ -x $BASEDIR/usr/sbin/vxdctl ]
	then
		x=`$BASEDIR/usr/sbin/vxdctl mode 2> /dev/null`
		if [ ! -z "$x" ] && [ "X$x" != "Xmode: not-running" ]
		then
			echo "Stopping vxconfigd..."
			$BASEDIR/usr/sbin/vxdctl stop
		fi
		$BASEDIR/usr/sbin/vxdctl -k stop 2> /dev/null
	fi

	#
	# Stop host_infod if it is running.
	# no much checks for now.
	echo "Stopping the host information RPC daemon"
	$BASEDIR/etc/rc2.d/S94vxnm-host_infod stop
	# RPC host info daemon
fi

#
# Remove VxVM drivers.
#
for MODULE in vxspec vxio vxdmp
do
	if modinfo | grep " $MODULE (" > /dev/null
	then
		echo "Unloading $MODULE driver..."
		if [ -z "$BASEDIR" -o "$BASEDIR" = / ]
		then
			rem_drv $MODULE >/dev/null 2>&1
		else
			rem_drv -b $BASEDIR $MODULE >/dev/null 2>&1 
		fi
		if [ $? != 0 ]
		then
			message "\
Unable to unload $MODULE driver.  You should reboot the system to
ensure that the driver is fully unloaded."
			/usr/ucb/touch $PKGSAV/reboot
		fi
	else
		echo "Removing $MODULE driver..."
		if [ -z "$BASEDIR" -o "$BASEDIR" = / ]
		then
			rem_drv $MODULE >/dev/null 2>&1
		else
			rem_drv -b $BASEDIR $MODULE >/dev/null 2>&1 
		fi
	fi
	# Get rid of the copies we made in postinstall
	MODPATH=$BASEDIR/kernel/drv/$MODULE
	if [ -f $MODPATH ]
	then
		echo "Removing $MODPATH"
		rm -rf $MODPATH > /dev/null 2>&1
	fi
	MODPATH=$BASEDIR/kernel/drv/sparcv9/$MODULE
	if [ -f $MODPATH ]
	then
		echo "Removing $MODPATH"
		rm -rf $MODPATH > /dev/null 2>&1
	fi
done

#
# Remove changes from /etc/devlink.tab
#
OLDFILE=$BASEDIR/etc/devlink.tab
NEWFILE=$BASEDIR/etc/NEW.devlink.tab

echo "Removing changes to $OLDFILE..."
egrep -v "vxspec|vxdmp" $OLDFILE > $NEWFILE 2>&1
if [ $? -ne 0 ]
then
	message "\
Unable to remove vxspec and/or vxdmp driver lines from:
\t${OLDFILE}
Please remove the changes manually."

else
	mv $NEWFILE $OLDFILE 2>/dev/null
	if [ $? -ne 0 ]
	then
		message "\
File could not be updated:
\t${OLDFILE}
Please check the file for correctness."
	fi
fi
rm -f $NEWFILE

#
# Undo the changes to /etc/system.
#
OLDFILE=$BASEDIR/etc/system
NEWFILE=$BASEDIR/etc/NEW.system

echo "Removing changes to $OLDFILE..."

#egrep -v "vxvm|vxio|vxspec|vxdmp" $OLDFILE > $NEWFILE 2>/dev/null
START=`egrep -n vxvm_START $OLDFILE | awk -F: '{print $1}'`
END=`egrep -n vxvm_END $OLDFILE | awk -F: '{print $1}'`
cat $OLDFILE | sed -e "$START,$END d" > $NEWFILE 2>/dev/null
if [ $? -ne 0 ]
then
	message "\
Error removing vxio vxspec and/or vxdmp lines from:
\t${OLDFILE}
Please remove the lines manually."

else
	mv $NEWFILE $OLDFILE 2>/dev/null
	if [ $? -ne 0 ]
	then
		message "\
File could not be updated:
\t${OLDFILE}
Please check the file for correctness."

	fi
fi
rm -f $NEWFILE

echo "Removing files under /var/vxvm..."
rm -rf $BASEDIR/var/vxvm/tempdb

echo "Removing vxconfigd..."
rm -rf $BASEDIR/usr/sbin/vxconfigd > /dev/null 2>&1
rm -rf $BASEDIR/sbin/vxconfigd > /dev/null 2>&1

# since this file is not in the pkg prototype file,
# remove it manually
rm -rf $BASEDIR/usr/lib/vxvm/diag.d/vxautoconfig > /dev/null 2>&1

echo 
exit 0
