#!/bin/sh
#
# code by ngc1927
#
export PATH=$PATH:.:/var/bin:/var/sbin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:.:/var/lib
# set modules to load
MODULES="/var/lib/modules/"
# set server start
SERVER="4"

cd /tmp/

# NOT CHANGE AFTER THIS ************************************************************
start_nfs() {
	# load modules ..if not
	check=`lsmod | grep -c "^exportfs"`
	if [ -r $MODULES/exportfs.ko ] && [ $check = 0 ]
	then
		insmod $MODULES/exportfs.ko
	fi
	check=`lsmod | grep -c "^nfsd"`
	if [ -r $MODULES/nfsd.ko ] && [ $check = 0 ]
	then
		insmod $MODULES/nfsd.ko
	fi
	# start programs
	start-stop-daemon --start --quiet --exec /var/sbin/rpc.portmap
	sleep 2
	start-stop-daemon --start --quiet --exec /var/sbin/rpc.mountd
	sleep 2
	start-stop-daemon --start --quiet --exec /var/sbin/rpc.statd
	sleep 2
	/var/sbin/exportfs -r
	sleep 2
	start-stop-daemon --start --quiet --exec /var/sbin/rpc.nfsd $SERVER
	sleep 2
	echo "NFSD Started"
}

stop_nfs() {
	kill -9 `pidof lockd` 1>/dev/null 2>/dev/null
	kill -9 `pidof nfsd` 1>/dev/null 2>/dev/null
	kill -9 `pidof nfsd` 1>/dev/null 2>/dev/null
	kill -9 `pidof nfsd` 1>/dev/null 2>/dev/null
	kill -9 `pidof nfsd` 1>/dev/null 2>/dev/null
	kill -9 `pidof rpc.statd` 1>/dev/null 2>/dev/null
	kill -9 `pidof rpc.mountd` 1>/dev/null 2>/dev/null
	kill -9 `pidof rpc.portmap` 1>/dev/null 2>/dev/null
	rmmod nfsd 1>/dev/null 2>/dev/null
	rmmod exportfs 1>/dev/null 2>/dev/null
	echo "NFSD Stopped"
}

case "$1" in
	start)
		start_nfs
		;;
	stop)
		stop_nfs
		;;
	restart)
		$0 stop
		sleep 5
		$0 start
		;;
	*)
		echo "usage: {start|stop|restart}"
		;;
esac

exit 0

