#!/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.

function printUsage {
  echo "Usage: celeborn-ratis COMMAND [GENERIC_COMMAND_OPTIONS] [COMMAND_ARGS]"
  echo
  echo "COMMAND is one of:"
  echo -e "  sh    \t Command line tool for Celeborn ratis"
  echo
  echo "GENERIC_COMMAND_OPTIONS supports:"
  echo -e "  -D<property=value>\t Use a value for a given ratis-shell property"
  echo
  echo "Commands print help when invoked without parameters."
}

function runJavaClass {
  CLASS_ARGS=()
  for arg in "$@"; do
      case "${arg}" in
          -D* | -X* | -agentlib* | -javaagent*)
              CELEBORN_RATIS_SHELL_JAVA_OPTS+=" ${arg}" ;;
          *)
              CLASS_ARGS+=("${arg}")
      esac
  done
  "${JAVA}" -cp "${CLASSPATH}" -XX:+IgnoreUnrecognizedVMOptions ${CELEBORN_RATIS_SHELL_JAVA_OPTS} "${CLASS}" ${PARAMETER} "${CLASS_ARGS[@]}"
}

function main {

  if [[ $# == 0 ]]; then
    printUsage
    exit 1
  fi

  COMMAND=$1
  shift

  local CELEBORN_RATIS_SHELL_CLASSPATH

  # load master jars since all ratis related in master
  while read -d '' -r jarfile ; do
      if [[ "$CELEBORN_RATIS_SHELL_CLASSPATH" == "" ]]; then
          CELEBORN_RATIS_SHELL_CLASSPATH="$jarfile";
      else
          CELEBORN_RATIS_SHELL_CLASSPATH="$CELEBORN_RATIS_SHELL_CLASSPATH":"$jarfile"
      fi
  done < <(find "$CELEBORN_HOME/master-jars" ! -type d -name '*.jar' -print0 | sort -z)

  CELEBORN_RATIS_SHELL_CLIENT_CLASSPATH="${CELEBORN_CONF_DIR}/:${CELEBORN_RATIS_SHELL_CLASSPATH}"

  if [ ! -f "${CELEBORN_CONF_DIR}/ratis-log4j.properties" ]; then
    echo "${CELEBORN_CONF_DIR}/ratis-log4j.properties not exists!" >&2
  fi

  CELEBORN_RATIS_SHELL_JAVA_OPTS+=" -Dratis.shell.logs.dir=${CELEBORN_LOG_DIR}"
  CELEBORN_RATIS_SHELL_JAVA_OPTS+=" -Dlog4j.configuration=file:${CELEBORN_CONF_DIR}/ratis-log4j.properties"
  CELEBORN_RATIS_SHELL_JAVA_OPTS+=" -Dorg.apache.jasper.compiler.disablejsr199=true"
  CELEBORN_RATIS_SHELL_JAVA_OPTS+=" -Djava.net.preferIPv4Stack=true"
  CELEBORN_RATIS_SHELL_JAVA_OPTS+=" -Dorg.apache.ratis.thirdparty.io.netty.allocator.useCacheForAllThreads=false"

  PARAMETER=""

  case ${COMMAND} in
  "sh")
    CLASS="org.apache.ratis.shell.cli.sh.RatisShell"
    CLASSPATH=${CELEBORN_RATIS_SHELL_CLIENT_CLASSPATH}
    runJavaClass "$@"
  ;;
  *)
    echo "Unsupported command ${COMMAND}" >&2
    printUsage
    exit 1
  ;;
  esac
}

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

. "${CELEBORN_HOME}/sbin/load-celeborn-env.sh"

main "$@"
