#!/sbin/runscript
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-x86/net-fs/autofs/files/autofs.rc10,v 1.1 2005/03/15 04:18:38 robbat2 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

DAEMON=/usr/sbin/automount

depend() {
	need localmount	
	use ypbind nfs slapd portmap net
}

opts="start stop status stats reload restart"

#
# Check for all maps that are to be loaded
#
function getschemes()
{
	awk '/^automount:/{ $1="" ; gsub("\\[.*\\]","") ; print $0 }' /etc/nsswitch.conf
}

function getfilemounts()
{
    if [ -f /etc/autofs/auto.master ] ; then
		sed -e '/^#/d' -e '/^$/d' < /etc/autofs/auto.master
    fi
}

function getnismounts()
{
    /usr/bin/ypcat -k ${nis_mapname} 2> /dev/null | sed -e '/^#/d' -e '/^$/d'
}

function getldapmounts()
{
    if [ -x /usr/lib/autofs/autofs-ldap-auto-master ]; then
        /usr/lib/autofs/autofs-ldap-auto-master \
        -m ${ldap_map_oc} -e ${ldap_entry_oc} \
		-n ${ldap_map_key} -k ${ldap_entry_key} \
		-v ${ldap_value} 2> /dev/null
    fi
}

function getrawmounts()
{
    for scheme in `getschemes` ; do
        case "$scheme" in
            files)
                getfilemounts
                ;;
            nis*)
                getnismounts
                ;;
            ldap*)
                getldapmounts
                ;;
        esac
    done
}


#
# 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()
{
	knownmaps=" "
	getrawmounts | (
	while read dir map options
	do
	    # These checks screen out duplicates and skip over directories
	    # where the map is '-'.
	    if [ ! -z "$dir" -a ! -z "$map" \
			-a x`echo "$map" | cut -c1` != 'x-' \
			-a "`echo "$knownmaps" | grep -w $dir/`" = "" ]
	    then
		# If the options include a -t or --timeout parameter, then
		# pull those particular options out.
		# echo DAEMONOPTIONS OPTIONS $daemonoptions $options
		allopts="$daemonoptions $options"
		startupoptions=

		# there is no way to get all of these out in a single sed call that I'm aware of :-(
		opt_timeout="$(echo ${allopts} | sed -n -e '/-t/s/.*--*t\(imeout\)*[[:space:]=]*\([0-9][0-9]*\).*$/--timeout \2/gp')"
		opt_pidfile="$(echo ${allopts} | sed -n -e '/-p/s/.*--*p\(id-file\)*[[:space:]=]*\([^[:space:]]*\).*$/--pid-file \2/gp')"
		opt_verbose="$(echo ${allopts} | sed -n -e '/-v/s/.*--*v\(erbose\)*.*$/--verbose/gp')"
		opt_debug="$(echo ${allopts} | sed -n -e '/-d/s/.*--*d\(ebug\)*.*$/--debug/gp')"
		opt_ghost="$(echo ${allopts} | sed -n -e '/-g/s/.*--*g\(host\)*.*$/--ghost/gp')"

		startupoptions="${opt_timeout} ${opt_pidfile} ${opt_verbose} ${opt_debug} ${opt_ghost}"
		

		# Other option flags are intended for maps.
		mapoptions="$(echo ${allopts} | sed  \
			-e '/-t/{s/--*t\(imeout\)*[[:space:]=]*\([0-9][0-9]*\)//g }' \
			-e '/-p/{s/--*p\(id-file\)*[[:space:]=]*\([^[:space:]]*\)//g }' \
			-e '/-v/{s/--*v\(erbose\)*//g }' \
			-e '/-d/{s/--*d\(ebug\)*//g }' \
			-e '/-g/{s/--*g\(host\)*//g }' \
			-e 's/^[[:space:]]*//g' \
			-e 's/[[:space:]]*$//g')"

		# Break up the maptype and map, if the map type is specified
		maptype="${map/:*}"
		# Handle degenerate map specifiers
		if [ "$maptype" = "$map" ] ; then
		    if [ -x "$map" ]; then
				maptype="program"
		    elif [ -x "/etc/$map" ]; then
				maptype="program"
				map=`echo /etc/$map | sed 's^//^/^g'`
		    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=
		    elif [ "$map" = "multi" ] ; then
				maptype="$map"
				map=
		    else
				maptype=yp
				map=`basename $map | sed -e s/^auto_home/auto.home/ -e s/^auto_mnt/auto.mnt/`
			fi
		fi
		map="${map#*:}"

		#echo STARTUPOPTIONS $startupoptions > /tmp/debug
		#echo DIR $dir >> /tmp/debug
		#echo MAPTYPE $maptype >> /tmp/debug
		#echo MAP $map >> /tmp/debug
		#echo MAPOPTIONS $mapoptions >> /tmp/debug
		#echo LOCALOPTIONS $localoptions >> /tmp/debug

		echo "$DAEMON $startupoptions $dir $maptype $map $mapoptions $localoptions" | sed -e 's/	/ /g' -e 's/  / /g'

		# echo ------------------------
	    fi
	    knownmaps=" $dir/ $knownmaps"
	done
    )
}


start() {
	ebegin "Starting automounter"
	# Check that maps exist
	if [ -z "$(getmounts)" ]; then
		eend 1 "No automount maps defined"
		exit 1
	fi
	# ensure autofs support is loaded
	grep -q autofs /proc/filesystems || modprobe -q autofs4 || modprobe -q autofs
	if [ $? -ne 0 ]; then
		eend 1 "No autofs support available"
		exit 1
	fi
	# Now go
	getmounts | while read cmd timeout time mnt rest
	do
		#FIXME: this works but it really sucks
		if echo $timeout|grep -v -- '--timeout' >/dev/null ; then
			rest="$time $mnt $rest"
			mnt="$timeout"
			time=""
			timeout=""
		fi

		echo -n " $mnt"
		pidfile=/var/run/autofs`echo $mnt | sed 's/\//./g'`.pid
		start-stop-daemon --start --pidfile $pidfile --quiet \
			--exec /usr/sbin/automount -- $timeout $time $mnt $rest
		#
		#	Automount needs a '--pidfile' or '-p' option.
		#	For now we look for the pid ourself.
		#
		ps ax | grep "[0-9]:[0-9][0-9] /usr/sbin/automount $timeout $time \?$mnt" | (
			read pid rest
			echo $pid > $pidfile
			echo "$mnt $rest" >> $pidfile
		)
	done
	echo
	eend $?
}

stop() {
	ebegin "Stopping automounter"
	start-stop-daemon --stop --quiet --signal 12 --exec /usr/sbin/automount
	eend $?
}

stats() {
	echo "Configured Mount Points:"
	echo "------------------------"
	getmounts
	echo ""
	echo "Active Mount Points:"
	echo "--------------------"
	ps ax|grep "[0-9]:[0-9][0-9] /usr/sbin/automount " | (
		while read pid tt stat time command; do echo $command; done
	)
}

reload() {
	echo "Reloading automounter: checking for changes ... "
	TMP=/var/run/autofs.tmp
	getmounts >$TMP
	for i in /var/run/autofs.*.pid
	do
		pid=`head -n 1 $i 2>/dev/null`
		[ "$pid" = "" ] && continue
		command=`tail -n +2 $i`
		if ! grep -q "^$command" $TMP
		then
			echo "Stopping automounter: $command"
			kill -USR2 $pid
		fi
	done
	rm -f $TMP
	svc_start
}

restart() {
	svc_stop
	svc_start
}