#!/usr/bin/env bash

SCRIPT_PATH="$(
  cd "$(dirname "$0")" >/dev/null 2>&1 || exit
  pwd -P
)"
source "$SCRIPT_PATH"/common.sh # load commons

# OS to build on
DOCKER_OS=rhel

DEFAULT_RHEL_VERSION=8
DEFAULT_AMAZONLINUX_VERSION=al2023

CMA_SERVER_SSL=false
AM2CM_DOCKER_IMAGE_VERSION=latest

AM2CM_VENV=venv

CMA_EXTRAS_GPL_TAR_LOCATION='cma-extras-gpl.tar.gz'

AIRGAPPED=false

CMA_DOCKER_DIST_TO_BUILD=${CMA_DOCKER_DIST_TO_BUILD:-distributed}

for i in "$@"; do
  case $i in
  -os=* | --docker-os=*)
    DOCKER_OS=${i#*=}
    ;;
  -iv=* | --image-version=*)
    AM2CM_DOCKER_IMAGE_VERSION=${i#*=}
    ;;
  -p=* | --profile=*)
    CMA_SERVER_EXTRA_PROFILE=${i#*=}
    ;;
  -d=* | --dist=*)
    CMA_DOCKER_DIST_TO_BUILD=${i#*=}
    ;;
  --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#*=}
    ;;
  -d | --dev-mode)
    DEV_MODE=true
    ;;
  -h | --help)
    HELP=true
    ;;
  *)
    echo "Unknown option: $i"
    HELP=true
    ;;
  esac
done

__usage="
 Usage: $(basename "$0") [options]
 Options:
    -os, --docker-os: OS to build on (rhel,amazonlinux) (Default: ${DOCKER_OS})
    -iv, --image-version: Set the version tag for the docker image
    -p, --profile: Set the active profile(s) for the cma-server
    -d, --dist: CMA distributive to build: distributed, agent (Default: ${CMA_DOCKER_DIST_TO_BUILD})
    --ssl: Enable ssl for the CMA server
    --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
    --cma-extras-gpl-tar-location: Location of GPL extras TAR
    -h, --help: Print this help
 "

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

if [ -z "${DOCKER_OS}" ]; then
  echo "ERROR: The OS for docker container is not specified!"
  exit 1;
fi

case ${DOCKER_OS} in
  "rhel") BUILD_OS=centos BUILD_OS_VERSION=$DEFAULT_RHEL_VERSION ;;
  "amazonlinux") BUILD_OS=centos BUILD_OS_VERSION=$DEFAULT_AMAZONLINUX_VERSION ;;
   *)
     echo "ERROR: ${DOCKER_OS} is unsupported!!"
     exit 1
     ;;
esac

if [ -z "${CMA_DOCKER_DIST_TO_BUILD}" ]; then
  CMA_DOCKER_DIST_TO_BUILD=distributed
fi

if [ "${CMA_DOCKER_DIST_TO_BUILD}" != "standalone" ] && [ "${CMA_DOCKER_DIST_TO_BUILD}" != "distributed" ] && [ "${CMA_DOCKER_DIST_TO_BUILD}" != "agent" ]; then
  echo "ERROR: Distribution type ${CMA_DOCKER_DIST_TO_BUILD} is unsupported!!"
  exit 1
fi

if [ "${CMA_MASTER}" = true ]; then
  AM2CM_DOCKER_IMAGE="cma-master-server"
else
  AM2CM_DOCKER_IMAGE="cma-${DOCKER_OS}"
fi

AM2CM_ROOT_DIR=/$(basename "${ROOT_DIR}")

SRC_CMA_SERVER=cma-server
SRC_CMA_ANSIBLE=cma-ansible
SRC_CMA_TOOLS=cma-tools
SRC_CMA_BIN=bin
SRC_CMA_LIB=lib
SRC_CMA_VAULT_LOCAL=vault-local
SRC_STARTUP_SCRIPT=docker/startup.sh

if [ "${DEV_MODE}" = true ]; then
  SRC_CMA_SERVER=$SRC_CMA_SERVER/target/$SRC_CMA_SERVER
fi

CMA_SERVER_PROFILE=$(get_docker_server_profiles)

if [ "$AIRGAPPED" = true ]; then
  echo "Copying $CMA_EXTRAS_GPL_TAR_LOCATION into $ROOT_DIR"
  cp $CMA_EXTRAS_GPL_TAR_LOCATION $ROOT_DIR
  if [ $? -ne 0 ]; then
    echo "Failed to copy cma-extras to the home dir"
    exit 1
  fi
  CMA_EXTRAS_GPL_TAR=$(basename $CMA_EXTRAS_GPL_TAR_LOCATION)
fi

mkdir -p "$ROOT_DIR/jdbc-drivers"

CMA_ROOT_DIR="/$(basename ${AM2CM_ROOT_DIR})"

build_standalone() {
  echo "Calling docker build command..."

  docker build -t "${AM2CM_DOCKER_IMAGE}":"${AM2CM_DOCKER_IMAGE_VERSION}" \
    --build-arg BUILD_OS="${BUILD_OS}" --build-arg BUILD_OS_VERSION="${BUILD_OS_VERSION}" \
    --build-arg CMA_ROOT_DIR="${CMA_ROOT_DIR}" --build-arg SRC_CMA_SERVER=${SRC_CMA_SERVER} \
    --build-arg SRC_CMA_ANSIBLE=${SRC_CMA_ANSIBLE} --build-arg SRC_CMA_TOOLS=${SRC_CMA_TOOLS} \
    --build-arg SRC_CMA_BIN=${SRC_CMA_BIN} \
    --build-arg SRC_CMA_LIB=${SRC_CMA_LIB} --build-arg SRC_STARTUP_SCRIPT=${SRC_STARTUP_SCRIPT} \
    --build-arg AIRGAPPED=${AIRGAPPED} --build-arg CMA_SERVER_EXTRA_PROFILE=${CMA_SERVER_EXTRA_PROFILE} \
    --build-arg CMA_SERVER_SSL=${CMA_SERVER_SSL} --build-arg CMA_SERVER_LDAP=${CMA_SERVER_LDAP} \
    --build-arg CMA_VAULT=${CMA_VAULT} --build-arg CMA_EXTRAS_GPL_TAR="${CMA_EXTRAS_GPL_TAR}" \
    --build-arg INNER_PYPY_WEBSERVER_PORT="${PYPI_WEBSERVER_PORT}" --build-arg SRC_CMA_VAULT_LOCAL=${SRC_CMA_VAULT_LOCAL} \
    -f "${ROOT_DIR}"/docker/Dockerfile "${ROOT_DIR}" \
    --target cma-final

  if [ $? -ne 0 ]; then
    echo "Docker build command failed... Exiting..."
    exit 1
  fi

  docker tag "${AM2CM_DOCKER_IMAGE}":"${AM2CM_DOCKER_IMAGE_VERSION}" "${AM2CM_DOCKER_IMAGE}":latest

  if [ $? -ne 0 ]; then
    echo "Docker tag command failed... Exiting..."
    exit 1
  fi
}

build_distributed() {
  export CMA_SERVER_EXTRA_PROFILE

  export CMA_HOME_DIR="${ROOT_DIR}"
  export CMA_SERVER_DOCKER_IMAGE="${AM2CM_DOCKER_IMAGE}"
  export DOCKER_IMAGE_VERSION="${AM2CM_DOCKER_IMAGE_VERSION}"
  export CMA_ROOT_DIR
  export CMA_GATEWAY_SERVER_HOST_PORT
  export CMA_SERVER_SSL

  export CMA_VAULT

  cmd=(docker compose -f "${ROOT_DIR}/docker/cma.distributed.yml")
  [[ "$CMA_VAULT" == true ]] && cmd+=(-f "${ROOT_DIR}/docker/cma-vault.yml")
  [[ $CMA_SERVER_EXTRA_PROFILE =~ "postgres" ]] && cmd+=(-f "${ROOT_DIR}/docker/cma-master-db.yml")
  [[ $CMA_SERVER_EXTRA_PROFILE =~ "mysql" ]] && cmd+=(-f "${ROOT_DIR}/docker/cma-master-mysql-db.yml")
  cmd+=(build)

  "${cmd[@]}"
}

build_agent() {
  export CMA_SERVER_EXTRA_PROFILE

  export CMA_HOME_DIR="${ROOT_DIR}"
  export DOCKER_IMAGE_VERSION="${AM2CM_DOCKER_IMAGE_VERSION}"
  export CMA_ROOT_DIR
  export CMA_SERVER_SSL
  export CMA_AGENT_DOCKER_IMAGE=${CMA_AGENT_DOCKER_IMAGE:-"cma-agent-${DOCKER_OS}:${DOCKER_IMAGE_VERSION}"}
  export BUILDX_NO_DEFAULT_ATTESTATIONS=1

  cmd=(docker compose -f "${ROOT_DIR}/docker/cma.agent.yml")
  [[ $CMA_SERVER_EXTRA_PROFILE =~ "postgres" ]] && cmd+=(-f "${ROOT_DIR}/docker/cma-agent-db.yml")
  [[ $CMA_SERVER_EXTRA_PROFILE =~ "mysql" ]] && cmd+=(-f "${ROOT_DIR}/docker/cma-agent-mysql-db.yml")
  cmd+=(build)

  "${cmd[@]}"
}

"build_${CMA_DOCKER_DIST_TO_BUILD}"
