#!/usr/bin/env bash
#
# Copyright (c) 2025, 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.
#

source "${SCRIPT_PATH:-${CMA_HOME}/bin}"/common.sh # load commons

CMA_AUTH_SERVER_HTTP_PORT=${CMA_AUTH_SERVER_HTTP_PORT:-9000}
CMA_AUTH_SERVER_HTTPS_PORT=${CMA_AUTH_SERVER_HTTPS_PORT:-9443}
CMA_GATEWAY_SERVER_HTTP_PORT=${CMA_GATEWAY_SERVER_HTTP_PORT:-8090}
CMA_GATEWAY_SERVER_HTTPS_PORT=${CMA_GATEWAY_SERVER_HTTPS_PORT:-8093}
CMA_SERVER_DISTRIBUTED_HTTP_PORT=${CMA_SERVER_DISTRIBUTED_HTTP_PORT:-8890}
CMA_SERVER_DISTRIBUTED_HTTPS_PORT=${CMA_SERVER_DISTRIBUTED_HTTPS_PORT:-8893}

if [ "${CMA_SERVER_SSL}" = true ] || is_ssl_active; then
  export CMA_AUTH_SERVER_HOST_PORT=${CMA_AUTH_SERVER_HOST_PORT:-${CMA_AUTH_SERVER_HTTPS_PORT}}
  export CMA_GATEWAY_SERVER_HOST_PORT=${DOCKER_HOST_PORT:-${CMA_GATEWAY_SERVER_HTTPS_PORT}}
  export CMA_SERVER_HOST_PORT=${CMA_SERVER_HOST_PORT:-${CMA_SERVER_DISTRIBUTED_HTTPS_PORT}}
  export CMA_SERVER_PROTOCOL=https
  export CMA_SERVER_SSL=true
  CMA_AUTH_SERVER_PROFILES="${CMA_AUTH_SERVER_PROFILES},ssl"
fi

export CMA_AUTH_SERVER_HOST=${CMA_AUTH_SERVER_HOSTNAME:-localhost}
export CMA_AUTH_SERVER_PORT=${CMA_AUTH_SERVER_HOST_PORT:-${CMA_AUTH_SERVER_HTTP_PORT}}

export CMA_AUTH_SERVER_PROFILES=${CMA_AUTH_SERVER_PROFILES}
export CMA_GATEWAY_SERVER_HOST=${CMA_GATEWAY_SERVER_HOST:-localhost}
export CMA_GATEWAY_SERVER_PORT=${CMA_GATEWAY_SERVER_HOST_PORT:-${CMA_GATEWAY_SERVER_HTTP_PORT}}
export CMA_AUTH_SERVER_LOG_FILE="${CMA_LOG_DIR}"/cma-auth-server.log
export CMA_GATEWAY_SERVER_LOG_FILE="${CMA_LOG_DIR}"/cma-gateway-server.log

export CMA_GATEWAY_SERVER_PROFILES=${CMA_GATEWAY_SERVER_PROFILES}

export CMA_SERVER_HOST=${CMA_SERVER_HOSTNAME:-localhost}
export CMA_SERVER_PORT=${CMA_SERVER_HOST_PORT:-${CMA_SERVER_DISTRIBUTED_HTTP_PORT}}

export CMA_GATEWAY_SERVER_PID_FILE="${CMA_GATEWAY_SERVER_PID_FILE:-${CMA_INSTANCE_DIR}/cma-gateway-server.pid}"
export CMA_AUTH_SERVER_PID_FILE="${CMA_AUTH_SERVER_PID_FILE:-${CMA_INSTANCE_DIR}/cma-auth-server.pid}"

export CMA_SERVER_PROFILES=$(get_server_profiles)
export CMA_GATEWAY_SERVER_PROFILES=${CMA_SERVER_PROFILES}
export CMA_AUTH_SERVER_PROFILES=${CMA_SERVER_PROFILES}
export CMA_SERVER_EXTRA_PROFILE

start_cma_auth_server() {
  "$ROOT_DIR"/cma-auth-server/cma-auth-server.sh start
}

start_cma_gateway_server() {
  "$ROOT_DIR"/cma-gateway-server/cma-gateway-server.sh start
}

stop_cma_auth_server() {
  echo "Stopping the CMA Auth server.." | tee -a "$LOG_FILE"
  if is_process_from_pidfile_running "$CMA_AUTH_SERVER_PID_FILE"; then
    "$ROOT_DIR"/cma-auth-server/cma-auth-server.sh stop
  else
    echo "CMA Auth Server is not running!"
  fi
}

stop_cma_gateway_server() {
  echo "Stopping the CMA Gateway server.." | tee -a "$LOG_FILE"
  if is_process_from_pidfile_running "$CMA_GATEWAY_SERVER_PID_FILE"; then
    "$ROOT_DIR"/cma-gateway-server/cma-gateway-server.sh stop
  else
    echo "CMA Gateway Server is not running!"
  fi
}

wait_for_auth_server_running() {
  echo "Waiting for Auth Server to fully start..."
  CMA_VERSION=${CMA_VERSION:-$(get_cma_version)}

  local retries=30
  local interval=2
  for i in $(seq 1 $retries); do
    if timeout 3s "${JAVA_HOME}"/bin/java -cp "$ROOT_DIR/cma-auth-server/cma-auth-server-${CMA_VERSION}.jar" com.cloudera.migration.cma.authorization.server.health.HealthChecker >/dev/null 2>&1; then
      echo "Auth server is running correctly."
      return 0
    fi
    sleep $interval
  done
  echo "Health check failed after $retries attempts." >&2
  # Run it again but this time print the output
  timeout 3s "${JAVA_HOME}"/bin/java -cp "$ROOT_DIR/cma-auth-server/cma-auth-server-${CMA_VERSION}.jar" com.cloudera.migration.cma.authorization.server.health.HealthChecker
  exit 1
}
