#!/sbin/sh
#
# Copyright (c) 2003 by Sun Microsystems, Inc.
# All rights reserved.
#
#ident	"@(#)$RCSfile: directorysnmp,v $	$Revision: 1.1.6.1 $ - $Date"

if [ "x"$VERBOSE != "x" ] ; then
    set -x
fi

debug() {
    if [ "x"$VERBOSE != "x" ] ; then
	echo $1
    fi
}

##########################################
# init() must define:
#     DS_BASEDIR
#     DS_SERVERROOT 
#     CUT_PATH /usr/bin
##########################################
init() {
    DS_BASEDIR=`pkginfo -r SUNWdsvu | head -1`
    DS_SERVERROOT=`cat $DS_BASEDIR/etc/mps/admin/v5.2/shared/config/serverroot.conf`
    CUT_PATH=/usr/bin
}

get_ldapagt_path() {
    if [ "`isainfo -b`" = "64" ]; then
	LDAPAGT_PATH=$DS_SERVERROOT/bin/slapd/server/sparcv9
    else
	LDAPAGT_PATH=$DS_SERVERROOT/bin/slapd/server
    fi
}

DIR=""

[ -z "$DS_SERVERROOT" ] || exit 0

for di in $DS_SERVERROOT/slapd-*
do
	[ -d "$di" ]  && DIR="$DIR $di"
done

case "$1" in
start)
	[ ! -z "$DIR" ] || exit 0
	#
	# Give our master agent a chance to start.
	# It is started asynchronously by snmpdx
	# just before us at boot time.
	#
	loop_counter=1
	max_count=10
	while [ $loop_counter -le $max_count ]; do
	    n=`/usr/bin/pgrep -f magt/CONFIG | /usr/bin/wc -l`
	    if [ $n -gt 0 ]; then break; fi
	    loop_counter=`expr $loop_counter + 1`
	    sleep 1
	done
	if [ $n -eq 0 ]; then
	    echo "Directory Server agents not started."
	    echo "The MPS agent must be started first."
	    exit 1
	fi
	( 
	    get_ldapagt_path
	    cd $LDAPAGT_PATH
	    for di in $DIR; do
		./ns-ldapagt -d $di >/dev/null 2>&1
	    done
	)
	;;

stop)
	[ ! -z "$DIR" ] || exit 0
	for di in $DIR; do
	    lkfile=${di}/logs/NSLDAPAGT.LK
	    if [ -r $lkfile ]; then
		pid=`$CUT_PATH/cut -f 1 -d' ' $lkfile`
		kill $pid >/dev/null 2>&1
	    fi
	done
	;;

*)
	echo "Usage: $0 { start | stop }"
	exit 1
	;;
esac

exit 0
