#!/bin/sh

sysconfdir="/etc"
sbindir="/usr/sbin"

OPTIONS=""
SLAPD_OPTIONS="-u ldap"
SLAPD_SERVICES=""
SLURPD_OPTIONS=""

if [ -r "$sysconfdir/sysconfig/ldap" ]; then
	. "$sysconfdir/sysconfig/ldap"
fi

start() {
	[ -f "$sysconfdir/openldap/slapd.conf" ] || exit 1
	"$sbindir/slaptest" || exit 1

	if [ -z "$SLAPD_SERVICES" ]; then
		if grep '^TLS' "$sysconfdir/openldap/slapd.conf" >/dev/null; then
			SLAPD_SERVICES="ldapi:/// ldap:/// ldaps:///"
		else
			SLAPD_SERVICES="ldapi:/// ldap:///"
		fi
	fi

	"$sbindir/slapd" -h "$SLAPD_SERVICES" $OPTIONS $SLAPD_OPTIONS
	if grep '^replogfile' "$sysconfdir/openldap/slapd.conf" >/dev/null; then
		"$sbindir/slurpd" $OPTIONS $SLURPD_OPTIONS
	fi
}

stop() {
	pkill -x slapd
	pkill -x slurpd
}

reload() {
	restart
}

restart() {
	stop
        sleep 2
	start
}

on() {
        ln -s ../init.d/ldap /etc/rcS.d/K40ldap
        ln -s ../init.d/ldap /etc/rc0.d/K40ldap
        ln -s ../init.d/ldap /etc/rc1.d/K40ldap
        ln -s ../init.d/ldap /etc/rc2.d/S70ldap
}

off() {
        rm -f /etc/rc?.d/[SK][0-9][0-9]ldap
}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		restart
		;;
	reload)
		reload
		;;
        on)
                on
                ;;
        off)
                off
                ;;
	*)
		echo "Usage: $0 { start | stop | restart | reload }"
		exit 1
                ;;
esac

exit 0

