#!/bin/sh
#
# ldap	This shell script takes care of starting and stopping
#	ldap servers (slapd and slurpd).
#
# chkconfig: - 39 61
# description:	LDAP stands for Lightweight Directory Access Protocol, used \
#				for implementing the industry standard directory services.
# processname: slapd
# config: /etc/openldap/slapd.conf
# pidfile: /var/run/slapd.pid

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

# Source networking configuration.
# Check that networking is up.
SourceIfNotEmpty /etc/sysconfig/network && [ "$NETWORKING" != no ] || exit

# Source an auxiliary options file if we have one, and pick up OPTIONS,
# SLAPD_OPTIONS, and SLURPD_OPTIONS.
SourceIfNotEmpty /etc/sysconfig/ldap

SLAPD=/usr/sbin/slapd
SLURPD=/usr/sbin/slurpd
CONFIG=/etc/openldap/slapd.conf

[ -x "$SLAPD" -a -x "$SLURPD" -a -s "$CONFIG" ] || exit

LOCKFILE=/var/lock/subsys/ldap
RETVAL=0

function start() {
	# Start daemons.
	echo -n "Starting slapd:"
	if grep -qs ^TLS "$CONFIG"; then
		daemon ${SLAPD} -u ldap -h '"ldap:/// ldaps:///"' $OPTIONS $SLAPD_OPTIONS
		RETVAL=$?
	else
		daemon ${SLAPD} -u ldap $OPTIONS $SLAPD_OPTIONS
		RETVAL=$?
	fi
	echo
	[ $RETVAL -eq 0 ] || return
	if grep -qs "^replogfile" "$CONFIG"; then
		echo -n "Starting slurpd:"
		daemon ${SLURPD} $OPTIONS $SLURPD_OPTIONS
		RETVAL=$?
		echo
	fi
	[ $RETVAL -eq 0 ] && touch "$LOCKFILE"
}

function stop() {
	# Stop daemons.
	echo -n "Shutting down slapd: "
	killproc ${SLAPD}
	RETVAL=$?
	echo
	[ $RETVAL -eq 0 ] || return
	if grep -qs "^replogfile" "$CONFIG"; then
		echo -n "Shutting down slurpd: "
		killproc ${SLURPD}
		RETVAL=$?
		echo
	fi
	[ $RETVAL -eq 0 ] && rm -f "$LOCKFILE" /var/run/slapd.args
}

restart()
{
	stop
	start
}

# Function reload removed becose not supported

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	status)
		status ${SLAPD}
		if grep -qs "^replogfile" "$CONFIG"; then
			status ${SLURPD}
		fi
		;;
	restart)
		restart
		;;
	reload)
		echo "This options not supported. Please use restart"
		;;
	condstop)
		if [ -e "$LOCKFILE" ]; then
			stop
		fi
		;;
	condrestart)
		if [ -e "$LOCKFILE" ]; then
			restart
		fi
		;;
	*)
		echo "Usage: ${0##*/} {start|stop|reload|restart|condstop|condrestart|status}"
		RETVAL=1
esac

exit $RETVAL
