#!/bin/ksh93
#
# $Id: stop-ds 553 2013-04-13 17:41:34Z elkner $
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at
# trunk/opends/resource/legal-notices/OpenDS.LICENSE
# or https://OpenDS.dev.java.net/OpenDS.LICENSE.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at
# trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
# add the following below this CDDL HEADER, with the fields enclosed
# by brackets "[]" replaced with your own identifying information:
#      Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
#      Copyright 2006-2009 Sun Microsystems, Inc.
#      Portions Copyright 2011 ForgeRock AS
#      Portions Copyright 2013 Jens Elkner

# This script may be used to request that the Directory Server shut down.
# It operates in two different ways, depending on how it is invoked.  If it
# is invoked without any arguments and a local PID file is available, then it
# will stop the server by sending a TERM signal to the process, and this
# script will wait until the server has stopped before exiting.  If any
# arguments were provided or there is no local PID file, then it will attempt
# to stop the server using an LDAP request.

INSTALL_ROOT="${.sh.file%/*/*}" SCRIPT_NAME='stop-ds'

# We keep this values to reset the environment before calling start-ds.
ORIGINAL_JAVA_ARGS=${OPENDJ_JAVA_ARGS}
ORIGINAL_JAVA_HOME=${OPENDJ_JAVA_HOME}
ORIGINAL_JAVA_BIN=${OPENDJ_JAVA_BIN}

# Set environment variables
. "${INSTALL_ROOT}"/lib/_script-util.sh
checkEnv -i 'set-full-environment-and-test-java' "$@"

if ! isVersionOrHelp "$@" ; then
	integer RESTART=0
	for ARG in "$@" ; do
		[[ ${ARG} == '-R' || ${ARG} == '--restart' ]] && RESTART=1 && break
	done
	# If restart is requested, check whether the service is not disabled. If so,
	# use svcadm to restart, otherwise fall through.
	if (( ${RESTART} )) && [[ -n "${SMF_FMRI}" && ${ uname -s ; } == 'SunOS' ]]
	then
		STATE=${ svcs -H -o state ${SMF_FMRI} 2>/dev/null ; } 
		if [[ -n ${STATE} && ${STATE} != 'disabled' ]]; then
			TARG=${ svcprop -p general_ovr/enabled ${SMF_FMRI} 2>/dev/null ; }
			[[ ${TARG} == 'true' ]] && TARG='-t' || TARG=''
			print -u2 "Restarting service '${SMF_FMRI}' ..."
			/usr/sbin/svcadm disable -s ${SMF_FMRI} || exit $?
			/usr/sbin/svcadm enable -s ${TARG} ${SMF_FMRI}
			RC=$?
			(( ${RC} != 0 )) && print -u2 'Done.' || print -u2 'Failed.'
			exit ${RC}
		fi
	fi
fi

"${OPENDJ_JAVA_BIN}" ${OPENDJ_JAVA_ARGS} ${SCRIPT_NAME_ARG} \
	org.opends.server.tools.StopDS --checkStoppability "$@"
RC=$?

STOPPED=1
EXIT_CODE=1
MUST_START_USING_SYSTEM_CALL=1
MUST_STOP_USING_SYSTEM_CALL=1
QUIET_MODE=1

if (( ${RC} == 98 )); then
	# Already stopped and nothing else to do.
	STOPPED=0
elif (( ${RC} == 99 || ${RC} == 105 )); then
	# Already stopped and must start locally
	STOPPED=0
	MUST_START_USING_SYSTEM_CALL=0
	(( ${RC} == 105 )) && QUIET_MODE=0
elif (( ${RC} == 100 )); then
	# Stop using system call
	MUST_STOP_USING_SYSTEM_CALL=0
elif (( ${RC} == 101 || ${RC} == 106 )); then
	# Restart using system call
	MUST_STOP_USING_SYSTEM_CALL=0
	MUST_START_USING_SYSTEM_CALL=0
	(( ${RC} == 106 )) && QUIET_MODE=0
elif (( ${RC} != 102 )); then
	# If != stop using protocol
	exit ${RC}
fi

if (( ${MUST_STOP_USING_SYSTEM_CALL} == 0 )); then
	if [[ ! -f ${INSTANCE_ROOT}/logs/server.pid ]]; then
		print -u2 'ERROR:  Unable to find the server.pid file to determine' \
			'the process ID of the OpenDJ process to terminate.'
		exit 1
	fi
	PID=$(< "${INSTANCE_ROOT}"/logs/server.pid)
	kill $PID
	if (( $? == 0 )) ; then
		"${OPENDJ_JAVA_BIN}" \
			-client org.opends.server.tools.WaitForFileDelete \
			--targetFile "${INSTANCE_ROOT}"/logs/server.pid \
			--logFile "${INSTANCE_ROOT}"/logs/errors
		(( $? == 0 )) && STOPPED=0
	fi
fi

# Delete the pid file if the server is stopped (this can occur if the process
# has been killed using kill -9).
(( ${STOPPED} == 0 )) && [[ -f ${INSTANCE_ROOT}/logs/server.pid ]] && \
	rm "${INSTANCE_ROOT}"/logs/server.pid

# Now if the user wants to restart the server, try to restart it if the server
# is stopped.
if (( ${MUST_START_USING_SYSTEM_CALL} == 0 && ${STOPPED} == 0 )); then
	# Set the original values that the user had on the environment in order
	# to be sure that the start-ds script works with the proper arguments
	# (in particular if the user specified not to overwrite the environment)
	OPENDJ_JAVA_ARGS=${ORIGINAL_JAVA_ARGS}
	OPENDJ_JAVA_HOME=${ORIGINAL_JAVA_HOME}
	OPENDJ_JAVA_BIN=${ORIGINAL_JAVA_BIN}
	(( ${QUIET_MODE} == 0 )) && QUIET='--quiet' || QUIET=''
	"${INSTALL_ROOT}"/bin/start-ds ${QUIET}
	exit $?
fi

# The user does not want to start the server locally and it is already stopped.
# Just exit.
(( ${STOPPED} == 0 )) && exit 0

# If we've gotten here, then we should try to stop the server over LDAP.
"${OPENDJ_JAVA_BIN}" ${OPENDJ_JAVA_ARGS} ${SCRIPT_NAME_ARG} \
	-Dorg.opends.server.InstallRoot="${INSTALL_ROOT}" \
    -Dorg.opends.server.InstanceRoot="${INSTANCE_ROOT}" \
	org.opends.server.tools.StopDS "$@"
