# @(#)src/unix/pagecache/sol/common/pkg/vxfs/preremove	3.11.55.1 08/28/02 10:54:24
#ident "@(#)vxfs:src/unix/pagecache/sol/common/pkg/vxfs/preremove	3.11.55.1"
#
# (c) Copyright 1991-2002 VERITAS Software Corporation. All rights reserved.
#

#
# 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
}

#
# remove the loadlicense script that executes vxenablef
#

cleanup_ldlic()
{
	SCRIPT=VXFSLDLIC
	rm -f ${BASEDIR}/etc/rc2.d/S01${SCRIPT} \
		${BASEDIR}/etc/init.d/${SCRIPT}
}
	

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_ldlic
	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 > /dev/null 2>&1
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 > /dev/null 2>&1
if [ $? -eq 0 -a `check_basedir` -eq 0 ]
then
	echo "vxfdd_nounload/W 0" | adb -kw /dev/ksyms /dev/mem > /dev/null 2>&1
	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 > /dev/null 2>&1
		rem_drv fdd
		rm -f /dev/fdd
	fi
fi

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

modinfo | grep vxfs > /dev/null 2>&1
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

