#!/bin/sh
PKGSTATUSFILE=/etc/zyxel/pkg_conf/status
PKGVOLPATH=`grep "NFS" ${PKGSTATUSFILE} |grep "Installed-Rule" |awk -F":" '{print $2}' |sed 's/\/$//g' |sed 's/ //g'`
PKGPATH=`grep "NFS" ${PKGSTATUSFILE} |grep "Installed-Rule" |awk -F":" '{print $2}' |sed 's/\/$//g' |sed 's/ //g'`
BINPATH="${PKGPATH}/bin"
. /etc/profile
bsname=${0##/*/}
CONTROLFILE="${PKGPATH}/zypkg_conf/info/NFS.control"

psax="ps"
NICE_VALUE="20"
EXPORTSFILE="/etc/exports"
NFS4ROOTITEM="fsid=0"
INSTVOL=`echo ${PKGPATH} | awk -F'/' '{print $3}'`

if [ ! -r ${PKGPATH}/etc/exports ]; then
	echo "${PKGPATH}/etc/exports does not exist, create a bare-bones version and continue" >> /tmp/michael.txt
	touch ${PKGPATH}/etc/exports
	chmod u+rw,g+r,o+r ${PKGPATH}/etc/exports
fi

chk_fsid_path() {
	if [ -e $EXPORTSFILE ]; then
		DNUM=`cat $EXPORTSFILE | grep -n "sysvol" | grep ${NFS4ROOTITEM} | awk -F':' '{print $1}'`
		echo ${DNUM}
		if [ "${DNUM}x" != "x" ]; then
			sed -i ${DNUM}d $EXPORTSFILE
		fi
	fi
}

nfs_export_ini() {
        if [ -e /etc/exports ]; then
                cat /etc/exports | grep "fsid=0"
                if [ "$?" != "0" ];then
                        echo "/i-data/${INSTVOL}/nfs *(rw,sync,crossmnt,fsid=0,no_subtree_check,wdelay,no_root_squash) #" >> /etc/exports
                fi
        else
			echo "/i-data/${INSTVOL}/nfs *(rw,sync,crossmnt,fsid=0,no_subtree_check,wdelay,no_root_squash) #" > /etc/exports
		fi
}

start_nfs() {
	if [ ! -f /etc/netconfig ]; then
		ln -s ${PKGPATH}/etc/netconfig /etc/netconfig
		ln -s ${PKGPATH}/etc/exports /etc/exports
		ln -s ${PKGPATH}/etc/services /etc/services		
		mkdir -p /var/lib/nfs/sm
		mkdir -p /var/lib/nfs/sm.bak
	fi
	chk_fsid_path
	# Initialize export file for NFSv4
	nfs_export_ini
	runrpcbind=`${psax}|grep -v grep|grep rpcbind`
	if [ "${runrpcbind}" == "" ]; then
		nice -n ${NICE_VALUE} ${BINPATH}/rpcbind
	fi

	#Use the build-in modules to instead of nfsd.ko and exportfs.ko
	#lsmod_out=`lsmod`
	#for i in exportfs nfsd; do
	#	needins=`echo "${lsmod_out}"|grep ${i}`
	#	if [ "${needins}" == "" ]; then
	#		insmod /usr/local/zy-pkgs/lib/modules/${i}.ko
	#	fi
	#done

	mountnfsd=`cat /proc/mounts|grep /proc/fs/nfsd`
	if [ "${mountnfsd}" == "" ]; then
		mount -t nfsd nfsd /proc/fs/nfsd
	fi
	echo -n "Starting NFS:"
	#Start Steps from README

	#A/  mount -t nfsd nfsd /proc/fs/nfsd
	#I do it in rcS

	#B/ svcgssd ; idmapd
	#svcgssd is only needed if exportfs NFS filesystem with crypto-security (Kerberos or SPKM3).
	#echo -n " rpc.svcgssd"; rpc.svcgssd
	#idmapd is only needed for NFSv4 support
	#echo -n " rpc.idmapd"; rpc.idmapd

	#C/ exportfs -av ; rpc.mountd
	#It is important that exportfs be run before mountd so that mountd is working from current information (in /var/lib/nfs/etab)
	echo " exportfs"; ${BINPATH}/exportfs -arv
	#any NFS requests that arrive before mountd is started will get replied to with a 'Stale NFS File handle' error.
	echo -n " rpc.mountd"; nice -n ${NICE_VALUE} ${BINPATH}/rpc.mountd

	#D/ rpc.statd --no-notify
	#If notify requests are sent out before nfsd start, clients may try to reclaim locks and, on finding that lockd isn't running
	#, they will give up and never reclaim the lock. rpc.statd is only needed for NFSv2 and NFSv3 support.
	#Michael: prevent rpc.statd take port 2049, I start it after nfsd
	#	rpc.statd 1246   nobody    8u  IPv4   2189       UDP *:2049
	#echo -n " rpc.statd"; rpc.statd --no-notify

	#E/ rpc.nfsd
	#When nfsd starts it will start lockd. If lockd then receives a lock request it will communicate with statd.
	echo -n " rpc.nfsd"; nice -n ${NICE_VALUE} ${BINPATH}/rpc.nfsd
	echo -n " rpc.statd"; nice -n ${NICE_VALUE} ${BINPATH}/rpc.statd --no-notify

	#F/ sm-notify
	#This will notify any client which might have locks from before a reboot to try to reclaim their locks.
	#sm-notify is only needed for NFSv2 and NFSv3 support.
	echo -n " sm-notify"; nice -n ${NICE_VALUE} ${BINPATH}/sm-notify

	result=`${psax}|grep nfsd|grep -v grep`
	if [ "${result}" == "" ]; then
		rm -f /etc/zyxel/storage/enable_nfs.flag
		echo ": failed"
	else
		echo ": done"
	fi
}
stop_nfs() {
	echo -n "Shutting down NFS:"
	killall sm-notify 2>/dev/null
	killall rpc.statd 2>/dev/null
	echo -n " rpc.mountd"; killall rpc.mountd
	echo -n " nfsd"; kill `${psax}|grep nfsd|grep -v grep|awk '{print $1}'`
	echo -n " exportfs"; ${BINPATH}/exportfs -auv
	#killall rpc.idmapd 2>/dev/null
	#killall rpc.svcgssd 2>/dev/null

	mountdpid=`${psax}|grep mountd|grep -v grep|awk '{print $1}'`
	nfsdpid=`${psax}|grep nfsd|grep -v grep|awk '{print $1}'`
	killcount=1
	if [ "${mountdpid}" != "" ] || [ "${nfsdpid}" != "" ]; then
		busy=1
		kill -9 ${mountdpid} ${nfsdpid} 2>/dev/null
	else
		busy=0
	fi
	#while [ ${killcount} -le 9 ] && [ "${busy}" == "1" ]; do
	while [ ${killcount} -le 2 ] && [ "${busy}" == "1" ]; do
		sleep ${killcount}
		killcount=$(($killcount + 1))
		mountdpid=`${psax}|grep mountd|grep -v grep|awk '{print $1}'`
		nfsdpid=`${psax}|grep nfsd|grep -v grep|awk '{print $1}'`
		if [ "${mountdpid}" != "" ]; then
			busy=1
			sleep 1
			echo -n [mountd busy] kill -9 ${mountdpid}; kill -9 ${mountdpid} 2>/dev/null
		else
			if [ "${nfsdpid}" != "" ]; then
				busy=1
				sleep 1
				echo -n [nfsd busy] kill -9 ${nfsdpid}; kill -9 ${nfsdpid} 2>/dev/null
			else
				busy=0
			fi
		fi
	done

	checkrpcbind=`${psax}|grep rpcbind|grep -v grep`
	checkrpccount=1
	
	while [ "${checkrpcbind}" != "" ] && [ ${checkrpccount} -le 3 ];do
		killall rpcbind 2>/dev/null
		sleep ${checkrpccount}
	        checkrpccount=$(($checkrpccount + 1))
	        checkrpcbind=`${psax}|grep rpcbind|grep -v grep`
	done

	if [ "${checkrpcbind}" == "" ];then
	        echo ": done"
	else
	        echo ": failed"
	fi
}

case "$1" in
	startup)
		NICE_VALUE="17"
		#zyshclient -p 150 -e "nfs start"
		if [ -e /etc/zyxel/storage/enable_nfs.flag ]; then
			start_nfs
		else
			echo "${bsname}:no action"
		fi
		;;
	shutdown)
		#zyshclient -p 150 -e "nfs stop"
		stop_nfs
		;;
	enable)
		NICE_VALUE="20"
		#zyshclient -p 150 -e "nfs enable"
		start_nfs
		touch /etc/zyxel/storage/enable_nfs.flag
		;;
	disable)
		#zyshclient -p 150 -e "nfs disable"
		stop_nfs
		rm -f /etc/zyxel/storage/enable_nfs.flag
		;;
	status)
		echo "Built-in"
		;;
	getlink)
		echo "nfsWin"
		;;
	start)
		NICE_VALUE="20"
		mountdpid=`${psax}|grep mountd|grep -v grep|awk '{print $1}'`
		nfsdpid=`${psax}|grep nfsd|grep -v grep|awk '{print $1}'`
		if [ "${mountdpid}" != "" ] || [ "${nfsdpid}" != "" ]; then
			stop_nfs
			start_nfs
		else
			start_nfs
		fi
		;;
	stop)
		stop_nfs
		;;
	restart)
		NICE_VALUE="20"
		stop_nfs
		start_nfs
		;;
	version)
		VERSION=`grep "Version:" ${CONTROLFILE} | awk -F"zypkg" '{print $2}'`
		return ${VERSION}
		;;
	*)
		echo $"Usage: nfs {start|stop|restart}"
		exit 1
		;;
esac

exit 0
