#	ATT:#ident	"initpkg:init.d/rfs	1.18"	
	
#ident	"@(#)initpkg/init.d:rfs	25.1"


USAGE="Usage: /etc/init.d/rfs {start | stop | rfstart}"

if [ ! -d /usr/bin ]
then			# /usr/not mounted ??
	exit 
fi
case "$1" in
'start')

	if [ ! -r /usr/nserve/domain ]
	then
		echo $0: /usr/nserve/domain not initialized >/dev/console
		exit 1
	fi
	if [ ! -r /usr/nserve/rfmaster ]
	then
		echo  $0: /usr/nserve/rfmaster not initialized >/dev/console
		exit 1
	fi

	if [ ! -r /usr/nserve/loc.passwd ]
	then
		echo  "$0: /usr/nserve/loc.passwd is not initialized;
execute rfstart to initialize /usr/nserve/loc.passwd.">/dev/console
		exit 1
	fi
	if [ -d /usr/adm/log ]
	then
		LOG=/usr/adm/log/rfuadmin.log
	else
		LOG=/usr/adm/rfuadmin.log
	fi
	if [ -f ${LOG} ]
	then
		trap 'rm -f /usr/tmp/rfu$$log;exit' 0 1 2 3 15
		tail -50 ${LOG} >/usr/tmp/rfu$$log
		cp /usr/tmp/rfu$$log ${LOG}
	fi
	if [ -x /usr/bin/rfstart ]
	then
		if [ -x /bin/setpgrp ]
		then
			/bin/setpgrp sh /etc/init.d/rfs rfstart&
		else
			/usr/lbin/setpgrp sh /etc/init.d/rfs rfstart&
		fi
	else
		echo $0: /usr/bin/rfstart not found >/dev/console
		exit 1
	fi
	;;

'rfstart')
	trap 'rm -f /usr/tmp/rfs$$;exit' 0 1 2 3 15
	stat=1
	retries=0
	while [ ${stat} -eq 1 ]
	do
		/usr/bin/rfstart </dev/console >/dev/console 2>/usr/tmp/rfs$$
		stat=$?
		case ${stat} in
		0)
			/bin/sh /etc/init.d/adv start
			/etc/rmountall /etc/fstab
			echo "\n*** RFS initialized"  >/dev/console
			exit 0
			;;
		1)
			# can't contact primary; wait for it
			if [ ${retries} -eq 0 ]
			then
				sh -c "echo '\n*** RFS not initialized: \c'"  >/dev/console
				sh -c "echo Waiting for Primary Name Server"  >/dev/console
			fi
			retries=`/bin/expr ${retries} + 1`
			sleep 60
			;;
		*)
			sh -c "cat /usr/tmp/rfs$$ >/dev/console"
			exit 1
			;;
		esac
	done
	;;

'stop')
 #	kill the rfs start daemon if it is still running
 	pid=`/bin/ps -ef 2>/dev/null | /bin/grep rfstart | /bin/grep -v grep \
 			| /bin/sed -e 's/  *[a-z0-9]*  *//' -e 's/ .*//'`
 	if [ "${pid}" != "" ]
 	then
 		/bin/kill ${pid}
		exit
 	fi

 	pid=`/bin/ps -ef 2>/dev/null | /bin/grep rfudaemon | /bin/grep -v grep \
 			| /bin/sed -e 's/  *[a-z0-9]*  *//' -e 's/ .*//'`
	if [ "${pid}" = "" ]
	then
		exit
	fi

	if [ -x /usr/bin/rfstop ]
	then
		/usr/bin/rfstop
		if [ $? -ne 0 ]
		then
			echo $0: Unable to terminate rfs >/dev/console
		fi
	else
		echo $0: /usr/bin/rfstop not found >/dev/console
	fi
	;;
*)
	echo ${USAGE}
	exit 1
	;;
esac
