#!/sbin/runscript
#
# Copyright (c) 2004 Novell, Inc.
# All rights reserved.
#
# Script to wait till nldap TLS port comes up or timeout after 60 secs approx.
#
# Simplyfied and Gentooized by Mario Fetka <fetka@arge.at>


depend() {
  need ndsd
  use ndsd
  after ndsd
}

checkconfig() {
  if [ ! -e /var/nds/ndsd.pid ] ; then
     eerror "Novell eDirectory server has not been started."
     return 1
  fi
}

#
# Get nldap TCP Port status 
# Cases:
# 1. nldap not loaded (nldap -c return 1 to the shell)
# 2. nldap TCP port is not listening (nldap -c returns 176 to the shell)
# 3. nldap 389 is disabled (nldap -c return 255 to the shell)
# 4. nldap TCP port is listning (nldap -c return 0 to the shell)

GetTCPPortStatus () {
count=1
while [ $count -ne 30 ] 
do
	if [ -f /opt/novell/eDirectory/sbin/nldap ]; then 
		/opt/novell/eDirectory/sbin/nldap -c > /dev/null 2>&1  # check the status
		returnValue=$?
		if [ $returnValue -eq 0 ]; then
			ebegin "Novell eDirectory LDAP Server TCP port is listening"
			eend 0
			break
		fi
	else
		ebegin "Novell eDirectory /opt/novell/eDirectory/sbin/nldap not found"
		eend 1
	fi

	sleep 1				# wait for maximum 60 seconds
	count=`expr $count + 1`        
done

	if [ $returnValue -eq 255 ]; then 
		ebegin "Novell eDirectory LDAP Server TCP port is disabled"
		eend 1
	elif [ $returnValue -ne 0 ]; then 
		ebegin "Novell eDirectory LDAP Server TCP port is not listening"
	eend 1
	fi
}	# GetTCPPortStatus

#
# Get nldap TLS Port status 
# Cases:
# 1. nldap not loaded (nldap -s return 1 to the shell)
# 2. nldap TLS port is not listening (nldap -s returns 176 to the shell)
# 3. nldap 636 is disabled (nldap -s return 255 to the shell)
# 4. nldap TLS port is listning (nldap -s return 0 to the shell)

GetTLSPortStatus () {

count=1
while [ $count -ne 30 ] 
do
	if [ -f /opt/novell/eDirectory/sbin/nldap ]; then 
		/opt/novell/eDirectory/sbin/nldap -s > /dev/null 2>&1  # check the status
		returnValue=$?
		if [ $returnValue -eq 0 ]; then
			ebegin "Novell eDirectory LDAP Server TLS port is listening"
	eend 0
		fi
		
		if [ $returnValue -eq 204 ]; then
			if [ $count -ge 10 ]; then
				ebegin "LDAP Server is not associated with Certificate"
	eend 1
			fi			
		fi

		if [ $returnValue -eq 255 ]; then 
			ebegin "Novell eDirectory LDAP Server TLS port is disabled"
	eend 1
		fi
	else
		ebegin "Novell eDirectory $sbindir/nldap not found"
	eend 1
	fi

	sleep 1				# wait for maximum 60 seconds
	count=`expr $count + 1`        
done

	/opt/novell/eDirectory/sbin/nldap -c > /dev/null 2>&1  # check the status
	if [ "$?" -eq 1 ]; then
		ebegin "Novell eDirectory LDAP services are not running"
	eend 1
	else
		ebegin "Novell eDirectory LDAP Server TLS port is not listening"
	eend 1
	fi
}	# GetTLSPortStatus

#
# MAIN FUNCTION
# 

start() {
if [ -f /var/nds/ndsd.pid ]; then
        GetTCPPortStatus   # TCP Port status
        GetTLSPortStatus   # TLS Port status
else
	eend 1
fi
}

stop() {
	/opt/novell/eDirectory/sbin/nldap -u > /dev/null 2>&1  # check the status
	if [ "$?" -eq 1 ]; then
		ebegin "Novell eDirectory LDAP services are not running"
	eend 1
	else
		ebegin "Novell eDirectory LDAP Server TCP and TLS  port is not listening"
	eend 0
	fi
}

#restart() {
#  (Commands necessary to restart the service)
#}