#!/bin/bash

# Some parts borrowed from /etc/init.d/slapd from Debian sarge

# Stop processing if slapd is not there
[ -x /usr/sbin/slapd ] || exit 0

# Source the init script configuration
if [ -f "/etc/default/slapd" ]; then
    . /etc/default/slapd
fi

# Load the default location of the slapd config file
if [ -z "$SLAPD_CONF" ]; then
    if [ -e /etc/ldap/slapd.conf ] ; then
	# I think I am on Debian
	SLAPD_CONF="/etc/ldap/slapd.conf"
    else 
	if [ -e /etc/openldap/slapd.conf ] ; then
	    # I think I am on Debian
	    SLAPD_CONF="/etc/openldap/slapd.conf"
	else
	# Where am I?
	    echo "ERROR ... Can't find configuration file"
	    exit 1
	fi
    fi
else
    SLAPD_OPTIONS="-f $SLAPD_CONF $SLAPD_OPTIONS"
    SLURPD_OPTIONS="-f $SLAPD_CONF $SLURPD_OPTIONS"
fi

# Stop processing if the config file is not there
if [ ! -r "$SLAPD_CONF" ]; then
    cat <<EOF >&2
No configuration file was found for slapd at $SLAPD_CONF.
If you have moved the slapd configuration file please modify
/etc/default/slapd to reflect this.  If you chose to not
configure slapd during installation then you need to do so
prior to attempting to start slapd.
An example slapd.conf is in /usr/share/slapd
EOF
    exit 0 # Should this be 1?
fi

# Find out the name of slapd's pid file
if [ -z "$SLAPD_PIDFILE" ]; then
	SLAPD_PIDFILE=`sed -ne 's/^pidfile[[:space:]]\+\(.\+\)/\1/p' \
		"$SLAPD_CONF"`
fi
if [ -z "$SLAPD_PIDFILE" ]; then
    cat <<EOF >&2
The pidfile for slapd is neither specified in "$SLAPD_CONF" nor
in /etc/default/slapd. Consequently, slapd will not be started.
EOF
    exit 1
fi


# Howto use if [ "`eval slapd_started`" = YES ] ; then

function slapd_started {
    if [ -e ${SLAPD_PIDFILE%.pid}.started ] ; then
	echo YES
    else
	echo NO
    fi
}

#if [ -e ${SLAPD_PIDFILE%.pid}.started ] ; then echo Started ; else echo Stopped ; fi
