#!/bin/sh
# $NFSV4 means NFSv4 is installed or not.
# $ENABLE_NFS_V3 & $ENABLE_NFS_V4 mean NFS v3 or v4 are enabled or not.
[ -x /usr/sbin/rpc.nfsd ] || exit 0
[ -x /usr/sbin/rpc.mountd ] || exit 0
[ -x /usr/sbin/exportfs ] || exit 0
NFSV4=no
[ -f /usr/sbin/rpc.idmapd ] && NFSV4=yes

# For Service-Binding.
BIND_ADDR=`/sbin/getbindaddr NFS`
PORTMAP_PARAM=
# This function will update the parameter string which is fed to the 'portmap' program for service-binding.
update_portmap_parameter()
{
        # if get "0.0.0.0", the service binding is disabled.
        if [ "x0.0.0.0" != "x$BIND_ADDR" ] ; then
                for ADDR in $BIND_ADDR
                do
                        # Skip the loopback address. ( 127.0.0.1(IPv4) or ::1(IPv6) )
                        if [ "x127.0.0.1" != "x$ADDR" ] && [ "x::1" != "x$ADDR" ]; then
                                PORTMAP_PARAM="$PORTMAP_PARAM -h $ADDR"
                        fi
                done
        fi
}

insert_nfs_users()
{
	i=1
	tmp_nfs_conf="/tmp/nfs.conf.`date +%s`"
	/bin/cp /etc/nfs.conf ${tmp_nfs_conf}
	nfs_conf_cnt=`/usr/bin/wc -l ${tmp_nfs_conf}|/sbin/cut -c1-8`
	while [ ${i} -le ${nfs_conf_cnt} ]
	do
		nfsuser=`/bin/sed -n "${i}p" ${tmp_nfs_conf}|/bin/sed -n "s/\([^:]*\):.*/\1/p"`
		nfsluid=`/bin/sed -n "s/${nfsuser}:[^:]*:\([^:]*\):.*/\1/p" /etc/passwd`
		paramline=`/bin/sed -n "${i}p" ${tmp_nfs_conf}|/bin/sed -n "s/${nfsuser}:\(.*\)/${nfsuser}:${nfsluid}:\1:/p"`
		cmdline="add ${paramline}"
		i=`expr $i + 1`
	done
	/bin/rm -f ${tmp_nfs_conf}
}

restart_mountd()
{
	mountd_port_enabled=`/sbin/getcfg NFS "Mount Port Enable" -u -d NONE`

	echo "Shutting down NFS mountd: "
	/sbin/daemon_mgr rpc.mountd stop /usr/sbin/rpc.mountd 
	/bin/rm -f /var/lock/subsys/rpc.mountd
	/bin/kill -9 `pidof rpc.mountd` 1>/dev/null 2>/dev/null

	if [ "x${mountd_port_enabled}" = "xTRUE" ]; then
		mountd_port=`/sbin/getcfg NFS "Port Number" -u -d 0`
		if [ "x${mountd_port}" = "x0" ]; then
			echo "Starting NFS mountd. Mountd port number assigned automatically."
			/sbin/daemon_mgr rpc.mountd start "/usr/sbin/rpc.mountd -F 2>/dev/null 1>/dev/null &"
			/bin/touch /var/lock/subsys/rpc.mountd
		else
			echo "Starting NFS mountd. Mountd port number = ${mountd_port}."
			/sbin/daemon_mgr rpc.mountd start "/usr/sbin/rpc.mountd -p ${mountd_port} -F 2>/dev/null 1>/dev/null &"
			/bin/touch /var/lock/subsys/rpc.mountd
		fi
	elif [ "x${mountd_port_enabled}" = "xFALSE" ]; 	then
		echo "Starting NFS mountd. Mountd port number assigned automatically."
		/sbin/daemon_mgr rpc.mountd start "/usr/sbin/rpc.mountd -F 2>/dev/null 1>/dev/null &"
		/bin/touch /var/lock/subsys/rpc.mountd
	else
		echo "Starting NFS mountd. Mountd port number assigned automatically."
		/sbin/daemon_mgr rpc.mountd start "/usr/sbin/rpc.mountd -F 2>/dev/null 1>/dev/null &"
		/bin/touch /var/lock/subsys/rpc.mountd
	fi
}


# See how we were called.
case "$1" in
  reexport)
	ENABLE_NFS_V3=`/sbin/getcfg NFS Enable -u -d FALSE`
	if [ "x$NFSV4" = "xyes" ]; then
		ENABLE_NFS_V4=`/sbin/getcfg NFS Enable_V4 -u -d FALSE`
	else
		ENABLE_NFS_V4=FALSE
	fi
	if [ "x$ENABLE_NFS_V3" = "xFALSE" ] && [ "x$ENABLE_NFS_V4" = "xFALSE" ]; then
		echo " disabled."
		exit 0
	fi

	/bin/mkdir /var/lib/nfs -p
	# Re-export /etc/exprotfs.
	tmp_smb_conf="/tmp/smb.conf.`date +%s`"
	/bin/cp /etc/smb.conf ${tmp_smb_conf}
	/sbin/gen_exports > /etc/exports
	#/bin/sed -n 's/^path = \(\/share\/.*\)/\1 \*\(rw,async,no_root_squash\)/p' ${tmp_smb_conf} > /etc/exports
	#/bin/sed -n 's/^path = \(\/share\/.*\)/\1 \*\(rw,sync,no_root_squash,all_squash,anonuid=100,anongid=100\)/p' /tmp/smb.conf.tmp > /etc/exports
	#/bin/grep -v "global" /tmp/smb.conf.tmp | /bin/grep -v "printers" | /bin/sed -n 's/^\[\(.*\)\]/\/\1 \*\(rw,sync,no_root_squash,anonuid=100,anongid=100\)/p' > /etc/exports
	/usr/sbin/exportfs -r 1>/dev/null 2>/dev/null
	/bin/rm -f ${tmp_smb_conf}
	#/bin/kill -s SIGHUP `pidof nasnfsd` 1>/dev/null 2>/dev/null
	echo "re-export."
	;;
  start)
	# Start daemons.
	[ -f /var/lock/subsys/rpc.nfsd ] && $0 stop
	[ -d /var/lib/nfs/sm ] || /bin/mkdir -p /var/lib/nfs/sm
	[ -d /var/lib/nfs/sm.bak ] || /bin/mkdir -p /var/lib/nfs/sm.bak

	TCP_PAYLOAD_SIZE=`/sbin/getcfg NFS TCP_PAYLOAD_SIZE -d FALSE`
	UDP_PAYLOAD_SIZE=`/sbin/getcfg NFS UDP_PAYLOAD_SIZE -d FALSE`
	[ ${TCP_PAYLOAD_SIZE} != "FALSE" ]&& echo $TCP_PAYLOAD_SIZE > /proc/net/rpc/tcp_payload_size
	[ ${UDP_PAYLOAD_SIZE} != "FALSE" ]&& echo $UDP_PAYLOAD_SIZE > /proc/net/rpc/udp_payload_size

	touch /etc/config/nfssetting

	echo -n "Starting NFS services: " 

	ENABLE_NFS_V3=`/sbin/getcfg NFS Enable -u -d FALSE`
	if [ "$NFSV4" = "yes" ]; then
		ENABLE_NFS_V4=`/sbin/getcfg NFS Enable_V4 -u -d FALSE`
	else
		ENABLE_NFS_V4=FALSE
	fi
	if [ $ENABLE_NFS_V3 = FALSE ] && [ $ENABLE_NFS_V4 = FALSE ]
	then 
		echo " disabled."
		exit 0
	fi
	echo -n "Starting portmapper:"
	#start-stop-daemon --start --quiet --exec /sbin/portmap
	# Update the portmap parameter for Service-binding.
	update_portmap_parameter
	/usr/bin/portmap $PORTMAP_PARAM > /dev/null 2>/dev/null
	echo -n "."
	echo

	if grep -qs nfsd /proc/filesystems ; then
		if ! grep -qs "^nfsd[[:space:]]/proc/fs/nfsd[[:space:]]" /proc/mounts ; then
			mount -t nfsd nfsd /proc/fs/nfsd
		fi
	fi

	if [ "$ENABLE_NFS_V4" = "TRUE" ] && grep -qs rpc_pipefs /proc/filesystems ; then
		if ! grep -qs "^rpc_pipefs[[:space:]]/var/lib/nfs/rpc_pipefs[[:space:]]" /proc/mounts ; then
			[ -d /var/lib/nfs/rpc_pipefs ] || mkdir -p /var/lib/nfs/rpc_pipefs
			mount -t rpc_pipefs rpc_pipefs /var/lib/nfs/rpc_pipefs
		fi
	fi

	$0 reexport

	echo -n "Starting NFS quotas: "
	echo -n " rpc.rquotad"
	/sbin/daemon_mgr rpc.rquotad start /usr/sbin/rpc.rquotad 1>/dev/null 2>/dev/null
	/bin/touch /var/lock/subsys/rpc.rquotad
	echo "."

	echo -n "Starting NFS mountd daemon: "
	restart_mountd

	echo -n "Starting NFS daemon: "
        echo " rpc.nfsd"
	NO_V3=""
	if [ $ENABLE_NFS_V3 = FALSE ]; then
		NO_V3="-N 3 -N 2"
	fi
	NO_V4=""
	if [ $ENABLE_NFS_V4 = FALSE ]; then
		NO_V4="-N 4"
	fi
        /sbin/daemon_mgr nfsd start "/usr/sbin/rpc.nfsd $NO_V3 $NO_V4 8" 1>/dev/null 2>/dev/null
        /bin/touch /var/lock/subsys/rpc.nfsd

	if [ "$ENABLE_NFS_V4" = "TRUE" ] ; then
		/usr/sbin/rpc.idmapd 1>/dev/null 2>/dev/null
	fi

        echo -n "Starting NFS lockd status:"
        /usr/sbin/rpc.statd 1>/dev/null 2>/dev/null
        echo "."
	;;
  stop)
	# Stop daemons.
	echo -n "Shutting down NFS daemon: "
	echo -n " rpc.nfsd"
	[ "$NFSV4" = "yes" ] && /bin/kill -9 `pidof rpc.idmapd` 1>/dev/null 2>/dev/null
	/sbin/daemon_mgr nfsd stop /usr/sbin/rpc.nfsd
	/bin/rm -f /var/lock/subsys/rpc.nfsd
	/bin/kill -9 `/sbin/pidof nfsd` 1>/dev/null 2>/dev/null
	/bin/kill -9 `/sbin/pidof statd` 1>/dev/null 2>/dev/null
	/bin/kill -9 `/sbin/pidof rpc.statd` 1>/dev/null 2>/dev/null
	echo "."

	echo -n "Shutting down NFS mountd: "
	echo -n " rpc.mountd"
	/sbin/daemon_mgr rpc.mountd stop /usr/sbin/rpc.mountd 
	/bin/rm -f /var/lock/subsys/rpc.mountd
	/bin/kill -9 `pidof rpc.mountd` 1>/dev/null 2>/dev/null
	echo "."

	echo -n "Shutting down NFS quotas: "
	echo -n " rpc.rquotad"
	/sbin/daemon_mgr rpc.rquotad stop /usr/sbin/rpc.rquotad
	/bin/rm -f /var/lock/subsys/rpc.rquotad
	/bin/kill -9 `pidof rpc.rquotad` 1>/dev/null 2>/dev/null
	echo "."

	exportfs -au

	echo -n "Stopping portmap"
	/bin/kill -9 `/sbin/pidof portmap` 1>/dev/null 2>/dev/null	
	echo "."

	[ "$NFSV4" = "yes" ] && /sbin/umount_nfs_mp >/dev/null 2>&1
	;;
  restart)
	$0 stop
	$0 start
	;;
  remountd)
	$0 reexport
	echo "Restarting rpc.mountd: "
	restart_mountd
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reexport|remountd}"
	exit 1
esac

exit 0

