# %W% %G% %U% - %Q%
#ident	"@(#)vxfs:%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

#
# During installation, we set the stack size of all threads to 16k.
# We now remove these changes.
#

cleanup_etcsystem()
{
	ETC_SYSTEM=${BASEDIR}/etc/system
	cp -p ${ETC_SYSTEM} ${BASEDIR}/etc/system.vxfs.preremove
	remove_all=1
	while [ $remove_all = 1 ]
	do
		grep -n vxfs_START ${ETC_SYSTEM} > /dev/null 2>&1
		if [ $? = 0 ]
		then
			start_line=`grep -n vxfs_START ${ETC_SYSTEM} | \
				head -1 | cut -d: -f1`
			start_line=`expr $start_line - 1`
			head -$start_line ${ETC_SYSTEM} > /tmp/system.$$
			end_line=`grep -n vxfs_END ${ETC_SYSTEM} | \
				head -1 | cut -d: -f1`
			if [ "$end_line" != "" ]
			then
				end_line=`expr $end_line + 1`
				tail +$end_line ${ETC_SYSTEM} >> /tmp/system.$$
			fi
			mv /tmp/system.$$ ${ETC_SYSTEM}
		else
			remove_all=0
		fi
	done
}

#
# remove the parallel fsck script installed for 2.6 and 7.
#

cleanup_checkfsys()
{
	OSVER=`uname -r`
	SCRIPT=CHECKVXFSYS
	if [ "$OSVER" = "5.6" -o "$OSVER" = "5.7" ]; then
		rm -f ${BASEDIR}/etc/rc2.d/S01${SCRIPT} \
		      ${BASEDIR}/etc/init.d/${SCRIPT}
	fi
}

check_basedir()
{
	if [ "${BASEDIR}" = "/" -o -z "${BASEDIR}" ]
	then
		echo 0
	else
		echo 1
	fi
}

cleanup()
{
	if [ `check_basedir` -eq 1 ]
	then
		rem_drv -b ${BASEDIR} vxportal > /dev/null 2>&1
		rem_drv -b ${BASEDIR} fdd > /dev/null 2>&1
		rem_drv -b ${BASEDIR} qlog > /dev/null 2>&1
	else
		rem_drv vxportal > /dev/null 2>&1
		rem_drv fdd > /dev/null 2>&1
		rem_drv qlog > /dev/null 2>&1
	fi
	rm -f ${BASEDIR}/dev/vxportal > /dev/null 2>&1
	rm -f ${BASEDIR}/dev/fdd > /dev/null 2>&1
	rm -f ${BASEDIR}/dev/qlog > /dev/null 2>&1
	cleanup_checkfsys
	cleanup_etcsystem
	exit 0
}

#
# Unload and remove QuickLog
# Then update the devlink.tab file
# (if BASEDIR is set & not "/", we're removing on a client so don't unload
#  qlog on this host!)
#
modinfo | grep qlog 2>&1 > /dev/null
if [ $? -eq 0 -a `check_basedir` -eq 0 ]
then
	modunload -i `modinfo | grep qlog | cut -f1,2 -d' '`
	if [ $? -ne 0 ]
	then
		echo "The QuickLog kernel module 'qlog' could not be unloaded."
		echo "You may proceed with the removal of the package,"
		echo "but you may have to reboot your system afterwards."
		echo
		echo "Do you want to proceed with removal of VRTSvxfs [y/n]? \c"
		read ans
		if [ "X" = "X$ans" ]
		then
			ans="n"
		fi
		if [ $ans = "N" -o $ans = "n" ]
		then
			exit 1
		fi
	fi
fi
grep qlog ${BASEDIR}/etc/name_to_major > /dev/null 2>&1
if [ $? -eq 0 ]
then
	if [ `check_basedir` -eq 1 ]
	then
		rem_drv -b ${BASEDIR} qlog > /dev/null 2>&1
		rm -rf ${BASEDIR}/dev/qlog > /dev/null 2>&1
		rm ${BASEDIR}/etc/qlog/config > /dev/null 2>&1
	else
		rem_drv qlog > /dev/null 2>&1
		rm -rf /dev/qlog > /dev/null 2>&1
		rm /etc/qlog/config > /dev/null 2>&1
	fi
fi
grep -v qlog ${BASEDIR}/etc/devlink.tab > ${BASEDIR}/etc/NEW1_devlink.tab
grep -v fdd ${BASEDIR}/etc/NEW1_devlink.tab > ${BASEDIR}/etc/devlink.tab
grep -v vxportal ${BASEDIR}/etc/devlink.tab > ${BASEDIR}/etc/NEW1_devlink.tab
mv ${BASEDIR}/etc/NEW1_devlink.tab ${BASEDIR}/etc/devlink.tab

sync; sleep 1; sync; sleep 1; sync

#
# Unload and remove QIO
# (if BASEDIR is set & not "/", we're removing on a client so don't unload
#  fdd on this host!)
#
modinfo | grep -w fdd 2>&1 > /dev/null
if [ $? -eq 0 -a `check_basedir` -eq 0 ]
then
	echo "vxfdd_nounload/W 0" | adb -kw /dev/ksyms /dev/mem 2>&1 > /dev/null
	modunload -i `modinfo | grep -w fdd | cut -f1,2 -d' '`
	if [ $? -ne 0 ]
	then
		echo "The QuickI/O kernel module 'fdd' could not be unloaded."
		echo "You may proceed with the removal of the package,"
		echo "but you may have to reboot your system afterwards."
		echo
		echo "Do you want to proceed with removal of VRTSvxfs [y/n]? \c"
		read ans
		if [ "X" = "X$ans" ]
		then
			ans="n"
		fi
		if [ $ans = "N" -o $ans = "n" ]
		then
			exit 1
		fi
	fi
fi
grep -w fdd ${BASEDIR}/etc/name_to_major > /dev/null 2>&1
if [ $? -eq 0 ]
then
	if [ `check_basedir` -eq 1 ]
	then
		rem_drv -b ${BASEDIR} fdd
		rm -f ${BASEDIR}/dev/fdd
	else
	echo "vxfdd_nounload/W 0" | adb -kw /dev/ksyms /dev/mem 2>&1 > /dev/null
		rem_drv fdd
		rm -f /dev/fdd
	fi
fi

modinfo | grep vxportal 2>&1 > /dev/null
if [ $? -eq 0 -a `check_basedir` -eq 0 ]
then
	modunload -i `modinfo | grep vxportal | cut -f1,2 -d' '`
fi

modinfo | grep vxfs 2>&1 > /dev/null
if [ $? -ne 0 ]
then
	cleanup
fi

if [ `check_basedir` -eq 0 ]
then
	modunload -i `modinfo | grep vxfs | cut -f1,2 -d' '`
	if [ $? -ne 0 ]
	then
		echo "The VxFS kernel module could not be unloaded."
		echo "You can proceed with the removal of the package,"
		echo "but you may have to reboot your system afterward."
		echo
		echo "Do you want to proceed with removal of VRTSvxfs [y/n]? \c"
		read ans
		if [ "X" = "X$ans" ]
		then
			ans="n"
		fi
		if [ $ans = "N" -o $ans = "n" ]
		then
			exit 1
		fi
	fi
fi

cleanup

