#! /bin/sh
# $Id$
#
# /etc/init.d/gfarm-slapd
#
#   and its symbolic link
#
# /usr/sbin/rcgfarm-slapd
#
### BEGIN INIT INFO
# Provides: gfarm-slapd
# Required-Start: $network $syslog
# Required-Stop: $network $syslog
# Default-Start: 3 5
# Default-Stop: 0 1 2 6
# Description: LDAP slapd for Gfarm
### END INIT INFO

. /etc/rc.status

prog="slapd"
FILE="gfarm-slapd"

CONF="@config_gfarm_slapd_config@"
PORT=@config_gfarm_backend_port@
DAEMON="@config_gfarm_openldap_libexec@/$prog"
PIDFILE="@config_gfarm_backend_pid_file@"

# pull in sysconfig settings
[ -f "/etc/sysconfig/$FILE" ] && . "/etc/sysconfig/$FILE"

# We won't just use "checkproc", "startproc" and "killproc" here,
# because these functions don't allow multiple server processes for
# same binary.

rc_reset

case $1 in
    start)
	echo -n "Starting $FILE"
	# increase the maximum number of open file descriptors
	ulimit -n 16384 2> /dev/null
	if [ ! -x "$DAEMON" ]; then
		rc_failed 5 # program is not installed
	elif "$DAEMON" -f "$CONF" -h "ldap://:$PORT/"; then
		:
	else
		rc_failed 1 # generic or unspecified error
	fi		
	rc_status -v
	;;
    stop)
	echo -n "Shutting down $FILE"
	if [ -f "$PIDFILE" ] && kill -TERM `cat "$PIDFILE"`; then
		:
	elif [ -f "$PIDFILE" ] && [ -d /proc/`sed 's/[^0-9]//g' "$PIDFILE"` ]
	then
		rc_failed 4 # user had insufficient privileges
	else
		if [ -f "$PIDFILE" ]; then
			echo -n "$FILE (pid" `cat "$PIDFILE"`") is dead, but $PIDFILE remains"
		fi
		rc_failed 7 # program is not running
	fi
	rc_status -v
	;;
    try-restart)
	## Do a restart only if the service was active before.
	$0 status >/dev/null && $0 restart
	rc_status # Remember status and be quiet
	;;
    restart)
	$0 stop
	$0 start
	rc_status # Remember status and be quiet
	;;
    force-reload)
	echo -n "Reload service $FILE"
	$0 stop && $0 start
	rc_status # Remember status and be quiet
	;;
    reload)
	## Like force-reload, but if daemon does not support
	## signaling, do nothing (!)
	echo -n "Reload service $FILE"
	rc_failed 3 # unimplemented feature
	rc_status -v
	;;
    status)
	echo -n "Checking for service $FILE: "
	if [ -f "$PIDFILE" ] && [ -d /proc/`sed 's/[^0-9]//g' "$PIDFILE"` ]
	then
		:
	elif [ -f "$PIDFILE" ]; then
		rc_failed 1 # service dead, but pid file exists
	else
		rc_failed 3 # service not running (unused)
	fi
	rc_status -v
	;;
    *)
	echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload}"
	exit 1
	;;
esac
rc_exit
