#!/bin/sh
#
# $Id: rpc,v 4.1 1993/03/23 16:17:11 mike Exp $
#
# Linux RPC start up.
# Starts the RPC daemons.
#

# Load the library and find various programs.
. /etc/init.d/LIB

TCPLIB=/etc

# Find out what service daemons are needed. If we don't have any
# listed in /etc/default/rpc we'll use a fairly minimal list of
# our own.
> /tmp/rpc.$$
if [ -r /etc/default/rpc ]; then
	awk '/^[ \t]*SERVICES/ { sub("^[ \t]*SERVICES", ""); print $0 }' /etc/default/rpc | while read i
	do
		echo $i >> /tmp/rpc.$$
	done
fi
PROCS="`cat /tmp/rpc.$$`"
rm -f /tmp/rpc.$$
if [ "$PROCS" = "" ]; then
	PROCS="portmap"
fi


case $1 in
	start)
		# We should have TCP/IP running before starting
		# the RPC services.

		started=
		for p in $PROCS
		do
			it=`pathof $p /etc:/etc/daemons:$TCPLIB`
			if [ -n "$it" -a "`pidof $p`" = "" ]; then
				if [ -z "$started" ]; then
					echo $use_escapes "Starting RPC services: \c"
					started=yes
				fi
				$it
				echo $use_escapes " $p\c"
			fi
		done
		if [ -n "$started" ]; then
			echo " ... done"
		fi
		;;

	stop)
		# We have to take NFS down before RPC is stopped.
		/etc/init.d/nfs stop

		stopped=
		for p in $PROCS
		do
			for i in `pidof $p`
			do
				kill $i
				if [ -z "$stopped" ]; then
					stopped=yes
				fi
			done
		done

		if [ -n "$stopped" ]; then
			echo "RPC stopped"
		fi
		;;

	*)
		echo "usage: rpc start|stop"
		exit 1
		;;
esac
