:
#      @(#)nfs	4.7.1.4 System V NFS  source
#      SCCS IDENTIFICATION
#
#	SCO Modifications: @(#)nfs	1.5 91/12/17 
#
#	Copyright (C) The Santa Cruz Operation, 1991.
#	This Module contains Proprietary Information of
#	The Santa Cruz Operation, and Lachman Associates Inc. 
#	and should be treated as Confidential.
#
#	Copyright 1987, 1988, 1989 Lachman Associates, Incorporated (LAI)
#
#	All Rights Reserved.
#
#	The copyright above and this notice must be preserved in all
#	copies of this source code.  The copyright above does not
#	evidence any actual or intended publication of this source
#	code.
#
#	This is unpublished proprietary trade secret source code of
#	Lachman Associates.  This source code may not be copied,
#	disclosed, distributed, demonstrated or licensed except as
#	expressly authorized by Lachman Associates.
#
#	System V STREAMS TCP was jointly developed by Lachman
#	Associates and Convergent Technologies.
#
: ${OK=0} ${FAIL=1} ${RELINK=2}
PATH=/bin:/usr/bin:/etc:/etc/conf/bin

LOWER_PRD=$1
LOWER_IF=$2
UPPER_PRD=$3
UPPER_IF=$4
CONFIGURE=$5

#
# Miscellaneous variable seclarations
#
tmp=/tmp/nfsrt$$
do_relink="N"			# Assume no re-link is required

#
# Initialise configuration variables
#
N_NFSCONS=256			# No of NFS conections configured by default

#
# Parameters used by the functions in tcp_nfs.comm.
#
NFSDDRVR="nfsd"
PRODUCT="NFS"
PERMSLIST=/etc/perms/nfsrt
LKPERM=/etc/perms/inst
DRIVERDIR=/usr/lib/nfsrt/ID
DRIVERS="nfs lockd nfsd"
KPARAMS="NINODE		300 \
	 NS5INODE	300 \
	 NFILE		300 \
	 NBLK512	+16 \
	 NBLK256	+64 \
	 NBLK128	+128 \
	 NUMTIM		+16 \
	 NUMTRW		+16"

#
# If the common functions file doesn't exist, we won't get
# very far.
#
COMMONFILE=/usr/lib/netconfig/bin/tcp_nfs.comm

if [ ! -r $COMMONFILE ]
then
    echo "\nError: Missing or unreadable $COMMONFILE." >&2
    exit ${FAIL}
fi

. $COMMONFILE

##############################################################################
#
# Add drivers via idinstall.  This function replaces the one in the
# common file, because we have some special processing to do for
# the nfs driver.
#
add_drivers() {
    message "Installing $PRODUCT drivers into the link kit ... \c"

    for drv in $DRIVERS
    do
	    cd ${DRIVERDIR}/$drv

	    #
	    # The nfs ``driver'' needs a different set of
	    # flags.
	    #
	    case $drv in
		nfs)
		    installflags="-e -k -o -p -c -l"

		    #
		    # Blasted idinstall won't make this directory
		    # if you give components explicitly on the command
		    # line.
		    #
		    if [ ! -d /etc/conf/pack.d/nfs ]
		    then
			mkdir /etc/conf/pack.d/nfs
		    fi
		    ;;

		*)
		    installflags="-e -k"
		    ;;
	    esac

	    /etc/conf/bin/idcheck -p $drv
	    if [ $? -lt 16 ]
	    then
		    /etc/conf/bin/idinstall -a $installflags $drv
	    else
		    /etc/conf/bin/idinstall -u $installflags $drv
	    fi

	    #
	    # Ensure driver is switched on.
	    #
	    if [ -f /etc/conf/sdevice.d/$drv ]
	    then
		sed "/	N/s/N/Y/" /etc/conf/sdevice.d/$drv >${tmp}sdev || {
			Error "Unable to edit /etc/conf/sdevice.d/${drv}."
			cleanup2 $FAIL
		}

		mv ${tmp}sdev /etc/conf/sdevice.d/$drv
	    fi
    done

    message "done.\n"
}

#####################################################################
# If extra entries are necessary to accommodate devices with
# extended minor device number then this functions creates them
# by calling sconfigxtr_mdev() for the three different devices required.
# Called as:	sconfigxtr <No of devices asked to be created>
#
sconfigxtr () {
	sconfigxtr_mdev $1 1
	do_relink="Y"
}

#####################################################################
# If extra entries are necessary to accommodate devices with
# extended minor device number then this functions creates them.
# This functions is doing this job by being called separately for 
# the master and slave ttys and mice drivers.
# Called as:	sconfigxtr <No of devices asked to be created> <1,2,3, or 4>
#
sconfigxtr_mdev () {
	case "$2" in
		1) dev_name=$NFSDDRVR
		   dev_name2=$NFSDDRVR
		   dev_flg2="oci"
		   dev_flg3="icbM"
		   dev_maj=`/etc/conf/cf.d/configure -j $NFSDDRVR`
		   sdev_line="nfsd\tY\t256\t0\t0\t0\t0\t0\t0\t0"
	  	   sdev_file=/etc/conf/sdevice.d/$NFSDDRVR	
				;;
	esac

	last_mdev $dev_name

	entr_exs=$cntd_entr
		# mdevice entries existing so far
	dev_num=`expr 256 \* $entr_exs`
		# number of devices configured
	entr_req=`expr \( $dev_num + $1 \) / 256`
		# mdevice entries to exist after the creation of the extra nodes
	entr_xtr=`expr $entr_req - $entr_exs`
		# mdevice entries to be added in mdevice after 
		# the creation of the extra nodes

	if [ "$entr_xtr" -gt 0 ]
	 then
		i=0
		while [ "$i" -lt "$entr_xtr" ]
		do
			next_maj=`/etc/conf/cf.d/configure -j NEXTMAJOR`
			off_set=`expr \( $entr_exs + $i \) \* 256`
			entry="$dev_name\t$dev_flg2\t$dev_flg3\t$dev_name2\t$next_maj\t$next_maj\t$dev_maj\t$off_set\t-1"
			echo $entry >> /etc/conf/cf.d/mdevice
			echo $sdev_line >> $sdev_file
			i=`expr $i + 1`
		done
	 fi
}

#####################################################################
# It returns the number of entries in the mdevice file with first field
# equal to the string passed as first argument.
#
last_mdev () {
	cntd_entr=0

	cntd_entr=`awk -e 'BEGIN { entries = 0 }
			   \$1 == "'"$1"'" { entries ++ }
			   END { print entries }' /etc/conf/cf.d/mdevice`
	 return 
}

#####################################################################
# It removes the mdevice entries corresponding to extended minor device
# numbered devices. It does not touch the entry with the base major number.
# Called as:	rmvdrvr_mul	<driver name> <base major no>
#
rmvdrvr_mul () {
	tmp_majno=`grep "^$1" mdevice | tail -1 | awk '{ print $6 }'`
	./configure -d -b -c -Z -m $tmp_majno >> conflog
}

#####################################################################
# If extra entries are are to be removed from devices with
# extended minor device number then this functions removes them
# by calling sconfigxtr_mdev() for the three different devices required.
# Called as:	sunconfigxtr <No of devices asked to be removed>
#
sunconfigxtr () {
	sunconfigxtr_mdev $1 1
	do_relink="Y"
}

#####################################################################
# If extra entries are are to be removed from devices with
# extended minor device number then this functions removes them
# This functions is doing this job by being called separately for 
# the nfsd driver.
# Called as: sunconfigxtr_mdev <No of devices asked to be created> <1,2,3, or 4>
#
sunconfigxtr_mdev () {
	case "$2" in
		1) dev_name=$NFSDDRVR
		   dev_maj=`/etc/conf/cf.d/configure -j $NFSDDRVR`
	  	   sdev_file=/etc/conf/sdevice.d/$NFSDDRVR	
				;;
	esac

	last_mdev $dev_name

	entr_exs=$cntd_entr
		# mdevice entries existing so far
	dev_num=`expr 256 \* $entr_exs`
		# number of devices configured
	entr_xtr=`expr $1 / 256`
		# mdevice entries to remove 

	if [ "$entr_xtr" -gt 0 ]
	 then
		i=0
		while [ "$i" -lt "$entr_xtr" ]
		do
			cat $sdev_file | sed -e "$ d" > /tmp/temp.$dev_name
			safemv /tmp/temp.$dev_name $sdev_file
			rmvdrvr_mul $dev_name $dev_maj
			i=`expr $i + 1`
		done
		addno=`expr \( $entr_exs - $entr_xtr - 1 \) \* 256`
	 fi
}

##############################################################################
#
# Update the number of NFS connections configured in the kernel
#
chknfsconns() {
	last_mdev $NFSDDRVR
	entr_exs=$cntd_entr
		# mdevice entries existing so far
	dev_num=`expr 256 \* $entr_exs`
		# number of devices configured
	addstr="How many additional NFS connections do you want to be created\n"
	rmvstr="How many NFS Connections do you want to be removed"

	while mesg="\n$dev_num NFS connections currently configured, do you want to:\n
		1. Add NFS Connections 
		2. Remove NFS Connections

Select an option"
		quit="quit"
	do prompt "$mesg" q || break
	select=$cmd
	case $select in
		1) accpt_nl=yes
		   addno=`getnumber "$addstr" "256"`
		   accpt_nl=no
		   __PWD=`pwd`
		   cd /etc/conf/cf.d
		   [ "$addno" -gt 0 ] && {
				temp=`expr $addno / 256`
				if [ `expr $temp \* 256` -ne $addno ]; then
					echo "\n\tNFS connections must be configured in multiples of 256"
					sleep 1
					cd $__PWD
					continue
				else
					echo "Adding $addno NFS connections ... \c"
					sconfigxtr $addno
					echo "done."
				fi
		    }
  		    cd $__PWD
		    # break out of while loop so we are not prompted again
		    # for addition or removal of NFS connections
		    break
		    ;;
		2) accpt_nl=yes
		   rmvno=`getnumber "$rmvstr" "256"`
		   accpt_nl=no
		   if [ $rmvno -ge $dev_num ] ; then
					echo "\n\tIf you want 0 NFS connections then remove SCO NFS via custom"
					sleep 1
					cd $__PWD
					continue
		   fi
		   __PWD=`pwd`
		   cd /etc/conf/cf.d
		   [ "$rmvno" -gt 0 ] && {
				temp=`expr $rmvno / 256`
				if [ `expr $temp \* 256` -ne $rmvno ]; then
					echo "\n\tNFS connections must be configured in multiples of 256"
					sleep 1
					cd $__PWD
					continue
				else
					echo "removing $rmvno NFS connections... \c"
					sunconfigxtr $rmvno
					echo "done."
				fi
		    }
  		    cd $__PWD
		    # break out of while loop so we are not prompted again
		    # for addition or removal of NFS connections
		    break
		    ;;
		Q|q)
		    # break out of while loop so we are not prompted again
		    # for addition or removal of NFS connections
		    break
		    ;;

		*) echo "\n	Please select either 1 or 2"
		   continue	;;
	esac
	done
}

##############################################################################
# Set the number of sockets configured in the kernel to N_NFSCONS
#
setnfsconns() {
	last_mdev $NFSDDRVR
	entr_exs=$cntd_entr
		# mdevice entries existing so far
	dev_num=`expr 256 \* $entr_exs`
		# number of devices configured
	extra_nfscons=`expr $N_NFSCONS - $dev_num`
		# number of scokets to be added/removed

	case $extra_nfscons in
		0) return $OK
		   break
		   ;;

		[1-9]*) __PWD=`pwd`
		   cd /etc/conf/cf.d
			temp=`expr $extra_nfscons / 256`
			if [ `expr $temp \* 256` -ne $extra_nfscons ]; then
				cd $__PWD
				return $FAIL
			else
				message "Adding $extra_nfscons NFS connections ... \c"
				sconfigxtr $extra_nfscons
				message "done."
			fi
  		    cd $__PWD
		    break
		    ;;

		-[1-9]*) __PWD=`pwd`
		   cd /etc/conf/cf.d
		   extra_nfscons=`expr 0 - $extra_nfscons`
		   [ $extra_nfscons -eq $dev_num ] && {
			cd $__PWD
			return $FAIL
		   }
			temp=`expr $extra_nfscons / 256`
			if [ `expr $temp \* 256` -ne $extra_nfscons ]; then
				cd $__PWD
				return $FAIL
			else
				message "Removing $extra_nfscons NFS connections ... \c"
				sunconfigxtr $extra_nfscons
				message "done."
			fi
  		    cd $__PWD
		    break
		    ;;
	esac
}

##############################################################################
# main() 
#
trap "cleanup $FAIL" 1 2 3 15

_upgrade=
_reconfig=
_no_prompt=

[ -f /tmp/system_upgrade ] && _upgrade=TRUE
[ -f /tmp/sco_nfs_reconf ] && _reconfig=TRUE
[ -f "$IQM_FILE" -a ! -f /tmp/system_upgrade ] && _no_prompt=TRUE

# Check that no more that one mode flag is set
n_of_args=`echo "$_upgrade $_no_prompt $_reconfig" | wc -w`
if [ $n_of_args -gt 1 ] ; then
	message "Netconfig nfs init script is confused. Multiple modes."
	cleanup $FAIL
fi

[ "$_reconfig" ] || message "\nInstalling $PRODUCT over $LOWER_PRD.\n"

# Netconfig sets CONFIGURE to "Y" if this is the first time NFS is installed.
# It is set to no for the reconfigure case
if [ "${CONFIGURE}" = "Y" ] ; then

    # Check to see that the link kit is installed, 
    # since drivers cannot be installed without it
    cd /
    chklinkkit

    # Configure kernel parameters
    set_kernel_params

    # Add drivers via idinstall.
    add_drivers

    #
    # WARNING -- WARNING -- KLUDGE ALERT -- DANGER -- RUN FOR YOUR LIVES
    #
    # The following hack forces the NFS entry in the fstypsw table
    # in the kernel to be the last one rather than the first.
    # This avoids a particular bug that occurs in the 3.2.0f kernel
    # on startup when the /etc/fsstat command is invoked from /etc/bcheckrc
    # to figure out the file system type of /dev/root.
    # To borrow a phrase, "you are not expected to understand this."
    #
    # Update 12/17/91 -- This still seems to be necessary in 3.2.4l.
    #
    cd /etc/conf/mfsys.d
    mv nfs znfs
    cd /etc/conf/sfsys.d
    mv nfs znfs
    #
    # END KLUDGE.  And now, back to our regular programming ...
    #

    do_relink="Y"

    #
    # Make sure the startup and shutdown scripts are
    # there.
    #
    if [ ! -f /etc/rc0.d/K79nfs ]
    then
	ln -f /etc/nfs /etc/rc0.d/K79nfs
    fi

    if [ ! -f /etc/rc2.d/S89nfs ]
    then
	ln -f /etc/nfs /etc/rc2.d/S89nfs
    fi
fi	# End of if $CONFIGURE = "Y"

# check to see how many NFS connections are configured.
# prompt user to see if they want to reconfigure them
# Interactive version chkptty, else set to default of N_NFSCONS with
# setnfscons.
if [ "$_upgrade"  -o "$_no_prompt" ] ; then
	setnfsconns
else
	chknfsconns
fi

[ "$_reconfig" ] || message "\n$PRODUCT Installation Complete."

#
# Ensure kernel is relinked by netconfig only if necessary.
#
if [ "$do_relink" = "Y" ]
then
    cleanup $RELINK
else
    cleanup $OK
fi
