#!/bin/sh
#
# nfs           This shell script takes care of starting and stopping
#               the NFS services.
#
# chkconfig: 35 60 40
# description: NFS is a popular protocol for file sharing across TCP/IP \
#              networks. This service provides NFS server functionality, \
#              which is configured via the /etc/exports file.
# probe: true

[  -e /etc/exports  ]  ||  exit 0

# defaults, overriden if /etc/config.d/nfs exists and is filled
# with NUMSERVERS and/or MOUNTDOPTS
CONFIGFILE="/etc/config.d/nfs";

#NUMSERVERS=8
((AUTONUMSERVERS=$(cat /proc/cpuinfo  | grep "model name" | wc -l)*3))

if [ -f $CONFIGFILE ]; then
  . $CONFIGFILE
fi

NUMSERVERS=${NUMSERVERS:-$AUTONUMSERVERS}
MOUNTDOPTS=${MOUNTDOPS:-""}

case $1 in
  start)  echo "$1ing NFS services"

    if ps -C nfsd > /dev/null;  then
      echo -n "NFS running already. Use stop first or restart..."
      echo -e $RESULT_FAIL
      exit 0
    fi

    ps -C portmap > /dev/null || /etc/init.d/portmap start

    # mount /proc/fs/nfsd
    if ! grep "/proc/fs/nfsd" /proc/mounts > /dev/null; then
      if ! mount -t nfsd nfsd /proc/fs/nfsd; then
        echo -n "Mounting /proc/fs/nfsd failed."
	echo -e $RESULT_FAIL
	exit 0
      fi
    fi

    # mount rpc_pipefs
    if ! grep "/var/lib/nfs/rpc_pipefs" /proc/mounts > /dev/null; then
      if [ ! -d /var/lib/nfs/rpc_pipefs ]; then
        mkdir -p /var/lib/nfs/rpc_pipefs
      fi
      if ! mount sunrpc -t rpc_pipefs /var/lib/nfs/rpc_pipefs/; then
        echo -n "Mounting /var/lib/nfs/rpc_pipefs failed."
        echo -e $RESULT_FAIL
        exit 0
      fi
    fi

    # It is important that exportfs be run before mountd so that
    # mountd is working from current information (in
    # /var/lib/nfs/etab)
    exportfs -a 

    if ! /usr/sbin/rpc.mountd $MOUNTDOPTS; then
      echo -n "Starting rpc.mountd failed."
      echo -e $RESULT_FAIL
      exit 0
    fi

    if ! /usr/sbin/rpc.rquotad; then
      echo -n "Starting rpc.rquotad failed."
      echo -e $RESULT_FAIL
      exit 0
    fi

    if ! /usr/sbin/rpc.nfsd $NUMSERVERS; then
      echo -n "Starting rpc.nfsd failed."
      echo -e $RESULT_FAIL
      exit 0
    fi
    if ! /usr/sbin/rpc.statd; then
      echo -n "Starting rpc.statd failed."
      echo -e $RESULT_FAIL
      exit 0
    fi
    exportfs -ra
  ;;

  stop)  echo "$1ping NFS services"
    /usr/sbin/exportfs -au

    pkill      "^rpc.rquotad$"
    pkill      "^rpc.mountd$"
    pkill  -1  "^nfsd$"
    pkill      "^rpc.statd$"

    # umount rpc_pipefs if mounted
    if grep "rpc_pipefs" /proc/mounts > /dev/null; then
      umount /var/lib/nfs/rpc_pipefs/
    fi
  ;;

  restart) echo "$1ing NFS services"
    $0 stop 
    $0 start
  ;;

  *)  echo "Usage: $0 {start|stop|restart}"
  ;;

esac
