#!/bin/sh
# chkconfig: 345 48 52
# processname: /usr/sbin/automount
# config: /etc/auto.master
# description: Automounts filesystems on demand

# This is used in the ALT 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!)
#

#
# Location of the automount daemon and the init directory
#
LOCKFILE=/var/lock/subsys/autofs
SKIPFILE=/var/lock/subsys/autofs.skip
DAEMON=/usr/sbin/automount
CONFIGFILE=/etc/auto.master
TABFILE=/etc/auto.tab
RETVAL=0

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

# Get config.
SourceIfNotEmpty /etc/sysconfig/autofs &&
	[ "$ENABLE" != no ] &&
	[ -x "$DAEMON" ] ||
	exit 0

function skip()
{
	[ -s "$CONFIGFILE" ] && [ -s "$TABFILE" ] || return 0
	return 1
}

#
# Check for all maps that are to be loaded
#

function enable()
{
    echo
}

function disable()
{
    echo
}

function getrawmounts()
{
	egrep -vs '^(#|$)' "$CONFIGFILE"
}

	
#
# 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
	#
	local knownmaps=
	local dir map options maptype mountoptions
	getrawmounts | (
		while read dir map options; do
			# These checks screen out duplicates and skip over directories
			# where the map is '-'.
			if [ -n "$dir" -a -n "$map" \
				-a "`echo $map |cut -c1`" != '-' \
	                        -a -z "`echo $knownmaps |grep $dir/`" ]; then
				# Break up the maptype and map, if the map type is specified
		                maptype=`echo $map | cut -f1 -d:`
		                # Handle degenerate map specifiers
		                if [ "$maptype" = "$map" ] ; then
		                        if [ -x "$map" ]; then
		                                maptype=program
		                        elif [ -f "$map" ]; then
		                                maptype=file
		                        elif [ -f "/etc/$map" ]; then
		                                maptype=file
		                                map=`echo /etc/$map |sed 's^//^/^g'`
		                        elif [ "$map" = "hesiod" -o "$map" = "userhome" ] ; then
		                                maptype=$map
		                                map=
		                        else
		                                maptype=yp
		                                map="${map##*/}"
		                        fi
		                fi
		
		                # Extract timeout option, if any
		                if echo $options |fgrep -qs -- '-t'; then
		                    mountoptions="$(echo "$options" |sed 's/.*--*t\(imeout\)*[ \t=]*\([0-9][0-9]*\).*$/--timeout \2/')"
		                fi
		
		                # Collect all other options, converting -rwsize, et al to rwsize
		                options=`echo "$options" | sed -e '
		                  s/[ \t]*--*t\(imeout\)*[ \t=]*[0-9][0-9]*//g
		                  s/\(^\|[ \t]\)-/\1/g'`
		
		                map=`echo "$map" | cut -f2- -d:`
		                if [ "$maptype" = file ]; then
		                        if [ -f "/etc/$map" ]; then
		                                map=`echo /etc/$map | sed 's^//^/^g'`
		                        fi
		                fi
		
				echo "$DAEMON $DAEMONOPTIONS $mountoptions $dir $maptype $map $options $LOCALOPTIONS" | sed 's/  / /g'
			fi
			knownmaps=" $dir/ $knownmaps"
		done
	)
}

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

function start()
{
	if skip; then
		touch "$SKIPFILE"
		RETVAL=0
		return $RETVAL
	fi
		
	# Make sure the autofs filesystem type is available.
	if ! fgrep -qs autofs /proc/filesystems; then
		if ! action "Loading autofs support:" modprobe -k autofs4 2>/dev/null; then
			RETVAL=1
			return $RETVAL
		fi
	fi

	echo -n 'Starting automounter: '
	getmounts |sh &&
	{
		RETVAL=0
		success "automounter startup"
	} || {
		failure "automounter startup"
		RETVAL=1
	}
	echo
	rm -f "$SKIPFILE"
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
	return $RETVAL
}

function stop()
{
	echo -n "Stopping automounter: "
	killproc $DAEMON
	RETVAL=$?
	echo
	rm -f "$SKIPFILE"
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE"
	return $RETVAL
}

function restart()
{
	stop
	start
}

function reload()
{
	if [ ! -f $LOCKFILE ]; then
		echo "Automounter not running"
		RETVAL=1
		return $RETVAL
	fi
	echo "Checking for changes to $CONFIGFILE ...."
        TMP1=`mktemp /tmp/autofs.XXXXXX` || { echo "could not make temp file" >& 2; exit 1; }
        TMP2=`mktemp /tmp/autofs.XXXXXX` || { echo "could not make temp file" >& 2; exit 1; }
	getmounts >$TMP1
	ps --no-headers axwww |grep "[0-9]:[0-9][0-9] $DAEMON " | (
	    while read pid tt stat time command; do
		echo "$command" >>$TMP2
		if ! grep -qs "^$command" $TMP2; then
			while kill -USR2 $pid; do
			    sleep 3
			done
			echo "Stop $command"
		fi
	    done
	)
	( while read x; do
		if ! grep -q "^$x" $TMP2; then
			$x
			echo "Start $x"
		fi
        done ) <$TMP1
	rm -f $TMP1 $TMP2
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status
		;;
	reload)
		reload
		;;
	restart)
		restart
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	skipstart)
		if [ -e "$SKIPFILE" -a ! -e "$LOCKFILE" ]; then
			start
		fi
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|status|reload|restart|condstop|condrestart}"
		RETVAL=1
esac

exit $RETVAL
