#!/usr/bin/env bash
# shellcheck disable=SC1091
set -ex

conda clean --all --yes

{(

  declare -gx CUDA_PATH
  export CUQUANTUM_ROOT="${PREFIX}"
  export CUTENSOR_ROOT="${PREFIX}"
  export LD_LIBRARY_PATH="${PREFIX}/lib":"${LD_LIBRARY_PATH}"
  declare -Agx cuda_paths=(
    ["linux-64"]="${BUILD_PREFIX}/targets/x86_64-linux/"
    ["linux-aarch64"]="${BUILD_PREFIX}/targets/sbsa-linux/"
  )
  declare -agx supported_platforms=(
    "linux-64"
    "linux-aarch64"
  )
  declare -gx cuda_compiler_version="${cuda_compiler_version:-}"
  declare -gx target_platform="${target_platform:-}"
  if [[ "${cuda_compiler_version%.*}" == "12" ]] ; then
    echo "CUDA_COMPILER_VERSION 12 detected: ${cuda_compiler_version}"
    if [[ "${supported_platforms[*]}" =~ ${target_platform} ]] ; then
      CUDA_PATH="${cuda_paths[${target_platform}]}"
    fi
  fi
  echo "CUDA_PATH: ${CUDA_PATH}"
  cd python &&
  "${PREFIX}/bin/python3" -m pip install --no-deps --no-build-isolation -vvv .
)}