#!/bin/sh
#
# Copyright (c) 2013 NetIQ Corporation and its affiliates. All Rights Reserved.
#
# Script to wait till nldap TLS port comes up or timeout after 60 secs approx.
#

# Template configuration variables

# Template configuration variables
if [ -f /usr/xpg4/bin/awk ]; then
 	AWK=/usr/xpg4/bin/awk
else
	AWK=awk
fi

#echo "awk:$AWK"
firstchar=`echo $0 | cut -c1`
dir=`dirname $0`
if [ "$firstchar" != "/" ]
then
  dir="`pwd`/$dir"
fi

#echo $dir
OS=`uname  -a | cut -c1-5`
if [ "$OS" = "HP-UX" ]
then
#NDSHOME=`echo $dir | $AWK -F"sbin" '{print $1}'`
NDSHOME=${dir%sbin*}
else
#NDSHOME=`echo $dir | $AWK -F"opt" '{print $0}'`
NDSHOME=`echo "$dir" | sed 's/^\(.*\)opt.*$/\1/'`
fi

default_conf=$NDSHOME/etc/opt/novell/eDirectory/conf
default_prefix=$NDSHOME/opt/novell/eDirectory
default_vardir=$NDSHOME/var/opt/novell/eDirectory/data

TEXTDOMAINDIR="$NDSHOME/opt/novell/eDirectory/share/locale"
export TEXTDOMAINDIR

ownername=`ls -l  $0 |awk '{print $3;}'`
ownerUid=`id $ownername | cut -d'=' -f2|cut -d'(' -f1 2>/dev/null`
default_config_file=`head -1 $default_conf/.edir/instances.$ownerUid 2> /dev/null`

if [ "$default_config_file" = "" ]
then
        default_config_file=$default_conf/nds.conf
fi

: ${NDS_CONF=$default_config_file}
: ${prefix=$default_prefix}

export NDS_CONF

#echo "prefix:$prefix"

#: ${NDS_CONF=/etc/nds.conf}
#: ${prefix=/usr}

configdir=`cat ${NDS_CONF=$default_config_file} |grep "^n4u.*configdir" | head -n 1 | sed 's/^.*=//'`
if [ -z "$configdir" ]; then
	 configdir=/etc
fi

localstatedir=`cat ${NDS_CONF=$default_config_file} |grep "^n4u.*dibdir" | head -n 1 | sed 's/^.*=//'`

libdir=`cat ${NDS_CONF=$default_config_file} |grep "^n4u.*libdir" | head -n 1 | sed 's/^.*=//'`

#prefix=/usr
exec_prefix=${prefix}
sbindir=${exec_prefix}/sbin
bindir=${exec_prefix}/bin
sysconfdir=/etc

if [ -z "$libdir" ]; then
	 libdir=${exec_prefix}/lib
fi

pkglibdir=$libdir/nds-modules

PATH=$PATH:/usr/local/bin

 if [ -d /usr/lib/lwp ]
 then
 LD_LIBRARY_PATH=/usr/lib/lwp:$LD_LIBRARY_PATH
 export LD_LIBRARY_PATH
 fi


#echo "configdir is:$configdir"
#
# 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 $sbindir/nldap ]; then 
		$sbindir/nldap -c --config-file $NDS_CONF > /dev/null 2>&1  # check the status
		returnValue=$?
		if [ $returnValue -eq 0 ]; then
			echo `gettext nds "Opentext eDirectory LDAP Server is listening on the TCP port."`
			break
		fi
	else
		echo `gettext nds "Opentext eDirectory $sbindir/nldap not found."`
		exit 1
	fi

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

	if [ $returnValue -eq 255 ]; then 
		echo `gettext nds "Opentext eDirectory LDAP Server TCP port is disabled."`
	elif [ $returnValue -ne 0 ]; then 
		echo `gettext nds "Opentext eDirectory LDAP Server is not listening on the TCP port."`
	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 $sbindir/nldap ]; then 
		$sbindir/nldap -s --config-file $NDS_CONF  > /dev/null 2>&1  # check the status
		returnValue=$?
		if [ $returnValue -eq 0 ]; then
			echo `gettext nds "Opentext eDirectory LDAP Server is listening on the TLS port."`
			exit 0
		fi

		if [ $returnValue -eq 204 ]; then
                        if [ $count -ge 10 ]; then
                                echo `gettext nds "LDAP Server is not associated with a certificate."`
                                exit 1
                        fi
		fi

		if [ $returnValue -eq 255 ]; then 
			echo `gettext nds "Opentext eDirectory LDAP Server TLS port is disabled."`
			exit 1
		fi
	else
		echo `gettext nds "Opentext eDirectory $sbindir/nldap not found."`
		exit 1
	fi

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

	$sbindir/nldap -c --config-file $NDS_CONF > /dev/null 2>&1  # check the status
	if [ "$?" -eq 1 ]; then
		echo `gettext nds "Opentext eDirectory LDAP services are not running."`
		exit 1
	else
		echo `gettext nds "Opentext eDirectory LDAP Server is not listening on the TLS port."`
	fi
}	# GetTLSPortStatus

#
# MAIN FUNCTION
# 

if [ -f $localstatedir/nds.db ]; then 
	GetTCPPortStatus   # TCP Port status
	GetTLSPortStatus   # TLS Port status
else
        exit 1
fi

