#!/bin/sh
# nfs   This shell script starts and stops the nfs services in a distribution
#       independent fashion.
#
# description:  starts and stops nfs server services
# chkconfig: 2345 60 20
#
# Copyright (c) 2000-2001 Mission Critical Linux, Inc.
#

PATH=/sbin:/bin:/usr/sbin:/usr/bin
export PATH

# Who am I?
SCRIPT_NAME=`basename $0`

# Grab our daemon functions.
. `dirname $0`/nfs-functions

# Kernel daemons and options
PREFIX="rpc."		# Prefix for kernel execs (usually "rpc.")
NFSD="nfsd"		# Kernel NFS Server
RPCNFSDCOUNT="8"	# Number of nfsd threads

# User daemons and options
RQUOTAD="rpc.rquotad"	# Remote quota server
MOUNTD="rpc.mountd"	# Mount server
RPCMOUNTDOPTS=""	# options for rpc.mountd
EXPORTFS="exportfs"	# Exportfs command

SCRIPT_NAME=`basename $0`
DESC="NFS kernel daemon"

# We use "type -path" instead of "which" since it's internal to bash.
[ -x "`type -path $PREFIX$NFSD`" ] || exit 0
[ -x "`type -path $MOUNTD`" ] || exit 0

# Also make sure we have our exportfs command.
[ -x "`type -path $EXPORTFS`" ] || exit 0
[ -s /etc/exports ] || exit 0

# rquotad is not required for NFS to work, however.
# Unset if it is not present.
[ -x "`type -path $RQUOTAD`" ] || unset RQUOTAD

# Handle how we were called.
case "$1" in
start)
    echo -n "Exporting directories for $DESC..."
    $EXPORTFS -r
    echo "done."

    echo -n "Starting $NFSD: "
    startdaemon $PREFIX$NFSD $RPCNFSDCOUNT

    # Disable NFSv3 on mountd if we don't have NFSv3
    ClearAddr=
    if [ -f /proc/net/rpc/auth.unix.ip/channel ] ; then
      if   grep -s 127.0.0.1 /proc/net/rpc/auth.unix.ip/content > /dev/null ; then
	     : address already known
      else
             echo nfsd 127.0.0.1 2147483647 localhost > /proc/net/rpc/auth.unix.ip/channel
	     ClearAddr=
      fi
    fi
    rpcinfo -u localhost nfs 3 &>/dev/null 
    if [ "$?" != "0" ]
    then
        RPCMOUNTDOPTS="$RPCMOUNTDOPTS --no-nfs-version 3"
    fi
    if [ -n "$ClearAddr" ]; then
          echo nfsd 127.0.0.1 1  > /proc/net/rpc/auth.unix.ip/channel
    fi

    echo -n "Starting $MOUNTD: "
    startdaemon $MOUNTD $RPCMOUNTDOPTS

    # Start rquotad if it is set
    if [ -n "$RQUOTAD" ]
    then
        echo -n "Starting $RQUOTAD: "
        startdaemon $RQUOTAD
    fi

    # if this lock file doesn't exist, init won't even try to run
    # the shutdown script for this service on RedHat systems!
    # on non-RedHat systems, /var/lock/subsys may not exist.
    touch /var/lock/subsys/$SCRIPT_NAME &> /dev/null
    ;;

stop)
    for process in $RQUOTAD $MOUNTD $NFSD
    do
        echo -n "Stopping $process: "
        stopdaemon $process
    done

    echo "Unexporting directories for $DESC..."
    $EXPORTFS -au
    rm -f /var/lock/subsys/$SCRIPT_NAME 
    echo "done."
    ;;

restart)
    $0 stop
    $0 start
    ;;

reload)
    # Update exports
    echo "Re-exporting directories for $DESC..."
    $EXPORTFS -r
    touch /var/lock/subsys/$SCRIPT_NAME &> /dev/null
    echo "done."
    ;;

status)
    # First, check status of userland daemons
    for process in $RQUOTAD $MOUNTD $NFSD
    do
	daemonstatus $process
    done
    exit 0
    ;;

*)
    echo "Usage: $0 {start|stop|status|restart|reload}"
    exit 1
esac
