#!/bin/bash

#
# Copyright (c) 2023-2026, Cloudera, Inc. All Rights Reserved.
#
# Licensed 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.
#

SCRIPT_PATH="$(
  cd "$(dirname "$0")" >/dev/null 2>&1 || exit
  pwd -P
)"

for i in "$@"; do
  case $i in
  -h | --help)
    HELP=true
    ;;
  esac
done

__usage="
 Usage: $(basename "$0") [options]
 Options:
    -h, --help: Print this help
 "

if [ "${HELP}" = true ]; then
  echo "$__usage"
  exit 0
fi

if [ -z ${DOCKER_MODE} ]; then
  export DOCKER_MODE=true
fi

DEFAULT_JVM_OPTS="-XX:+UseContainerSupport -XX:+UseG1GC -XX:MaxRAMPercentage=75.0"
export CMA_AGENT_SERVER_JAVA_OPTS="${DEFAULT_JVM_OPTS} ${CMA_AGENT_SERVER_JAVA_OPTS:-"-Xms2g -Xmx4g"}"

# Set CMA_CONF_DIR before sourcing common.sh so that CMA_CLIENT_SSL_CONF_FILE (set in
# common.sh via CMA_SERVER_CONF_DIR) resolves to the agent's conf directory.  Without
# this, common.sh falls back to ${ROOT_DIR}/cma-server which does not exist in the agent.
CMA_INSTANCE_NAME="${CMA_INSTANCE_NAME:-cma-agent}"
export CMA_CONF_DIR="${CMA_CONF_DIR:-${CMA_APP_DATA_DIR:-/home/cma}/${CMA_INSTANCE_NAME}/conf}"

SCRIPT_PATH="${CMA_BIN_DIR}"
source "${CMA_BIN_DIR}/common.sh"

"${CMA_BIN_DIR}/cma" agent start --verbose
