#!/bin/sh
#
# $Id: rc.autofs.in,v 1.4 2000/01/22 22:17:34 hpa Exp $
#
# rc file for automount using a Sun-style "master map".
# We first look for a local /etc/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
#

# For Redhat-ish systems
#
# chkconfig: 345 15 85
# description: Automounts filesystems on demand

# 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"

. /etc/thinstation.global

#
# Location of the automount daemon and the init directory
#
DAEMON=/bin/automount
initdir=

#
# Determine which kind of configuration we're using
#
system=redhat

test -e $DAEMON || exit 0
thisscript="$0"
if [ ! -f "$thisscript" ]; then
    echo "$0: Cannot find myself" 1>&2
    exit 1
fi

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

#
# 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
#
getmounts()
{
#
# Check for local maps to be loaded
#
if [ -f /etc/auto.master ]
then
    cat /etc/auto.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 "$DAEMON $dir program $map $options $localoptions"
		elif [ -f $map ]; then
		    echo "$DAEMON $dir file $map $options $localoptions"
		else
		    echo "$DAEMON $dir `basename $map` $options $localoptions"
		fi
	    fi
	done
    )
fi
}

#
# Status lister.
#
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
	)
}


#
# Redhat start/stop function.
#
redhat()
{

#
# See how we were called.
#
case "$1" in
  init)
	# Check if the automounter is already running?
	if [ ! -f /var/lock/autofs ]; then
	    echo 'Starting automounter: '
	    getmounts | sh
	    touch /var/lock/autofs
	fi
	;;
  stop)
	kill -TERM $(/bin/pidof /bin/automount)
	rm -f /var/lock/autofs
	;;
  reload|restart)
	if [ ! -f /var/lock/autofs ]; then
		echo "Automounter not running"
		exit 1
	fi
	echo "Checking for changes to /etc/auto.master ...."
        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 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 -f $TMP1 $TMP2
	;;
  status)
	status
	;;
  *)
	echo "Usage: $initdir/autofs {init|stop|restart|reload|status}"
	exit 1
esac
}

redhat "$@"

exit 0
