#!/usr/bin/env bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

if [ -z "${CELEBORN_HOME}" ]; then
  export CELEBORN_HOME="$(cd "`dirname "$0"`"/..; pwd)"
fi

export CELEBORN_CONF_DIR="${CELEBORN_CONF_DIR:-"${CELEBORN_HOME}/conf"}"

# print launch command for debug
export CELEBORN_PRINT_LAUNCH_COMMAND="0"

if [ -z "$CELEBORN_ENV_LOADED" ]; then
  export CELEBORN_ENV_LOADED=1

  if [ -f "${CELEBORN_CONF_DIR}/celeborn-env.sh" ]; then
    # Promote all variable declarations to environment (exported) variables
    set -a
    . "${CELEBORN_CONF_DIR}/celeborn-env.sh"
    set +a
  fi
fi

# Find CELEBORN jars.

for i in "$@"
do
  if [ "$i" == "org.apache.celeborn.service.deploy.master.Master" ] ; then
    LAUNCH_CLASS=org.apache.celeborn.service.deploy.master.Master
  fi
  if [ "$i" == "org.apache.celeborn.service.deploy.worker.Worker" ] ; then
    LAUNCH_CLASS=org.apache.celeborn.service.deploy.worker.Worker
  fi
done

if [ "${LAUNCH_CLASS}" == "org.apache.celeborn.service.deploy.master.Master" ] ; then
  if [ -d "${CELEBORN_HOME}/master-jars" ]; then
    CELEBORN_JARS_DIR="${CELEBORN_HOME}/master-jars"
  else
    CELEBORN_JARS_DIR="${CELEBORN_HOME}/master/target"
  fi
fi

if [ "${LAUNCH_CLASS}" == "org.apache.celeborn.service.deploy.worker.Worker" ] ; then
  if [ -d "${CELEBORN_HOME}/worker-jars" ]; then
    CELEBORN_JARS_DIR="${CELEBORN_HOME}/worker-jars"
  else
    CELEBORN_JARS_DIR="${CELEBORN_HOME}/worker/target"
  fi
fi

if [ ! -d "$CELEBORN_JARS_DIR" ]; then
  echo "Failed to find CELEBORN jars directory ($CELEBORN_JARS_DIR)." 1>&2
  echo "You need to build CELEBORN with the target \"package\" before running this program." 1>&2
  exit 1
else
  CELEBORN_CLASSPATH="$CELEBORN_CONF_DIR:$HADOOP_CONF_DIR:$CELEBORN_JARS_DIR/*:$JAVA_TOOLS_JAR"
fi

# Turn off posix mode since it does not allow process substitution
set +o posix
CMD=()
CMD+=("$JAVA")
CMD=(${CMD[@]} "-XX:+IgnoreUnrecognizedVMOptions" "$CELEBORN_JAVA_OPTS")
CMD+=("-cp")
CMD+=("$CELEBORN_CLASSPATH")
CMD=(${CMD[@]} "$@")

COUNT=${#CMD[@]}

if [ "${CELEBORN_PRINT_LAUNCH_COMMAND}" = "1" ]; then
  echo "Start to launch ${CMD[@]}"
fi

exec "${CMD[@]}"
