#! /bin/bash
#
#       $Id: rc.autofs,v 3.0 1999/04/17 08:00 patrol Exp $
#
#	rc file for automount using a Sun-style "master map".
#	We first look for a local /etc/autofs/auto.master, then a YP
#	map with that name
#
#	On most distributions, this file should be called:
#	/etc/rc.d/init.d/autofs or /etc/init.d/autofs
#

#       This is used in the Debian distribution to determine the proper
#       location for the S- and K-links to this init file.
#	The following value is extracted by debstd to figure out how to
#	generate the postinst script. Edit the field to change the way the
#	script is registered through update-rc.d (see the manpage for
#	update-rc.d!)
#
FLAGS="defaults 21"

test -f /usr/sbin/automount || exit 0
PATH=/sbin:/usr/sbin:/bin:/usr/bin
export PATH
export MASTER=/etc/autofs/auto.master
export LOCK=/var/lock/automount

#
# 	We can add local options here
#	e.g. localoptions='rsize=8192,wsize=8192'
#
localoptions=''

#
#	The automount options may be added here
#	e.g. amoptions='-t 15'
amoptions='-t 15'

#
#	This function will build a list of automount commands to execute in
#	order #	to activate all the mount points. It is used to figure out
#	the difference of automount points in case of a reload
#
function getmounts()
{
#
#	Check for local maps to be loaded
#
if [ -f $MASTER ]
then
    cat $MASTER | sed -e '/^#/d' -e '/^$/d'| (
	while read dir map options
	do
	    if [ ! -z "$dir" -a ! -z "$map" \
			-a x`echo "$map" | cut -c1` != 'x-' ]
	    then
		map=`echo "/etc/$map" | sed -e 's:^/etc//:/:'`
		options=`echo "$options" | sed -e 's/\(^\|[ \t]\)-/\1/g'`
		if [ -x $map ]; then
		    echo "automount $amoptions $dir program $map $options $localoptions"
		elif [ -f $map ]; then
		    echo "automount $amoptions $dir file $map $options $localoptions"
		else
		    echo "automount $amoptions $dir `basename $map` $options $localoptions"
		fi
	    fi
	done
    )
fi

#
#	Check for YellowPage maps to be loaded
#
if [ -e /usr/bin/ypcat ] && [ `ypcat -k auto.master 2>/dev/null | wc -l` -gt 0 ]
then
    ypcat -k auto.master | (
	while read dir map options
	do
	    if [ ! -z "$dir" -a ! -z "$map" \
			-a x`echo "$map" | cut -c1` != 'x-' ]
	    then
		map=`echo "$map" | sed -e 's/^auto_/auto./'`
		options=`echo "$options" | sed -e 's/\(^\|[ \t]\)-/\1/g'`
		echo "automount $amoptions $dir yp $map $options $localoptions"
	    fi
	done
    )
fi
}

#
#	Status lister.
#
function status()
{
	echo "Configured Mount Points:"
	echo "------------------------"
	getmounts
	echo ""
	echo "Active Mount Points:"
	echo "--------------------"
	ps ax|grep "[0-9]:[0-9][0-9] automount " | (
		while read pid tt stat time command; do echo $command; done
	)
}


#
#	Sinux start/stop function.
#
function sinux()
{

#
#	See how we were called.
#
case "$1" in
  start)
	# Check if the automounter is already running?
	if [ ! -f $LOCK ]; then
	    echo 'Starting automounter: '
	    modprobe autofs4
	    getmounts | sh
	    touch $LOCK
	fi
	;;
  stop)
	killall -TERM automount
	rm -f $LOCK
	;;
  reload|restart)
	if [ ! -f $LOCK ]; then
		echo "Automounter not running"
		exit 1
	fi
	echo "Checking for changes to $MASTER ...."
	TMP1=/var/run/automount.tmp1;
	TMP2=/var/run/automount.tmp2;
	getmounts >$TMP1
	ps ax|grep "[0-9]:[0-9][0-9] automount " | (
	    while read pid tt stat time command; do
		echo "$command" >>$TMP2
		if ! grep -q "^$command" $TMP2; then
			kill -USR2 $pid
			echo "Stop $command"
		fi
	    done
	)
	cat $TMP1 | ( while read x; do
		if ! grep -q "^$x" $TMP2; then
			$x
			echo "Start $x"
		fi
        done )
	rm $TMP1 $TMP2
	;;
  status)
	status
	;;
  *)
	echo "Usage: $0 {start|stop|restart|reload|status}"
	exit 1
esac
}

sinux "$@"

exit 0
