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


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

start_ds_51(){
    $DS_BASEDIR/usr/sbin/directoryserver -u 5.1 start
    return $?
}

stop_ds_51(){
    $DS_BASEDIR/usr/sbin/directoryserver -u 5.1 stop
    return $?
}

start_admin_ds_51(){
    $DS_BASEDIR/usr/sbin/directoryserver -u 5.1 start-admin
    return $?
}

stop_admin_ds_51(){
    $DS_BASEDIR/usr/sbin/directoryserver -u 5.1 stop-admin
    return $?
}

start_ds_52(){
    $DS_BASEDIR/usr/sbin/directoryserver -u 5.2 -b start
    return $?
}

stop_ds_52(){
    $DS_BASEDIR/usr/sbin/directoryserver -u 5.2 stop
    return $?
}

is_cluster_mode() {
    /usr/sbin/clinfo > /dev/null 2>&1
    return $?
}

# init() must define:
#     DS_52_SERVERROOT 
#     DS_51_SERVERROOT /var/ds5
#     LDAPSEARCH_PATH /usr/bin
#     GREP_PATH /usr/bin
#     CUT_PATH /usr/bin

# contact_servers
#
# input: $1 - the list of "<serverroot>/slapd-*"
# return: 0 - at least one server is running
#         1 - no server is running
contact_servers() {
	VERS="$1"

	[ $VERS != "5.2" -a $VERS != "5.1" ] && return 1

	# For 5.1 server
	IDIR="/usr/iplanet/ds5"

	for dir in $2; do
		if [ $VERS = "5.2" ]; then
			DSE=$dir/config/dse.ldif
		else
			SLAPDDIR=`/usr/bin/basename $dir`
			DSE="$IDIR/$SLAPDDIR/config/dse.ldif"
		fi
		PORT=`$GREP_PATH/grep -i 'nsslapd-port:' $DSE | $CUT_PATH/cut -f 2 -d' '`
		[ -z $PORT ] && PORT=389

		PORT_PARAM="-p $PORT"

		CMD="$LDAPSEARCH/ldapsearch $PORT_PARAM -b \"\" -s base objectclass=\* > /dev/null 2>&1"
		eval $CMD
		if [ $? -eq 0 ]; then
			echo "ldap server is running at port: $PORT"
			return 0
		fi
	done

	return 1
}

init

# for compatibility issue, we need to be able to start also 5.1
# instances, if there have not already been unconfigured.

DIR=""

[ ! -z "$DS_52_SERVERROOT" ] || exit 0

# Test if we are in a cluster and silently exit if so
is_cluster_mode
[ $? -eq 0 ] && exit 0

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

case "$1" in
start)
	[ ! -z "$DIR" ] || exit 0
	contact_servers "5.2" "$DIR"
	if [ $? -ne 0 ]; then
	    start_ds_52
	    if [ $? -ne 0 ]; then
		echo "$0: unable to start the Directory Server 5.2" 
		exit 1
	    fi
	fi
	;;

stop)
	[ ! -z "$DIR" ] || exit 0
	stop_ds_52
	if [ $? -ne 0 ]; then
          echo "$0: unable to stop the Directory Server 5.2" 
          exit 1
        fi
	;;

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

DIR=""

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

case "$1" in
start)
    [ ! -z "$DIR" ] || exit 0
    contact_servers "5.1" "$DIR"
    if [ $? -ne 0 ]; then
	start_ds_51
        if [ $? -ne 0 ]; then
          echo "$0: unable to start the Directory Server 5.1" 
          exit 1
        fi
	start_admin_ds_51
        if [ $? -ne 0 ]; then
          echo "$0: unable to start the Admin Server 5.1" 
          exit 1
        fi
    fi
    ;;

stop)
    [ ! -z "$DIR" ] || exit 0
    stop_admin_ds_51
    if [ $? -ne 0 ]; then
          echo "$0: unable to stop the Admin Server 5.1" 
          exit 1
        fi
    stop_ds_51
    if [ $? -ne 0 ]; then
          echo "$0: unable to stop the Directory Server 5.1" 
          exit 1
        fi
    ;;

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

exit 0
