#!/bin/bash

set -e -E -u -o pipefail -o noclobber -o noglob +o braceexpand || exit 1
trap 'printf "[ee] failed: %s\n" "${BASH_COMMAND}" >&2' ERR || exit 1

if test "$( getent passwd -- mos-services | cut -f 3 -d : || printf '%s' "${UID}" )" -ne "${UID}" ; then
	exec sudo -u mos-services -g mos-services -E -n -- "${0}" "${@}"
	exit 1
fi

if ! test "${#}" -eq 0 ; then
	printf '[ii] invalid arguments; aborting!\n' >&2
	exit 1
fi

umask 0027

exec </dev/null >&2

_variable_defaults=(
		
		# NOTE:  Add other definitions that could come from the packaging descriptor, and are needed at runtime.
		#   It enables hard-coding some definitions, which could later be overriden with environment variables.
		
		_TOWER4CLOUDS_MANAGER_DISTRIBUTION='/opt/modaclouds-services-tower4clouds-manager-0.7.13/lib/distribution'
		
		_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_PORT=8170
		_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_PORT=8175
		_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_PORT=31337
		_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_IP=127.0.0.1

		_JAVA_HOME='/opt/mosaic-rt-jre-7'
		_PATH="/opt/mosaic-rt-jre-7/bin:/usr/bin:/bin"
		_TMPDIR='/tmp/modaclouds/tower4clouds-manager'
)
declare "${_variable_defaults[@]}"


# NOTE:  Don't touch, it enables running multiple instances of the same service at the same time.
_identifier="${modaclouds_service_identifier:-0000000000000000000000000000000000000000}"

# NOTE:  Don't touch, it tries to "auto-discover" the proper temporary folder.
if test -n "${modaclouds_service_temporary:-}" ; then
	_TMPDIR="${modaclouds_service_temporary:-}"
elif test -n "${modaclouds_temporary:-}" ; then
	_TMPDIR="${modaclouds_temporary}/services/${_identifier}"
else
	_TMPDIR="${_TMPDIR}/${_identifier}"
fi


_variable_overrides=(
		
		# NOTE:  Add the same definitions as in the previous case, but keep the pattern as bellow.
		#   It enables overriding some definitions with environment variables, or fallbacks to the hard-coded ones.
		
		_TOWER4CLOUDS_TOWER4CLOUDS_MANAGER_DISTRIBUTION="${MODACLOUDS_TOWER4CLOUDS_MANAGER_DISTRIBUTION:-${_TOWER4CLOUDS_MANAGER_DISTRIBUTION}}"
		
		_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_PORT="${MODACLOUDS_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_PORT:-${_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_PORT}}"
		_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_IP="${MODACLOUDS_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_IP}"
		_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_PORT="${MODACLOUDS_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_PORT:-${_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_PORT}}"
		_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_IP="${MODACLOUDS_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_IP}"
		_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_PORT="${MODACLOUDS_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_PORT:-${_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_PORT}}"
		_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_IP="${MODACLOUDS_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_IP:-${_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_IP}}"

		_JAVA_HOME="${MODACLOUDS_TOWER4CLOUDS_MANAGER_JAVA_HOME:-${_JAVA_HOME}}"
		_PATH="${MODACLOUDS_TOWER4CLOUDS_MANAGER_PATH:-${_PATH}}"
		_TMPDIR="${MODACLOUDS_TOWER4CLOUDS_MANAGER_TMPDIR:-${_TMPDIR}}"
)
declare "${_variable_overrides[@]}"


export PATH="${_PATH}"


if test ! -e "${_TMPDIR}" ; then
	mkdir -p -- "${_TMPDIR}"
	mkdir -- "${_TMPDIR}/tmp"
	mkdir -- "${_TMPDIR}/home"
fi


# NOTE:  Don't touch, it tries to ensure that only one instance (with the same identifier) of the same service is running.
exec {_lock}<"${_TMPDIR}"
if ! flock -x -n "${_lock}" ; then
	printf '[ee] failed to acquire lock; aborting!\n' >&2
	exit 1
fi


# NOTE:  Don't touch, it changes the current working directory to an empty one, specific to the service instance.
if test -d "${_TMPDIR}/cwd" ; then
	chmod -R u+w -- "${_TMPDIR}/cwd"
	rm -R -- "${_TMPDIR}/cwd"
fi
mkdir -- "${_TMPDIR}/cwd"

cd -- "${_TMPDIR}/cwd"


_environment=(
		PATH="${_PATH}"
		TMPDIR="${_TMPDIR}/tmp"
		HOME="${_TMPDIR}/home"
		USER='modaclouds-services'
)


_environment+=(
		
		# NOTE:  Add here the environment variables you want to pass to the actual process.
		#   (I.e. just defining them in the previous sections doesn't export them.)
		
		# NOTE:  No other variables, except the ones present in the `_environment` array are exported to the service.
		#   (I.e. the service starts with a clean environment.)
		
		MODACLOUDS_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_IP="${_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_IP}"
		MODACLOUDS_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_PORT="${_TOWER4CLOUDS_DATA_ANALYZER_PUBLIC_ENDPOINT_PORT}"
		MODACLOUDS_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_PORT="${_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_PORT}"
		MODACLOUDS_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_IP="${_TOWER4CLOUDS_MANAGER_PUBLIC_ENDPOINT_IP}"
		MODACLOUDS_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_PORT="${_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_PORT}"
		MODACLOUDS_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_IP="${_TOWER4CLOUDS_RDF_HISTORY_DB_ENDPOINT_IP}"
)


printf '[--]\n' >&2
printf '[ii] parameters:\n' >&2
printf '[ii]   * environment:\n' >&2
for _variable in "${_environment[@]}" ; do
	printf '[ii]       * `%s`;\n' "${_variable}" >&2
done
printf '[ii]   * working directory: `%s`;\n' "${PWD}" >&2
printf '[--]\n' >&2

printf '[ii] starting service...\n' >&2
printf '[--]\n' >&2


# NOTE:  Actually execute the service.
exec \
	env \
			-i "${_environment[@]}" \
	"${_JAVA_HOME}/bin/java" \
			-jar "${_TOWER4CLOUDS_MANAGER_DISTRIBUTION}/manager-server-0.2.1.jar"


exit 1