#!/bin/ksh93
#
# $Id: start-ds 560 2013-05-22 07:36: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

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

# Set environment variables
CHECK_VERSION='yes'
. "${INSTALL_ROOT}"/lib/_script-util.sh
# would use -i, but -s is also just version info but is not a common switch
checkEnv 'set-full-environment-and-test-java'

integer QUIET=0 DETACH=1 INFO=0
typeset -a DJARGS=( )
if isVersionOrHelp "$@" ; then
	INFO=1
else
	if [[ ${ LC_ALL= LC_MESSAGES=C tty ; } == 'not a tty' ]]; then
		# remove no detach arg and assume, it is started by SMF
		for ARG in "$@" ; do
			[[ ${ARG} == '-N' || ${ARG} == '--nodetach' ]] && continue
			[[ ${ARG} == '-Q' || ${ARG} == '--quiet' ]] && QUIET=1
			[[ ${ARG} == '-F' || ${ARG} == '--fullVersion' ]] && INFO=1
			[[ ${ARG} == '-s' || ${ARG} == '--systemInfo' ]] && INFO=1
			DJARGS+=( "${ARG}" )
		done
	else
		for ARG in "$@" ; do
			[[ ${ARG} == '-N' || ${ARG} == '--nodetach' ]] && DETACH=0
			[[ ${ARG} == '-Q' || ${ARG} == '--quiet' ]] && QUIET=1
			[[ ${ARG} == '-F' || ${ARG} == '--fullVersion' ]] && INFO=1
			[[ ${ARG} == '-s' || ${ARG} == '--systemInfo' ]] && INFO=1
			DJARGS+=( "${ARG}" )
		done
		# issue a warning, if the service is running and one tries to start it
		# manually as well - let the app do the rest
		if (( ! INFO )) && [[ ${ uname -s ; } == 'SunOS' ]]; then
			STATE=''
			[[ -n ${SMF_FMRI} ]] && \
				STATE=${ svcs -H -o state ${SMF_FMRI} 2>/dev/null; }
			if [[ -n ${STATE} && ${STATE} != 'disabled' ]]; then
				print -u2 "WARNING: The service '${SMF_FMRI}' is not disabled!"
			fi
		fi
	fi
fi

# Specify the locations of important files that may be used when the server
# is starting.
CONFIG_FILE="${INSTANCE_ROOT}"/config/config.ldif
PID_FILE="${INSTANCE_ROOT}"/logs/server.pid
LOG_FILE="${INSTANCE_ROOT}"/logs/server.out
STARTING_FILE="${INSTANCE_ROOT}"/logs/server.starting

if (( ${INFO} )); then
	"${OPENDJ_JAVA_BIN}" -client ${SCRIPT_NAME_ARG} \
		-Dorg.opends.server.InstallRoot="${INSTALL_ROOT}" \
		-Dorg.opends.server.InstanceRoot="${INSTANCE_ROOT}" \
		org.opends.server.core.DirectoryServer \
		--configClass org.opends.server.extensions.ConfigFileHandler \
		--configFile "${CONFIG_FILE}" "$@"
	exit $?
fi
checkInstanceDir

# We do not check for startability, since for non-Windows it just returns
# based on the passed arguments:
# 99 => 'start as detached'
# 100 => 'start as non-detached'
# 103 => 'start as detached quiet'
# 104 => 'start as non-detached quiet'
# what we already know by parsing the args above. If an arg or instance problem
# exists, exit 999 from checkEnv or checkInstance would prevent getting here.
# So doing a --checkStartability would not by us anything ...

if (( ${DETACH} )); then
	touch "${STARTING_FILE}"
	nohup "${OPENDJ_JAVA_BIN}" ${OPENDJ_JAVA_ARGS} ${SCRIPT_NAME_ARG} \
		-Dorg.opends.server.InstallRoot="${INSTALL_ROOT}" \
		-Dorg.opends.server.InstanceRoot="${INSTANCE_ROOT}" \
		org.opends.server.core.DirectoryServer \
		--configClass org.opends.server.extensions.ConfigFileHandler \
		--configFile "${CONFIG_FILE}" "${DJARGS[@]}" > "${LOG_FILE}" 2>&1 &
	print $! > "${PID_FILE}"

	# wait until server startup has finished: removes ${STARTING_FILE} when done
	if (( ${QUIET} )); then
		"${OPENDJ_JAVA_BIN}" -client org.opends.server.tools.WaitForFileDelete \
			--targetFile "${STARTING_FILE}" \
			"${DJARGS[@]}"
	else
		"${OPENDJ_JAVA_BIN}" -client org.opends.server.tools.WaitForFileDelete \
			--targetFile "${STARTING_FILE}" --logFile "${LOG_FILE}" \
			"${DJARGS[@]}"
	fi
	RC=$?
	# 1 => 'internal error', '2' => 'timeout'
	(( ${RC} != 0 )) && exit ${RC}

	# now check, whether the server is still running
	"${OPENDJ_JAVA_BIN}" -client ${SCRIPT_NAME_ARG} \
		-Dorg.opends.server.InstallRoot="${INSTALL_ROOT}" \
		-Dorg.opends.server.InstanceRoot="${INSTANCE_ROOT}" \
		org.opends.server.core.DirectoryServer \
		--configClass org.opends.server.extensions.ConfigFileHandler \
		--configFile "${CONFIG_FILE}" --checkStartability > /dev/null 2>&1
	# 98 => 'already started', otherwise server doesn't run
	(( $? == 98 )) && exit 0 || exit 1
fi

# non-detach
if (( ${QUIET} )); then
	print $$ > "${PID_FILE}"
	rm -f "${LOG_FILE}"
	exec "${OPENDJ_JAVA_BIN}" ${OPENDJ_JAVA_ARGS} ${SCRIPT_NAME_ARG} \
		-Dorg.opends.server.InstallRoot="${INSTALL_ROOT}" \
		-Dorg.opends.server.InstanceRoot="${INSTANCE_ROOT}" \
		org.opends.server.core.DirectoryServer \
		--configClass org.opends.server.extensions.ConfigFileHandler \
		--configFile "${CONFIG_FILE}" "${DJARGS[@]}" > /dev/null
fi

print $$ > "${PID_FILE}"
rm -f "${LOG_FILE}"
exec "${OPENDJ_JAVA_BIN}" ${OPENDJ_JAVA_ARGS} ${SCRIPT_NAME_ARG} \
	-Dorg.opends.server.InstallRoot="${INSTALL_ROOT}" \
	-Dorg.opends.server.InstanceRoot="${INSTANCE_ROOT}" \
	org.opends.server.core.DirectoryServer \
	--configClass org.opends.server.extensions.ConfigFileHandler \
	--configFile "${CONFIG_FILE}" "${DJARGS[@]}"
