#!/bin/sh
#
# portmap	Start/Stop RPC portmapper
#
# chkconfig:	345 11 89
#
# description:	The portmapper manages RPC connections, which are used by \
#		protocols such as NFS and NIS. The portmap server must be \
#		running on machines which act as servers for protocols which \
#		make use of the RPC mechanism.
# processname:	portmap


# Source function library
. /etc/rc.d/init.d/functions

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/portmap ] && . /etc/sysconfig/portmap

# Check that networking is up.
if is_yes "${NETWORKING}"; then
	if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
		msg_network_down portmap
		exit 1
	fi
else
	exit 0
fi

start() {
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/portmap ]; then
		PORTMAP_OPTIONS=
		[ -z "${PORTMAP_CHROOT}" ] || PORTMAP_OPTIONS="-t ${PORTMAP_CHROOT}"
		[ -z "${PORTMAP_IP}" ] || PORTMAP_OPTIONS="${PORTMAP_OPTIONS} -i ${PORTMAP_IP}"
		msg_starting portmap
		daemon portmap -u 22 -g 99 ${PORTMAP_OPTIONS}
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/portmap
	else
		msg_already_running portmap
	fi
}

stop() {
	if [ -f /var/lock/subsys/portmap ]; then
		msg_stopping portmap
		killproc portmap
		rm -f /var/lock/subsys/portmap
	else
		msg_not_running portmap
	fi
}

dump() {
  	pmap_dump >/var/lib/misc/portmap.dump
	RETVAL=$?
}

restore() {
	if [ -f /var/lib/misc/portmap.dump ] ; then
		pmap_set </var/lib/misc/portmap.dump
		RETVAL=$?
	fi
}

RETVAL=0
# See how we were called.
case "$1" in
  start)
  	start
	;;
  stop)
  	stop
	;;
  dump)
  	dump
	;;
  restore)
  	restore
	;;
  cleanup)
	stop
	start
	;;
  restart|force-reload)
	dump
	stop
	start
	restore
	;;
  status)
	status portmap
	exit $?
	;;
  *)
	msg_usage "$0 {start|stop|restart|force-reload|dump|restore|cleanup|status}"
	exit 3
esac

exit $RETVAL
