#!/bin/sh
#ident "@(#)S86slapd	1.60 04/14/99 SMI"
#
#
# Copyright 04/14/99 Sun Microsystems, Inc. All Rights Reserved
#
#
# Start/Stop SunDS daemons
#
trap 'echo "$0: Interrupted "; exit 1' INT QUIT

USAGE="Usage: `basename $0` { start | stop | restart }"
BASEDIR=`/bin/pkgparam SUNWsds BASEDIR 2>/dev/null`
if [ -z "$BASEDIR" ]	# pkg not installed
then
	BASEDIR=/opt	# set defaul. Maybe the dir is nfs mounted
fi
PATH=/usr/sbin:/usr/bin:$BASEDIR/SUNWconn/ldap/lib:$BASEDIR/SUNWconn/ldap/sbin:$BASEDIR/SUNWconn/ldap/bin
LD_LIBRARY_PATH=$BASEDIR/SUNWconn/ldap/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH
#
# Set NLSPATH for I18N
#
NLSPATH=${NLSPATH}:${BASEDIR}/SUNWconn/lib/locale/%L/%N.cat
export NSLPATH
#
# NIS auxiallary daemon management
#
DSYPXFRDPID=/var/opt/SUNWconn/ldap/log/dsypxfrd.pid
DSYPXFRD=$BASEDIR/SUNWconn/ldap/lib/dsypxfrd
DSYPPASSWDD=$BASEDIR/SUNWconn/ldap/lib/dsyppasswdd

ExitCode=0

start_dsypxfrd() {

	#
	# Make sure NIS replication is enabled
	#

        enabled=`cat /etc/opt/SUNWconn/ldap/current/mapping/nis.mapping|grep "DBM_XFR_REPLICATION"| awk 'BEGIN { FS="=" } {print $2} END {}'`
	if [ "$enabled" = "enabled" ]
	then
		pid=`ps -fe | grep lib/dsypxfrd | grep -v grep|awk '{print $2}'`
		if [ -n "$pid" ]
		then
			echo "Another dsypxfrd process is already running"
		else	
                	echo "Starting NIS replication daemon (dsypxfrd)..."
        		$DSYPXFRD
		fi
	else

		#
		# Stop it if it is running
		#

		pid=`ps -fe | grep lib/dsypxfrd | grep -v grep|awk '{print $2}'`
		if [ -n "$pid" ]
		then
			echo "NIS replication disabled and dsypxfrd running!"
			stop_dsypxfrd
		fi
	fi
}

start_dsyppasswd() {

        pid=`ps -fe | grep lib/dsyppasswdd | grep -v grep|awk '{print $2}'`
        if [ -n "$pid" ]
        then
                echo "Another dsyppasswd process is already running"
        else
                echo "Starting NIS password daemon (dsyppasswdd)..."
                $DSYPPASSWDD
        fi
}

start_nis() {

	# Make sure NIS is enabled.

        state=`cat /etc/opt/SUNWconn/ldap/current/dsserv.conf|grep "^frontend" |grep nis|grep -v grep|grep -v frontend_|awk '{print $3}'`

	if [ "$state" = "on" ] 
	then

		# Make sure NIS configured as a server

		if [ -f /etc/opt/SUNWconn/ldap/current/nis.master ]
		then 
	
			start_dsypxfrd
			start_dsyppasswd
		fi

		#
		# Sleep for a while to make sure the NIS server is operational
		#

		sleep 5
	fi
}

stop_dsyprsvd() {
        pid=`ps -fe | grep dsyprsvd | grep -v grep|awk '{print $2}'`
        if [ -n "$pid" ]
        then
                echo "Stopping DNS forwarding daemon (dsyprsvd)..."
                kill -TERM ${pid}
        fi
}

stop_dsyppasswdd() {
        pid=`ps -fe | grep lib/dsyppasswdd | grep -v grep|awk '{print $2}'`
        if [ -n "$pid" ]
        then
                echo "Stopping NIS password daemon (dsyppasswdd)..."
                kill -TERM ${pid}
        fi
}

stop_dsypxfrd() {
 
        if [ -f ${DSYPXFRDPID} ]; then
                pid=`cat ${DSYPXFRDPID}`
                /bin/rm -f ${DSYPXFRDPID}
                count=`/bin/ps -e| /bin/awk '{print $1}' | grep -c ${pid}`
                if [ ${count} -eq "1" ]; then
                        echo "Stopping NIS replication daemon (dsypxfrd)..."
                        kill -TERM ${pid}
                fi
        else
                pid=`ps -fe | grep lib/dsypxfrd | grep -v grep|awk '{print $2}'`
                if [ -n "$pid" ]
                then
                        echo "Stopping NIS replication daemon (dsypxfrd)..."
                        kill -TERM ${pid}
                fi
        fi
}


stop_nis() {
  if grep "^frontend nis" /etc/opt/SUNWconn/ldap/current/dsserv.conf >/dev/null 2>&1
	then
		stop_dsypxfrd
		stop_dsyppasswdd
		stop_dsyprsvd
	fi
}

#
# set the global variables as set by the admin tool
#
if [ -f /etc/opt/SUNWconn/ldap/current/dsserv.ini ]
then
	. /etc/opt/SUNWconn/ldap/current/dsserv.ini
fi

logfile=`cat /etc/opt/SUNWconn/ldap/current/dsserv.conf|grep "^logfile" | awk '{print $2}'`
if [ "$logfile" = "" ]
then
	logfile=/var/opt/SUNWconn/ldap/log/dsserv.log
fi


logdir=`dirname $logfile`

case "$1" in
'start')

	if [ -n "$LdapPort" ]
	then
		LdapPort="-p $LdapPort"
	fi
	pid=`ps -fe | grep lib/dsservd | grep -v grep|awk '{print $2}'`
	if [ -n "$pid" ]
	then
		echo "Another SunDS daemon is already running"
		exit 1
	fi
	echo "Starting SunDS daemon \c"
	cd $logdir
	test -f $logfile.8 && mv $logfile.8  $logfile.9
	test -f $logfile.7 && mv $logfile.7  $logfile.8
	test -f $logfile.6 && mv $logfile.6  $logfile.7
	test -f $logfile.5 && mv $logfile.5  $logfile.6
	test -f $logfile.4 && mv $logfile.4  $logfile.5
	test -f $logfile.3 && mv $logfile.3  $logfile.4
	test -f $logfile.2 && mv $logfile.2  $logfile.3
	test -f $logfile.1 && mv $logfile.1  $logfile.2
	test -f $logfile   && mv $logfile    $logfile.1
	touch $logfile
	#
	# Start dsserv 
	#
	$BASEDIR/SUNWconn/ldap/lib/dsservd $LdapPort >/dev/null 2>&1
	until [ -s $logfile ]
	do
		echo ".\c"
		pid=`ps -fe | grep lib/dsservd | grep -v grep|awk '{print $2}'`
		if [ -z "$pid" ]
		then
			echo "SunDS daemon not started"
			cat $logfile
			exit 1
		fi
		sleep 1
	done
	echo
	cat $logfile
	#
	# dspushd daemon if needed to be started
	#

	pushlog=$logdir"/dspushd.log"

	if [ "$StartDspushd" = "true" ]
	then
		cd $logdir
		test -f $pushlog.8 && mv $pushlog.8  $pushlog.9
		test -f $pushlog.7 && mv $pushlog.7  $pushlog.8
		test -f $pushlog.6 && mv $pushlog.6  $pushlog.7
		test -f $pushlog.5 && mv $pushlog.5  $pushlog.6
		test -f $pushlog.4 && mv $pushlog.4  $pushlog.5
		test -f $pushlog.3 && mv $pushlog.3  $pushlog.4
		test -f $pushlog.2 && mv $pushlog.2  $pushlog.3
		test -f $pushlog.1 && mv $pushlog.1  $pushlog.2
		test -f $pushlog   && mv $pushlog    $pushlog.1
		echo "Starting dspushd daemon"
		$BASEDIR/SUNWconn/ldap/lib/dspushd
	fi
	#
	# NIS daemons
	#
	# *** Need to add a check to make sure NIS is enabled ***
	start_nis
	;;   

'stop')

	pidserv=`ps -fe | grep lib/dsservd | grep -v grep|awk '{print $2}'`
	if [ -n "$pidserv" ]
	then
		echo "Stopping SunDS daemon"
		kill -TERM $pidserv
		rm -f /var/opt/SUNWconn/ldap/log/dsserv.pid
	fi

	#
	# Stop the NIS deamons
	#

	stop_nis

	#
	# stop the dspushd daemon
	#
	pidpush=`ps -fe | grep lib/dspushd | grep -v grep|awk '{print $2}'`
	if [ -n "$pidpush" ]
	then
		kill -TERM $pidpush
		echo "Stopping dspushd daemon"
	fi
	#
	#  check that processes stopped
	#
	if [ -z "$pidserv" -a -z "$pidpush" ]
	then
		exit 0
	fi
	for x in 1 2 3 4 5 6 7 8 9 0 11 12 13 14 15 16 17 18 19 20
	do
		pidserv2=`ps -fe | grep lib/dsservd | grep -v grep|awk '{print $2}'`
		pidpush2=`ps -fe | grep lib/dspushd | grep -v grep|awk '{print $2}'`
		if [ -n "$pidserv2" -o "$pidpush2" ]
		then
			echo ".\c"
			sleep 1
		else
			break
		fi
	done

	if [ -n "$pidserv2" ]
	then
		echo "SunDS stopped failed"
		ExitCode=1
	else
		echo "SunDS daemon stopped"
	fi

	if [ -n "$pidpush" ]
	then
	if [ -n "$pidpush2" ]
	then
		kill -9 $pidpush2
		echo "dspushd daemon stopped"
	else
		echo "dspushd daemon stopped"
	fi
	fi

	rm -f /var/opt/SUNWconn/ldap/log/dsserv.pid

        if [ -f /var/opt/SUNWconn/ldap/log/dsradius.pid ]
        then
                rm -f /var/opt/SUNWconn/ldap/log/dsradius.pid
        fi
	;;


'restart')
 
        pidserv=`ps -fe | grep lib/dsservd | grep -v grep|awk '{print $2}'`
        if [ -n "$pidserv" ]
        then
                echo "Restarting dsservd..."
                stop_nis
                kill -HUP $pidserv

                if [ "$StartDspushd" = "true" ]
                then
                        pidpush=`ps -fe | grep lib/dspushd | grep -v grep|awk '{print $2}'`
                        if [ -n "$pidpush" ]
                        then
                                sleep 2
                                pid=`ps -fe | grep lib/dspushd | grep -v grep|awk '{print $2}'`
                                if [ -n "$pid" ]
                                then
                                        kill -9 $pid
                                        echo "dspushd daemon killed"
                                else
                                        echo "dspushd daemon stopped"
                                fi
                        fi
 
                	pushlog=$logdir"/dspushd.log"
 
                        cd $logdir
                        test -f $pushlog.8 && mv $pushlog.8  $pushlog.9
                        test -f $pushlog.7 && mv $pushlog.7  $pushlog.8
                        test -f $pushlog.6 && mv $pushlog.6  $pushlog.7
                        test -f $pushlog.5 && mv $pushlog.5  $pushlog.6
                        test -f $pushlog.4 && mv $pushlog.4  $pushlog.5
                        test -f $pushlog.3 && mv $pushlog.3  $pushlog.4
                        test -f $pushlog.2 && mv $pushlog.2  $pushlog.3
                        test -f $pushlog.1 && mv $pushlog.1  $pushlog.2
                        test -f $pushlog   && mv $pushlog    $pushlog.1
                        echo "Starting dspushd daemon"
                        $BASEDIR/SUNWconn/ldap/lib/dspushd
                fi

                start_nis	
        fi
        ;;

 
*)
	echo ${USAGE}


	exit 1
	;;
esac

exit $ExitCode
