#!/usr/bin/env bash

SCRIPT_PATH=$(cd $(dirname "$(readlink -f "$0")")/ && pwd)
source "$SCRIPT_PATH"/common.sh # load commons
create_logs_dir

LOG_FILE="$LOGS_DIR"/$(basename "$0" .sh).log

DEFAULT_DOCKER_OS=rhel
DEFAULT_AM2CM_DOCKER_IMAGE_VERSION=latest
DEFAULT_CMA_DOCKER_DIST_TO_BUILD=distributed
DEFAULT_PYTHON_EXECUTABLE=python3

DOCKER_OS=${DEFAULT_DOCKER_OS}
CMA_DOCKER_DIST_TO_BUILD=${DEFAULT_CMA_DOCKER_DIST_TO_BUILD}
AM2CM_DOCKER_IMAGE_VERSION=${DEFAULT_AM2CM_DOCKER_IMAGE_VERSION}
CMA_SERVER_SSL=${CMA_SERVER_SSL:-false}
AIRGAPPED=${CMA_AIRGAPPED:-false}
PYPI_WEBSERVER_PORT=${CMA_PYPI_WEBSERVER_PORT:-9003}
CMA_SKIP_JAVA_CHECK=${CMA_SKIP_JAVA_CHECK:-false}
SKIP_PYTHON_CHECK=${CMA_SKIP_PYTHON_CHECK:-false}

for i in "$@"; do
  case $i in
  --local)
    SETUP_LOCAL=true
    ;;
  --docker)
    SETUP_DOCKER=true
    ;;
  -d=* | --dist=*)
    CMA_DOCKER_DIST_TO_BUILD=${i#*=}
    ;;
  -os=* | --docker-os=*)
    DOCKER_OS=${i#*=}
    ;;
  -iv=* | --image-version=*)
    AM2CM_DOCKER_IMAGE_VERSION=${i#*=}
    ;;
  -p=* | --profile=*)
    CMA_SERVER_EXTRA_PROFILE=${i#*=}
    ;;
  -py=* | --python-executable=*)
    PYTHON_EXECUTABLE=${i#*=}
    ;;
  --skip-java-check)
    CMA_SKIP_JAVA_CHECK=true
    ;;
  --skip-python-check)
    SKIP_PYTHON_CHECK=true
    ;;
  --ssl)
    CMA_SERVER_SSL=true
    ;;
  --ldap)
    CMA_SERVER_LDAP=true
    ;;
  --vault)
    CMA_VAULT=true
    ;;
  --airgapped)
    AIRGAPPED=true
    ;;
  --pypi-webserver-port=*)
    PYPI_WEBSERVER_PORT=${i#*=}
    ;;
  --cma-extras-gpl-tar-location=*)
    CMA_EXTRAS_GPL_TAR_LOCATION=${i#*=}
    ;;
  -h | --help)
    HELP=true
    ;;
  *)
    echo "Unknown option: $i"
    HELP=true
    ;;
  esac
done

__usage="
 Usage: $(basename "$0") [options]
 Options:
    --local: Install CMA on the local machine
    --docker: Install CMA in a docker container
    --ssl: Enable ssl for the cma-server
    -os, --docker-os: the OS of the docker container (Default: $DEFAULT_DOCKER_OS)
    -d, --dist: the CMA distribution to build in docker (Default: ${DEFAULT_CMA_DOCKER_DIST_TO_BUILD})
    -p, --profile: Set additional profile(s) for the cma-server
    -py, --python-executable: Path to python3 executable
    --airgapped: Turn on airgapped mode (meaning: install all pypi deps from cma & cma-extras artifact) (Default: $AIRGAPPED)
    --pypi-webserver-port: Port of pypi webserver to install dependencies from (Default: $PYPI_WEBSERVER_PORT)
    --cma-extras-gpl-tar-location: Location of GPL extras TAR (Default: ROOT_DIR/..) NOTE: this a requirement for airgapped mode!!
    --skip-java-check: Skip java version check. This is not recommended unless the version check fails even though the JAVA_HOME is JDK 11 or later.
    --skip-python-check: Skip python version check. This is not recommended unless the version check fails even though the python version is supported.
    -h, --help: Print this help
 "

if [ "$AIRGAPPED" = true ]; then
  echo "CMA_EXTRAS_GPL_TAR_LOCATION: $CMA_EXTRAS_GPL_TAR_LOCATION"
  if [ ! -f "$CMA_EXTRAS_GPL_TAR_LOCATION" ]; then
    echo "CMA extras gpl tar is missing, please provide as follows: --cma-extras-gpl-tar-location=<location>"
    echo "or place it next to your CMA root dir"
    exit 1
  fi
fi


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

while [ -z "${SETUP_LOCAL}" ] && [ -z "${SETUP_DOCKER}" ]; do
  read -r -p "Select the type of installation ([l]ocal|[d]ocker):" INSTALL_TYPE
  case  $INSTALL_TYPE in
    l | local) SETUP_LOCAL=true;;
    d | docker) SETUP_DOCKER=true;;
    *) echo "ERROR: The '${INSTALL_TYPE}' is not a valid installation type! Please select one of the following options [l]ocal or [d]ocker"
      ;;
  esac
done

if [ "${SETUP_DOCKER}" = true ]; then
  if [ -z "${DOCKER_OS}" ]; then
    read -r -p "Enter the OS of the docker container [${DEFAULT_DOCKER_OS}]:" DOCKER_OS
    DOCKER_OS=${DOCKER_OS:-${DEFAULT_DOCKER_OS}}
  fi
  if [ -z "${AM2CM_DOCKER_IMAGE_VERSION}" ]; then
    read -r -p "Enter the version for the docker image [${DEFAULT_AM2CM_DOCKER_IMAGE_VERSION}]:" AM2CM_DOCKER_IMAGE_VERSION
    AM2CM_DOCKER_IMAGE_VERSION=${AM2CM_DOCKER_IMAGE_VERSION:-${DEFAULT_AM2CM_DOCKER_IMAGE_VERSION}}
  fi
  if [ -z "${CMA_DOCKER_DIST_TO_BUILD}" ]; then
    read -r -p "Enter the type of image you want to create - distributed or agent [${DEFAULT_CMA_DOCKER_DIST_TO_BUILD}]:" CMA_DOCKER_DIST_TO_BUILD
    CMA_DOCKER_DIST_TO_BUILD=${CMA_DOCKER_DIST_TO_BUILD:-${DEFAULT_CMA_DOCKER_DIST_TO_BUILD}}
  fi
fi

if [ "${SETUP_DOCKER}" = true ]; then
  echo "Building docker image..." | tee -a "$LOG_FILE"
  BUILD_DOCKER_OPTS="$DEV_OPTS -os=${DOCKER_OS} -iv=${AM2CM_DOCKER_IMAGE_VERSION} --profile=${CMA_SERVER_EXTRA_PROFILE} -d=${CMA_DOCKER_DIST_TO_BUILD} "
  if [ "$AIRGAPPED" = true ]; then
    BUILD_DOCKER_OPTS+="--cma-extras-gpl-tar-location=${CMA_EXTRAS_GPL_TAR_LOCATION} --pypi-webserver-port=${PYPI_WEBSERVER_PORT} --airgapped "
  fi

  if [ "$CMA_SERVER_SSL" = true ]; then
    BUILD_DOCKER_OPTS+="--ssl "
  fi

  if [ "$CMA_SERVER_LDAP" = true ]; then
    BUILD_DOCKER_OPTS+="--ldap "
  fi

  if [ "$CMA_VAULT" = true ]; then
    BUILD_DOCKER_OPTS+="--vault "
  fi

  "$SCRIPT_PATH"/build-docker.sh ${BUILD_DOCKER_OPTS} | tee -a "$LOG_FILE"

  if [ "${PIPESTATUS[0]}" -ne "0" ]; then
    echo "Building the image has failed..." | tee -a "$LOG_FILE"
    exit 1
  fi
fi

if [ "${SETUP_LOCAL}" = true ]; then

  # check java
  if [ "$CMA_SKIP_JAVA_CHECK" = true ]; then
    echo "Java verification skipped!" | tee -a "$LOG_FILE"
  else
    locate_and_verify_cma_java_home > >(tee -a "$LOG_FILE")
    echo "Java verification done!" | tee -a "$LOG_FILE"
  fi

  if [ -z "${PYTHON_EXECUTABLE}" ]; then
    PYTHON_EXECUTABLE=$(get_python_executable $DEFAULT_PYTHON_EXECUTABLE)
  fi

  # check python
  if [ "$SKIP_PYTHON_CHECK" = true ]; then
    echo "Python verification skipped!" | tee -a "$LOG_FILE"
  else
    # Using redirection instead of piping because if a function exports a variable,
    # it shouldn't be called in a subshell, otherwise the variable will NOT be exported.
    locate_and_verify_cma_python_executable > >(tee -a "$LOG_FILE")
    echo "Python verification done!" | tee -a "$LOG_FILE"
  fi

  # create venv and install python dependencies
  setup_control_node_python_with_dependencies

  if [ "${CMA_VAULT}" = true ]; then
    echo "Setup vault server.." | tee -a "$LOG_FILE"
    ${VAULT_SERVER_BIN_DIR}/vault-server.sh setup | tee -a "$LOG_FILE"
  fi
fi

echo "Setup completed!" | tee -a "$LOG_FILE"
