#!/bin/sh
#
# rc file for automount using a Sun-style "master map".
#
# On most distributions, this file should be called:
# /etc/rc.d/init.d/autofs or /etc/init.d/autofs or /etc/rc.d/rc.autofs
#
#
### BEGIN INIT INFO
# Provides: autofs
# Required-Start: $network ypbind
# Required-Stop: $network ypbind
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Automounts filesystems on demand
# Description: Automounts filesystems on demand
### END INIT INFO
#
# Location of the automount daemon and the init directory
#
DAEMON=/usr/sbin/automount
prog=`basename $DAEMON`
MODULE="autofs4"
DEVICE="autofs"
confdir=/etc/default

test -e $DAEMON || exit 0

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

#
# load customized configuation settings
#
if [ -r $confdir/autofs ]; then
	. $confdir/autofs
fi

start() {
	echo -n "Starting $prog: "

	# Make sure autofs4 module is loaded
	if ! grep -q autofs /proc/filesystems
	then
		# Try load the autofs4 module fail if we can't
		modprobe $MODULE >/dev/null 2>&1
		if [ $? -eq 1 ]
		then
			echo "Error: failed to load autofs4 module."
			return 1
		fi
	elif ([ -f /proc/modules ] && lsmod) | grep -q autofs[^4]
	then
		# wrong autofs filesystem module loaded
		echo
		echo "Error: autofs kernel module is loaded, autofs4 required"
		return 1
	fi

	# Check misc device
	if [ -n "$USE_MISC_DEVICE" -a "x$USE_MISC_DEVICE" = "xyes" ]; then
		sleep 1
		if [ -e "/proc/misc" ]; then
			MINOR=`awk "/$DEVICE/ {print \\$1}" /proc/misc`
			if [ -n "$MINOR" -a ! -c "/dev/$DEVICE" ]; then
				mknod -m 0600 /dev/$DEVICE c 10 $MINOR
			fi
		fi
		if [ -x /sbin/restorecon -a -c /dev/$DEVICE ]; then
			/sbin/restorecon /dev/$DEVICE
		fi
	else
		if [ -c /dev/$DEVICE ]; then
			rm /dev/$DEVICE
		fi
	fi

	$prog $OPTIONS 
	RETVAL=$?
	if [ $RETVAL -eq 0 ] ; then
		echo "done."
	else
		echo "failed."
	fi
	return $RETVAL
}

stop() {
	echo -n $"Stopping $prog: "
	count=0
	while [ -n "`pidof $prog`" -a $count -lt 60 ] ; do
		killall -TERM $prog >& /dev/null
		RETVAL=$?
		[ $RETVAL = 0 -a -z "`pidof $prog`" ] || sleep 1
		count=`expr $count + 1`
	done
	if [ -z "`pidof $prog`" ] ; then
		echo "done."
	else
		echo "failed."
	fi
	return $RETVAL
}

restart() {
	stop
	while [ -n "`pidof $prog`" ] ; do
		sleep 5
	done
	start
}

reload() {
	pid=`pidof $prog`
	if [ -z $pid ]; then
		echo $"$prog not running"
		RETVAL=1
	else
		kill -HUP $pid 2> /dev/null
		echo $"Reloading maps"
		RETVAL=0
	fi
	return $RETVAL
}

RETVAL=0

case "$1" in
	start)
		start
		;;
	forcestart)
		OPTIONS="$OPTIONS --force"
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	forcerestart)
		OPTIONS="$OPTIONS --force"
		restart
		;;
	reload)
		reload
		;;
	*)
		echo $"Usage: $0 {start|forcestart|stop|restart|forcerestart|reload}"
		exit 1;
		;;
esac

exit $?

