#! /bin/sh

#  /etc/init.d/am-utils: start and stop the 4.4BSD automouter.
#  Copyright (C) 2001-2003 Philippe Troin <phil@fifi.org>
#  Copyright (C) 1996-98 by Dominik Kubla, <kubla@Uni-Mainz.DE> and
#                        Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
#
# AMD needs to be started _AFTER_ NFS and NIS are up...
#
# $Id: am-utils.init,v 1.16 2003/07/17 21:56:00 phil Exp $

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

# Defaults
CONF=/etc/default/am-utils

# If the package is removed, skip it.
test -x /usr/sbin/amd || exit 0

get_amd_args() {
    if [ ! -f "$CONF" ]
    then
	echo "$0: not configured yet (try running \"dpkg-reconfigure am-utils\")." 1>&2
	exit 1
    fi
    . "$CONF"

    if test "$AM_UTILS_USE_NIS" = true
    then
	map="$AM_UTILS_NIS_MASTER_MAP"
	case "$AM_UTILS_NIS_MASTER_MAP_KEY_STYLE" in
	    config)
		key="$AM_UTILS_NIS_KEY"
		AMDARGS="`ypmatch \"$key\" \"$map\"`"
		;;
	    onekey)
		AMDARGS="`ypcat \"$map\"`"
		;;
	    mountpoint)
		AMDARGS="`ypcat -k \"$map\"`"
		;;
	    custom)
		AMDARGS="`sh -c \"$AM_UTILS_NIS_CUSTOM\"`"
		;;
	    *)
		echo "$0: bad key style"
		exit 1
		;;
	esac
	if [ -z "$AMDARGS" ]
	then
	    echo "$0: cannot find master map" 1>&2
	    exit 1
	fi
    else
	AMDARGS=""

	if [ "$AM_UTILS_MAP_NET" = true ]
	then
	    AMDARGS="$AMDARGS /net /usr/share/am-utils/amd.net"
	fi

	if [ "$AM_UTILS_MAP_HOME" = true ]
	then
	    AMDARGS="$AMDARGS /home /etc/passwd"
	fi

	AMDARGS="$AMDARGS $AM_UTILS_MAP_OTHERS"
    fi
}

start_amd() {
    pid="`amq -p 2>/dev/null`"
    if [ -n "$pid" ]; then
	echo "Starting automounter: amd is already running" 1>&2
	exit 1
    fi

    # If the hostname is not a FQHN, add the -d command line switch,
    # so that hostnames in the maps are stripped correctly.
    case `hostname` in
	*.*)	dnsdomain=""
		;;
	*)	dnsdomainname=`dnsdomainname`
		if [ "$dnsdomainname" ] ; then
			dnsdomain="-d $dnsdomainname"
		else
			echo "$0: please setup your domainname" 1>&2
			exit 1
		fi
		;;
    esac

    get_amd_args

    echo -n "Starting automounter: amd"
    /usr/sbin/amd -F /etc/am-utils/amd.conf $dnsdomain $AMDARGS
    echo "."
}

stop_amd() {
    pid="`amq -p 2>/dev/null`"
    if [ -z "$pid" ]; then
	echo "Stopping automounter: amd not running" 1>&2
	if [ $# -eq 0 ]
	then
	    exit 1
	else
	    return
	fi
    fi
    echo -n "Stopping automounter: amd "
    kill -s TERM "$pid"
    # wait until amd has finished; this may take a little bit, and amd can't
    # start while an old one is running
    i=0
    maxsecs=120
    while [ $i -le $maxsecs ] && kill -s 0 $pid >/dev/null 2>&1; do
	echo -n "."
	sleep 1
	i="`expr $i + 1`"
	[ $i -eq 15 ] && echo -n " [will wait `expr $maxsecs - $i` secs more] "
    done
    if kill -s 0 $pid >/dev/null 2>&1
    then
	echo " failed."
	exit 1
    else
	echo " done."
    fi
}

case "$1" in
  start)
	start_amd
	;;

  stop)
	stop_amd
	;;


  restart|force-reload)
	stop_amd -noquit
	start_amd
	;;

  reload)
	# amd tests itself if its map files have changed, so nothing to do here
	;;

  *)
	echo "Usage: /etc/init.d/amd {start|stop|restart|[force-]reload}"
	exit 1
esac

exit 0
