#!/bin/sh
#
# ldap		Startup script for the OpenLDAP server
#
# chkconfig:	345 80 10
#
# description:	OpenLDAP is a Lightweight Directory Access Protocol server
#
# processname:	slapd
# pidfile:	/var/run/slapd.pid


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

# Get network config
. /etc/sysconfig/network

# Get service config
[ -f /etc/sysconfig/ldap ] && . /etc/sysconfig/ldap

# Check that networking is up.
if is_no "${NETWORKING}"; then
        # nls "ERROR: Networking is down. %s can't be run." <service>
        msg_network_down OpenLDAP
        exit 1
fi
			

# See how we were called.
case "$1" in
  start)
	# Check if the service is already running?
	if [ ! -f /var/lock/subsys/ldap ]; then
		msg_starting "OpenLDAP"
		ARGS="-u slapd -g slapd"
		if [ -n "$SLAPDSYSLOGLEVEL" ] ; then
			ARGS="$ARGS -s $SLAPDSYSLOGLEVEL"
		fi
		if [ -n "$SLAPDURLLIST" ] ; then
			ARGS="$ARGS -h \"$SLAPDURLLIST\""
		fi
		daemon slapd $ARGS
		if grep -q '^replogfile' /etc/openldap/slapd.conf; then
			show "Starting OpenLDAP Update Replication Daemon"
			daemon --user slapd slurpd
		fi
		RETVAL=$?
		[ $RETVAL -eq 0 ] && touch /var/lock/subsys/ldap
		if [ $RETVAL -eq 0 -a -S /var/run/ldapi ] ; then
			chown "$LDAPI_SOCKET_OWNER"."$LDAPI_SOCKET_GROUP" /var/run/ldapi && \
			chmod "$LDAPI_SOCKET_MODE" /var/run/ldapi 
		fi
	else
		msg_already_running "OpenLDAP"
		exit 1
	fi
	;;
  stop)
	if [ -f /var/lock/subsys/ldap ]; then
	    msg_stopping "OpenLDAP"
	    killproc slapd
	    if grep -q '^replogfile' /etc/openldap/slapd.conf; then
		    msg_stopping "OpenLDAP Update Replication Daemon"
		    killproc slurpd
 	    fi
	    rm -f /var/lock/subsys/ldap >/dev/null 2>&1
	else
	    msg_not_running "OpenLDAP"
	    exit 1
	fi
	;;
  status)
	status slapd
	if grep -q '^replogfile' /etc/openldap/slapd.conf; then
		status slurpd
	fi
	exit $?
	;;
  restart)
	$0 stop
	$0 start
	;;
  *)
	msg_usage "$0 {start|stop|restart|status}"
	exit 1
esac

exit $RETVAL
