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

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

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

#########################################
# pkg_get_basedir
#
# Get basedir of package $1
##########################################
pkg_get_basedir () {
    debug "pkg_get_basedir" $1
    pkg=$1
    num=`rpm -q --queryformat '%{INSTPREFIXES}' "${pkg}" 2>/dev/null | sort | uniq | wc -l`
    if [ $num -eq 0 ]; then
#	echo "ERROR: The following package is not installed: ${pkg}"
	return 1
    fi
    BASEDIR=`rpm -q --queryformat '%{INSTPREFIXES}' "${pkg}" | head -1`
    if [ "$BASEDIR" = "(none)" ] ; then
	BASEDIR="/"
    fi
    return 0
}
##########################################
# init() must define:
#     DS_51_SERVERROOT /var/ds5
#     DS_52_SERVERROOT 
#     LDAPSEARCH_PATH /usr/bin
#     GREP_PATH /usr/bin
#     CUT_PATH /usr/bin
##########################################
init() {
    pkg_get_basedir sun-directory-server
    DS_BASEDIR=$BASEDIR
    DS_51_SERVERROOT=""
    DS_52_SERVERROOT=`cat $DS_BASEDIR/directory-server/5.2/shared/config/serverroot.conf`
    LDAPSEARCH_PATH=/usr/bin
    GREP_PATH=/bin
    CUT_PATH=/bin
}

start_ds_51(){
    return 0
}

stop_ds_51(){
    return 0
}

start_admin_ds_51(){
    return 0
}

stop_admin_ds_51(){
    return 0
}

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

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

is_cluster_mode() {
    return 1
}

# 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
