#!/bin/sh
#
# ident "@(#)utds.sh	1.70 04/09/16 SMI"
#
# Copyright 1996-1999,2001-2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#
# Start/Stop Sun Ray Data Store daemon
#
###########################################
# chkconfig header info for RH
#
# chkconfig: 5 30 30
# description: Sun Ray Datastore services
#
###########################################
# insserv header info for JDS/SuSE
### BEGIN INIT INFO
# Provides:			utds
# Required-Start:		$network utsyscfg
# X-UnitedLinux-Should-Start:
# Required-Stop:
# X-UnitedLinux-Should-Stop:
# Default-Start:		5
# Default-Stop:			1 2 3 4
# Short-Description:
# Description:			Sun Ray Datastore services
### END INIT INFO


# defines the option for ps command
OS=`uname -s`
if [ "$OS" = "SunOS" ]
then
	PSOPTS="-o ppid,pid,comm"
else
	PSOPTS="-o ppid,pid,cmd"
fi

#
# it finds the processes ID for the spcified program on the server
# Returns: pid
#
findProc() {
	ps -e $PSOPTS | grep "$1" | awk '$1 == 1 {print $2}'
}

trap 'echo "$0: Interrupted "; exit 1' INT QUIT

USAGE="Usage: `basename $0` { start | stop | restart }"

# relying the basedir created in /etc/opt/SUNWut to locate utprodinfo
UTPRODINFO=/etc/opt/SUNWut/basedir/lib/utprodinfo
if [ -x $UTPRODINFO ]
then
	BASEDIR=`$UTPRODINFO -p SUNWutdso BASEDIR 2>/dev/null`
fi

if [ -z "$BASEDIR" ]	# pkg not installed
then
	BASEDIR=/opt	# set defaul. Maybe the dir is nfs mounted
fi
PATH=/sbin:/bin:/usr/sbin:/usr/bin:$BASEDIR/SUNWut/srds/lib:$BASEDIR/SUNWut/srds/sbin:$BASEDIR/SUNWut/srds/bin
LD_LIBRARY_PATH=$BASEDIR/SUNWut/srds/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH
UTPUSHD=$BASEDIR/SUNWut/srds/lib/utpushd
UTDSD=$BASEDIR/SUNWut/srds/lib/utdsd


#
# Set NLSPATH for I18N
#
# NLSPATH=${NLSPATH}:${BASEDIR}/SUNWut/srds/locale/%L/%N.cat
# export NSLPATH

ExitCode=0

#
# check if SRDS is configured
#
if [ ! -f /etc/opt/SUNWut/srds/current/utdsd.run ]
then
	exit 0
fi

#
# set the global variables as set by the admin tool
#
if [ -f /etc/opt/SUNWut/srds/current/utdsd.ini ]
then
	. /etc/opt/SUNWut/srds/current/utdsd.ini
fi

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


logdir=`dirname $logfile`

case "$1" in
'start')

	if [ -n "$LdapPort" ]
	then
		LdapPort="-p $LdapPort"
	fi
	pid=`findProc $UTDSD`
	if [ -n "$pid" ]
	then
		echo "Another Sun Ray Data Store daemon is already running"
		exit 1
	fi
	printf "Starting Sun Ray Data Store daemon "
	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 utdsd 
	#
	$UTDSD $LdapPort >/dev/null 2>&1
	until [ -s $logfile ]
	do
		printf "."
		pid=`findProc $UTDSD`
		if [ -z "$pid" ]
		then
			echo "Sun Ray Data Store daemon not started"
			cat $logfile
			exit 1
		fi
		sleep 1
	done
	echo
	cat $logfile
	#
	# utpushd daemon if needed to be started
	#

	pushlog=$logdir"/utpushd.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 utpushd daemon"
		$UTPUSHD
	fi
	;;   

'stop')

	pidserv=`findProc $UTDSD`
	if [ -n "$pidserv" ]
	then
		echo "Stopping Sun Ray Data Store daemon"
		kill -TERM $pidserv 2> /dev/null
		rm -f /var/opt/SUNWut/srds/log/utdsd.pid
	fi

	#
	# stop the utpushd daemon
	#
	pidpush=`findProc $UTPUSHD`
	if [ -n "$pidpush" ]
	then
		kill -TERM $pidpush 2> /dev/null
		echo "Stopping utpushd 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=`findProc $UTDSD`
		pidpush2=`findProc $UTPUSHD`
		if [ -n "$pidserv2" -o -n "$pidpush2" ]
		then
			printf "."
			sleep 1
		else
			break
		fi
	done

	if [ -n "$pidserv2" ]
	then
		echo "Failed to stop the Sun Ray Data Store daemon"
		ExitCode=1
	else
		echo "Sun Ray Data Store daemon stopped"
	fi

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


'restart')
 
        pidserv=`findProc $UTDSD`
        if [ -n "$pidserv" ]
        then
                echo "Restarting utdsd..."
                kill -HUP $pidserv

                if [ "$StartDspushd" = "true" ]
                then
                        pidpush=`findProc $UTPUSHD`
                        if [ -n "$pidpush" ]
                        then
                                sleep 2
                                pid=`findProc $UTPUSHD`
                                if [ -n "$pid" ]
                                then
                                        kill -TERM $pid 2> /dev/null
                                        echo "utpushd daemon killed"
                                else
                                        echo "utpushd daemon stopped"
                                fi
                        fi
 
                	pushlog=$logdir"/utpushd.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 utpushd daemon"
                        $UTPUSHD
                fi

        fi
        ;;

 
*)
	echo ${USAGE}


	exit 1
	;;
esac

exit $ExitCode
