#!/bin/sh
# chkconfig: - 29 71
# description: Creates user group directories on demand

### BEGIN INIT INFO
# Provides: autogroup
# Required-Start: $local_fs
# Required-Stop: $local_fs
# Short-Description: Creates group home directories on demand
# Description: Autogroup creates group home directories in a transparent manner.
#       It relies on the autofs protocol for its operation.
### END INIT INFO

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

# Source configuration
. /etc/sysconfig/autogroup

prog=autogroup
RETVAL=0

start() {

	# If autofs old module is loaded unload it now
	grep -q -w autofs /proc/modules || \
		/sbin/rmmod autofs &>/dev/null

	# If module is not loaded load it now
	grep -q autofs4 /proc/modules || \
		/sbin/modprobe -k autofs4 &>/dev/null

	# See if autofs module actually loaded
	if ! grep -q -w autofs4 /proc/modules
	then
		exit 1
	fi

	echo -n $"Starting $prog: "

	daemon /usr/sbin/autodir -d $AUTOGROUP_HOME -m $AUTOGROUP_MODULE \
		${AUTOGROUP_OPTIONS+"-o $AUTOGROUP_OPTIONS"} \
		${AUTOGROUP_TIMEOUT+"-t $AUTOGROUP_TIMEOUT"} \
		${AUTOGROUP_BACKUP+"-b $AUTOGROUP_BACKUP"} \
		${AUTOGROUP_BACKWAIT+"-w $AUTOGROUP_BACKWAIT"} \
		${AUTOGROUP_BACKPRI+"-p $AUTOGROUP_BACKPRI"} \
		${AUTOGROUP_MAXBACK+"-c $AUTOGROUP_MAXBACK"} \
		-l /var/run/$prog.pid

	RETVAL=$?
	[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
	echo
}

stop() {
	echo -n $"Stopping $prog: "
	killproc $prog
	RETVAL=$?
	[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
	echo
}

restart() {
	stop
	start
}

dostatus() {
	pid=`pidfileofproc $prog`

	if [ -n "$pid" -a -d "/proc/$pid" ]
	then
		echo "$prog (pid $pid) running..."
		RETVAL=0
	else
		echo "$prog stopped"
		RETVAL=1
	fi
}

# See how we were called.
case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  restart|reload)
	restart
	;;
  condrestart)
	[ -f /var/lock/subsys/$prog ] && restart
	;;
  status)
	dostatus
	;;
  *)
	echo $"Usage: $prog {start|stop|restart|reload|condrestart|status}"
	exit 1
esac

exit $RETVAL

