#! /bin/sh
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL
# Rc for OpenLdap
#

USAGE="$0 [ star|stop ] [ ldapserver-Names ] ";
HOSTNAME=`hostname`

#debug_args="-d 256";

N=""
C=""
if echo -n | grep n > /dev/null
then
  C="\c"
else
  N="-n"
fi
case `uname -a` in
*inux*) N="$N -e" ;;
esac

if [ -n "$2" ]
then
    ServerNames=$2;
else
    ServerNames="ttaldap ttacache"
fi
ttaldap_args=" -h ldap://$HOSTNAME:389/ -f /etc/openldap/ttaldap.conf $debug_args "
ttacache_args=" -h ldap://$HOSTNAME:802/ -f /etc/openldap/ttacache.conf $debug_args "
SLAPD_BIN="/usr/local/libexec/slapd"

case "$1" in
    start)
        echo -n "Starting-up Open LDAP servers:"
        for nom in `echo $ServerNames`
        do
             eval LDAP_ARGS=\$$nom"_args"
             SLAPD_CMD=$SLAPD_BIN" "$LDAP_ARGS
             $SLAPD_CMD & 
             [ $? -eq 0 ] && echo $N "$nom $C"
        done
        ;;
    stop)
        echo -n "Shutting down ALL Open LDAP server: "
        for nom in `echo $ServerNames`
        do
            eval ldap_args=\$$nom"_args"
            LDAP_ARGS=`echo $ldap_args | cut -f1-2  -d"-" 2>/dev/null`
            SLAPD_CMD=$SLAPD_BIN" "$LDAP_ARGS
            pids=`ps -ef | grep "$SLAPD_CMD" | \
                       grep -v grep | awk '{ print $2 }' `
                if [ -n "$pids" ] ; then
                        kill -TERM $pids
                        [ $? -eq 0 ] && echo $N "$nom $C"
                fi
        done
        ;;
    restart)
	$0 stop
	# sometimes slapd needs some time to stop
	sleep 3
	$0 start
        ;;
    *)
	echo "$USAGE"
        exit 1
esac
echo " "
