#!/usr/bin/env sh

set -o errexit
set -o pipefail
set -o nounset

source ${CI_PROJECT_DIR}/ci/stack_env.sh

echo "Getting system compiler"
${STACK_LOCATION}/spack/bin/spack compiler find --scope system /usr

for c in  "core_compilers" "compilers"; do
    echo "Installing $c"
    _specs=$(comp=$c jq -Mr '.stack | .system_arch as $arch | with_entries(select(.key==env.comp)) | to_entries | .[].value | map("\(.spec) \($arch)")[]' ${STACK_CONFIG})

    echo "Compilers to install:"
    echo "${_specs}"

    if [ "x${_specs}" != "x" ]; then
        ${STACK_LOCATION}/spack/bin/spack spec \
            --install-status \
            --namespaces \
            --long \
            --types \
            ${_specs}

        ${STACK_LOCATION}/spack/bin/spack spec \
            --install-status \
            --namespaces \
            --long \
            --types \
            --json \
            ${_specs} > compilers_spec.json

        ${STACK_LOCATION}/spack/bin/spack install \
            --log-file spack-install.xml \
            --log-format junit \
            --fail-fast \
            --show-log-on-error \
            ${_specs}

        hashes=$(jq -Mr '.spec.nodes | map("/\(.hash)") | join(" ")' compilers_spec.json)

        echo "Adding packages to buildcache"
        ${STACK_LOCATION}/spack/bin/spack buildcache create \
            --update-index \
            --key EDC904DCE3D2E84E \
            /buildcache ${hashes}

        for compiler_hash in $(echo ${hashes}); do
            location=$(${STACK_LOCATION}/spack/bin/spack location -i ${compiler_hash})
            # echo "Checking for compiler"
            # echo "  - ${location}"
            # echo "  - ${location}/compiler/latest"
            # echo "  - ${location}/compiler/latest/linux"
            # echo "  - ${location}/Linux_x86_64/2024/compilers"

            ${STACK_LOCATION}/spack/bin/spack compiler find \
                --scope system \
                ${location} \
                ${location}/compiler/latest \
                ${location}/compiler/latest/linux \
                ${location}/Linux_x86_64/2024/compilers
        done
    fi
done

compilers_specs=$(jq -Mr '.stack | .system_arch as $arch | .core_compilers + .compilers | map("\(.spec) \($arch)")' ${STACK_CONFIG})
system_compiler=$(jq -Mrc '.stack | .system_arch as $arch | .system_compiler | map("\(.compiler)@\(.version) \($arch)")' ${STACK_CONFIG})
core_compilers=$(jq -Mrc '.stack | .system_compiler + .core_compilers | map("\(.compiler)@\(.version)")' ${STACK_CONFIG})
compilers=$(jq -Mrc '.stack | .compilers  | map("\(.compiler)@\(.version)")' ${STACK_CONFIG})

echo "definitions:" > ${SPACK_SYSTEM_CONFIG_PATH}/compiler_definitions.yaml
set +o nounset
for c in "compilers_specs" "system_compiler" "core_compilers" "compilers"; do
    if [ "x${!c}" != "x" ]; then
        echo " - ${c}: ${!c}" >> ${SPACK_SYSTEM_CONFIG_PATH}/compiler_definitions.yaml
    fi
done
set -o nounset

cat ${SPACK_SYSTEM_CONFIG_PATH}/compiler_definitions.yaml
yq -y -s  ".[0].spack.definitions = .[1].definitions + .[0].spack.definitions | .[0]" ${SPACK_SYSTEM_CONFIG_PATH}/spack.yaml ${SPACK_SYSTEM_CONFIG_PATH}/compiler_definitions.yaml > ${SPACK_SYSTEM_CONFIG_PATH}/spack.yaml.new

mv ${SPACK_SYSTEM_CONFIG_PATH}/spack.yaml.new ${SPACK_SYSTEM_CONFIG_PATH}/spack.yaml
