#!/bin/sh
PATH="/sbin":${PATH}
rc=0
NCS_VAR=/var/opt/novell/ncs

DEVFILE="/dev/adminfsdrv"
ADMINFSD_BIN=/opt/novell/adminfs/sbin/adminfsd


. /etc/rc.status
rc_reset

if [ -e /proc/ncs/cluster ]
then
    cluster leave
    rc=$?
    if [ $rc -ne 0 ] ; then
        echo "cluster leave failed"
        exit 1;
    fi
fi

killproc ncs-emaild
killproc ncsdkd
killproc NCS_NDSD_monitord

echo
echo "#---------------------------------------------------------------+"
echo "#                                                               |"
echo "#   Please wait for the Novell Cluster Services KOs to unload   |"
echo "#                                                               |"
echo "#---------------------------------------------------------------+"
echo

modprobe -r cma cmsg crm cvb css vipx sbd gipc vll sbdlib clstrlib ncs_timer

# Unloading adminfs

function mount_status() {
	mp=`mount | awk '/ type adminfs /{print $3}'`
	if [ -n "$mp" ] ; then
		return 0 #mounted
	else
		return 1 #not mounted
	fi
}

#unmount all mount points for adminfs
if mount_status ; then
	for m in $mp ; do
		echo -n "Unmounting adminfs from $m ... "
		if umount $m 1>&2 2>/dev/null ; then
			echo -n "unmounted"
			rc_status -v
		else
			echo -n "unable to umount"
			rc_failed 1
			rc_status -v
			rc_exit				
		fi		
	done
else
	echo -n "adminfs is not mounted"
	rc_status -v
fi

# Stopping daemon
echo -n "Stopping adminfs daemon ... "
killproc $ADMINFSD_BIN ;
sleep 1;
pgrep adminfsd > /dev/null;
if [ $? -eq 1 ] ; then
	echo -n "done"
	rc_status -v
else
	echo -n "still running"
	rc_status -v
fi

modprobe -r adminfs adminfsdrv

#removing device file
if [ -e "${DEVFILE}" ] ; then
	echo -n "Removing ${DEVFILE} ... "
	rm -f ${DEVFILE}
	echo -n "done"
	rc_status -v
fi


# Temporary solution to make the immediate start not to see inconsistent epoch by the master.
#sleep 20

echo "Novell Cluster Services Unloaded"
echo

exit $rc
