#!/usr/bin/env bash

# This script is run from the user's Constellation workspace (BUILD_WORKING_DIRECTORY).
# It prepares the workspace by symlinking all required binaries into folder.

###### script header ######

lib=$(realpath @@BASE_LIB@@) || exit 1
stat "${lib}" >> /dev/null || exit 1

# shellcheck source=../sh/lib.bash
if ! source "${lib}"; then
  echo "Error: could not find import"
  exit 1
fi

if [[ ${BUILD_WORKSPACE_DIRECTORY} == "${BUILD_WORKING_DIRECTORY}" ]]; then
  echo "Error: You are trying to run a devbuild in the project root directory."
  echo "You probably want to run it in a subdirectory instead:"
  echo "mkdir -p build && cd build && bazel run //:devbuild"
  exit 1
fi

goos=@@GOOS@@
goarch=@@GOARCH@@
yq=$(realpath @@YQ@@)
stat "${yq}" >> /dev/null
sed=$(realpath @@SED@@)
stat "${sed}" >> /dev/null
bootstrapper=$(realpath @@BOOTSTRAPPER@@)
stat "${bootstrapper}" >> /dev/null
upgrade_agent=$(realpath @@UPGRADE_AGENT@@)
stat "${upgrade_agent}" >> /dev/null
cli=$(realpath @@CLI@@)
stat "${cli}" >> /dev/null
cdbg=$(realpath @@CDBG@@)
stat "${cdbg}" >> /dev/null
container_sums=$(realpath @@CONTAINER_SUMS@@)
stat "${container_sums}" >> /dev/null
edition=$(cat @@EDITION@@)
raw_provider="@@TERRAFORM_PROVIDER@@"
if [[ -n ${raw_provider} ]]; then
  terraform_provider=$(realpath "${raw_provider}")
  stat "${terraform_provider}" >> /dev/null
else
  terraform_provider=""
fi
build_version=$(cat @@VERSION_FILE@@)
if [[ -z ${build_version} ]]; then
  echo "Error: version file is empty"
  exit 1
fi

cd "${BUILD_WORKING_DIRECTORY}"

###### script body ######

replace_prefix() {
  local host_cache=$1
  local builder_cache=$2
  local dir=$3
  if [[ ${dir#"${builder_cache}"} == "${dir}" ]]; then
    echo "${dir}"
    return
  fi
  relpath=${dir#"${builder_cache}"}
  realpath -m "${host_cache}/${relpath}"
}

# Set HOST_CACHE when running in a container with mounted cache.
host_cache="${HOST_CACHE:-${HOME}/.cache}"
builder_cache="${HOME}/.cache"

if [[ $# -eq 0 ]]; then
  workdir="."
else
  workdir="$1"
fi

echo "Using ${edition} cli edition"

ln -sf "$(replace_prefix "${host_cache}" "${builder_cache}" "${bootstrapper}")" "${workdir}/bootstrapper"
ln -sf "$(replace_prefix "${host_cache}" "${builder_cache}" "${upgrade_agent}")" "${workdir}/upgrade-agent"
ln -sf "$(replace_prefix "${host_cache}" "${builder_cache}" "${cdbg}")" "${workdir}/cdbg"
ln -sf "$(replace_prefix "${host_cache}" "${builder_cache}" "${container_sums}")" "${workdir}/container_sums.sha256"
ln -sf "$(replace_prefix "${host_cache}" "${builder_cache}" "${cli}")" "${workdir}/constellation"

if [[ -n ${terraform_provider} ]]; then
  terraform_provider_dir=${HOME}/.terraform.d/plugins/registry.terraform.io/edgelesssys/constellation/${build_version#v}/${goos}_${goarch}/
  mkdir -p "${terraform_provider_dir}"
  ln -sf "${terraform_provider}" "${terraform_provider_dir}/terraform-provider-constellation_${build_version}"
fi

if [[ ! -f "${workdir}/constellation-conf.yaml" ]]; then
  echo "constellation-conf.yaml not present in workspace"
  echo "Build version: ${build_version}"
else
  ${yq} -i eval ".microserviceVersion=\"${build_version}\"" ./constellation-conf.yaml
  echo "Microservice version updated to ${build_version} in constellation-conf.yaml"
fi