#! /bin/sh
# STTA Rev. 1.3 Copyright (c) 2001-2002 Jesús Pérez Lorenzo  --- license GNU GPL
# stta: (@#) 1.31051043346- [2004_01_29_102036]
# Rc for OpenLdap for STTA Ldap Servers
#
USAGE="$0 [ star|stop ] [ ldapserver-Names ] ";
HOSTNAME=`hostname`
STTA_TOOLSDIR="/opt/tarantella/tools"
if [ -z "$LDAP_daemon" ] ; then 
     [ -x $STTA_TOOLSDIR/setenv_vars ] &&  . $STTA_TOOLSDIR/setenv_vars
fi
[ -z "$LDAP_daemon" ] && echo "LDAP_daemon $msg_envnotset. $msg_runsttacfg (shells) !!! " && exit 2
#debug_args="-d 256";
if [ -n "$2" ]
then
    case "$2" in 
        $LDAP_dirname|$LDAPCACHE_dirname) ServerNames=$2  ;;
        *) echo "$msg_noldapserver" ; exit 2 ;;
    esac
else
    ServerNames="$LDAP_dirname $LDAPCACHE_dirname"
fi
ahora=`date '+%d-%m-%Y %H:%M:%S'`
case "$1" in
    start)
        echo -n "$msg_ldapstart"
        for nom in `echo $ServerNames`
        do
             DEBUG_daemon="";
             case "$nom" in 
                $LDAP_dirname) 
                              [ "$LDAP_TYPE" = "IPLANET" ] && continue 
                              [ "$LDAP_debug" -gt 0 ] && DEBUG_daemon="-d $LDAP_debug" 
                              echo "$ahora: $LDAP_daemon -h ldap://$HOSTNAME:$LDAP_port/ -f $LDAP_conf $DEBUG_daemon " >> $LDAP_log 
                              $LDAP_daemon -h ldap://$HOSTNAME:$LDAP_port/ -f $LDAP_conf $DEBUG_daemon 2>> $LDAP_acclog &
                             [ $? -eq 0 ] && echo $N "$nom $DEBUG_daemon $C"
                              ;;
                $LDAPCACHE_dirname) 
                             [ "$LDAPCACHE_TYPE" = "IPLANET" ] && continue 
                             [ "$LDAPCACHE_debug" -gt 0 ] && DEBUG_daemon="-d $LDAPCACHE_debug" 
                             echo "$ahora : $LDAP_daemon -h ldap://$HOSTNAME:$LDAPCACHE_port/ -f $LDAPCACHE_conf $DEBUG_daemon ">> $LDAPCACHE_log 
                             $LDAP_daemon -h ldap://$HOSTNAME:$LDAPCACHE_port/ -f $LDAPCACHE_conf $DEBUG_daemon 2>> $LDAPCACHE_acclog &
                             [ $? -eq 0 ] && echo $N "$nom $DEBUG_daemon $C"
                             ;;
             esac
        done
        ;;
    stop)
        echo -n "$msg_ldapstop"
        for nom in `echo $ServerNames`
        do
             SLAP_CMD="";
             [ "$LDAP_dirname" = "$nom" ] && SLAPD_CMD="$LDAP_daemon -h ldap://$HOSTNAME:$LDAP_port/"
             [ "$LDAPCACHE_dirname" = "$nom" ] && SLAPD_CMD="$LDAP_daemon -h ldap://$HOSTNAME:$LDAPCACHE_port/"
             if [ -n "$SLAPD_CMD" ]  ; then 
               pids=`ps -ef | grep "$SLAPD_CMD" | grep -v grep | awk '{ print $2 }' `
               if [ -n "$pids" ] ; then
                       kill -TERM $pids
                       if [ $? -eq 0 ] ; then 
                           echo $N "$nom $C" 
                           [ "$LDAPCACHE_dirname" = "$nom" ] && echo "$ahora : $nom $1 ">> $LDAPCACHE_log 
                           [ "$LDAP_dirname" = "$nom" ] && echo "$ahora : $nom $1 ">> $LDAP_log 
                       fi
               fi
             fi
        done
        ;;
    restart)
	$0 stop
	# sometimes slapd needs some time to stop
	sleep 3
	$0 start
        ;;
    *)
	echo "$USAGE"
        exit 1
esac
echo " "
