#!/bin/bash
#
### BEGIN INIT INFO
# Provides:          autodir
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Should-Start:      autofs
# Should-Stop:       autofs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start autodir daemons at boot start
# Description:       Automatically manages home/group directories for LDAP/NIS/SQL/local accounts
### END INIT INFO
#
# autodir - Init script used to run autodir on Debian
#           This is written starting from the script provided by
#           autodir upstream for general use.
#
#   Copyright (C) 2005-2009 Francesco P. Lovergine <frankie@debian.org>
#   
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#
#   On Debian the license is available in /usr/share/common-license/GPL
#

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/autodir
NAME=autodir
DESC=Autodir

[ -x $DAEMON ] || exit 0

RUN_AUTOHOME="no"
RUN_AUTOGROUP="no"

# Include autodir defaults if available
[ -r /etc/default/autodir ] && . /etc/default/autodir

AUTOHOME_PID=/run/autohome.pid
AUTOGROUP_PID=/run/autogroup.pid
AUTOHOME_LOCKDIR=/run/lock/autodir
AUTOHOME_LOCK=$AUTOHOME_LOCKDIR/autohome
AUTOGROUP_LOCK=$AUTOHOME_LOCKDIR/autogroup

log_daemon_msg() {
        echo -n "$1: $2"
}

log_end_msg() {
        if [ $1 -ne 0 ]; then
                echo " failed!"
        else
                echo "."
        fi
}

# Make sure the autofs filesystem type is available.
load_autofs() {
        ( grep -q autofs /proc/filesystems || /sbin/modprobe -s autofs4 ) 2>/dev/null
}

run_autohome() {
	RETVAL=0
  	if [ "$RUN_AUTOHOME" = "yes" ]; then
		load_autofs
		mkdir $AUTOHOME_LOCKDIR 2>/dev/null || true
		start-stop-daemon --start --quiet --pidfile $AUTOHOME_PID --exec $DAEMON -- \
			-d $AUTOHOME_HOME -m $AUTOHOME_MODULE \
                	${AUTOHOME_OPTIONS+ -o $AUTOHOME_OPTIONS} \
			${AUTOHOME_TIMEOUT+ -t $AUTOHOME_TIMEOUT} \
			${AUTOHOME_BACKUP+ -b $AUTOHOME_BACKUP} \
                	${AUTOHOME_BACKWAIT+ -w $AUTOHOME_BACKWAIT} \
			${AUTOHOME_BACKPRI+ -p $AUTOHOME_BACKPRI} \
			${AUTOHOME_MAXBACK+ -c $AUTOHOME_MAXBACK} \
			-l $AUTOHOME_PID 2>/dev/null
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch $AUTOHOME_LOCK
	fi
	return $RETVAL
}

kill_autohome() {
	RETVAL=0
	if [ -r $AUTOHOME_PID ]; then
		start-stop-daemon --stop --quiet --pidfile $AUTOHOME_PID --exec $DAEMON
		RETVAL=$?
		[ $RETVAL -eq 0 ] && rm -f $AUTOHOME_LOCK
	fi
	return $RETVAL
}

run_autogroup() {
	RETVAL=0
  	if [ "$RUN_AUTOGROUP" = "yes" ]; then
		load_autofs
		mkdir $AUTOHOME_LOCKDIR 2>/dev/null || true
		start-stop-daemon --start --quiet --pidfile $AUTOGROUP_PID --exec $DAEMON -- \
			-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 $AUTOGROUP_PID 2>/dev/null
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch $AUTOGROUP_LOCK
	fi
	return $RETVAL
}

kill_autogroup() {
	RETVAL=0
	if [ -r $AUTOGROUP_PID ]; then
		start-stop-daemon --stop --quiet --pidfile $AUTOGROUP_PID --exec $DAEMON
		RETVAL=$?
		[ $RETVAL -eq 0 ] && rm -f $AUTOGROUP_LOCK
	fi
	return $RETVAL
}

[ -r /lib/lsb/init-functions ] && . /lib/lsb/init-functions

[ -d /var/lock/autodir ] || mkdir /var/lock/autodir

set -e

case "$1" in
  start)
	log_daemon_msg "Starting $DESC" "$NAME"
	run_autohome && run_autogroup
	log_end_msg $?
	;;
  stop)
	log_daemon_msg "Stopping $DESC" "$NAME"
	kill_autohome && kill_autogroup
	log_end_msg $?
	;;
  restart|force-reload)
	log_daemon_msg "Restarting $DESC" "$NAME"
	kill_autohome && kill_autogroup
	sleep 1 
	run_autohome && run_autogroup
	log_end_msg $?
	;;
  *)
	N=/etc/init.d/$NAME
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
