#!/bin/sh
#
# ident "@(#)utds.sh	1.75	12/06/05 Oracle"
#
# Copyright (c) 1996, 2012, Oracle and/or its affiliates. All rights reserved.
#
# 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() {
	echo "Usage: $0 {start|stop|restart|status}"
}

# BackOffCmd timeout cmd
#
# Run a command until it succeeds or a specified time has elapsed
# Use exponential backoff between successive executions to avoid thrashing machine
#
# command output will be discarded, including stderr
# return status of final command execution
BackOffCmd () {
	timeout=$1
	seconds=1
	halftimeout=`expr $timeout / 2`
	moretodo=true

	while $moretodo; do
		if $2 >/dev/null 2>&1 </dev/null; then
			if [ $seconds != 1 ]; then
				echo ""
			fi
			return 0
		fi
		printf "."
		if [ $seconds -gt $halftimeout ]; then
			moretodo=false
			seconds=`expr $timeout - $seconds + 1`
		fi
		sleep $seconds
		seconds=`expr $seconds \* 2`
	done

	echo "."
	$2 >/dev/null 2>&1 </dev/null
}

IsDSStopped () {
	[ -z "`findProc $UTDSD`" ]
}

IsPushdStopped () {
	[ -z "`findProc $UTPUSHD`" ]
}

IsDSAndPushdStopped () {
	IsDSStopped && IsPushdStopped
}

WaitForDSStop () {
	timeout=${1:-45}
	BackOffCmd "$timeout" IsDSStopped
}
	
WaitForDSAndPushdStop () {
	timeout=${1:-45}
	BackOffCmd "$timeout" IsDSAndPushdStopped
}
	
BASEDIR=/etc/opt/SUNWut/basedir

PATH=/sbin:/bin:/usr/sbin:/usr/bin:$BASEDIR/srds/lib:$BASEDIR/srds/sbin
LD_LIBRARY_PATH=$BASEDIR/srds/lib:$LD_LIBRARY_PATH
export PATH LD_LIBRARY_PATH
UTPUSHD=$BASEDIR/srds/lib/utpushd
UTDSD=$BASEDIR/srds/lib/utdsd


#
# Set NLSPATH for I18N
#
# NLSPATH=${NLSPATH}:${BASEDIR}/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`

if [ $# -ne 1 ] ; then
	usage 1>&2
	exit 1
fi

case "$1" in
'start')
	if [ ! -f /etc/opt/SUNWut/utenabled.utdsd ]; then
		exit 0
	fi

	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
	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
		$UTPUSHD
	fi
	;;   

'stop')

	pidserv=`findProc $UTDSD`
	if [ -n "$pidserv" ]
	then
		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
	fi
	#
	#  check that processes stopped
	#
	if [ -z "$pidserv" -a -z "$pidpush" ]
	then
		exit 0
	fi

	# Wait a while to see if it eventually exits
	if WaitForDSAndPushdStop
	then
		:
	else
		if [ -n "`findProc $UTDSD`" ]
		then
			echo "Failed to stop the Sun Ray Data Store processes"
			ExitCode=1
		fi

		if [ -n "$pidpush" ]
		then
			pidpush2=`findProc $UTPUSHD`
			if [ -n "$pidpush2" ]
			then
				kill -9 $pidpush2
			fi
		fi
	fi
	;;


'restart')
	if [ ! -f /etc/opt/SUNWut/utenabled.utdsd ]; then
		exit 0
	fi

        pidserv=`findProc $UTDSD`
        if [ -n "$pidserv" ]
        then
                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
				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
			$UTPUSHD
		fi
	fi
	# XXX I believe "restart" semantics require a process to be
	# started, if none is running.  I don't believe this code is
	# exercised.
	;;

'status')
	exit 0
	;;
 
*)
	usage 1>&2
	exit 1
	;;
esac

exit $ExitCode
