#!/bin/bash
set -eu

TEST_POD=$(kubectl get pod -l app="test_pg" -nsre -o=jsonpath='{.items[*].metadata.name}')
LLVM_VERSIONS=(19 18 17 16 15 14 13)

get_llvm_version()
{
    kubectl exec "$pod" -- bash -c "ldd /var/lib/postgresql/.local/lib/llvmjit.so | grep libLLVM | awk '{print \$1}'"
}

run_test_on_pod()
{
    local pod="$1"

    kubectl cp test_script.sh "$pod":/var/lib/postgresql/

    for version in ${LLVM_VERSIONS[@]}; do
        build_dir="~/postgres_build/build_${version}"
        kubectl exec "$pod" -- bash -c "su postgres -c \" cd $build_dir; make -j19 install &>> /dev/null \" "

        echo -n "Test unpatched version on LLVM $version, $(get_llvm_version): "
        kubectl exec "$pod" -- bash -c "su postgres -c \" cd; bash test_script.sh \" "
    done

    for version in ${LLVM_VERSIONS[@]}; do
        build_dir="~/postgres_build/build_patched_${version}"
        kubectl exec "$pod" -- bash -c "su postgres -c \" cd $build_dir; make -j19 install &>> /dev/null \" "

        echo -n "Test patched version on LLVM $version, $(get_llvm_version): "
        kubectl exec "$pod" -- bash -c "su postgres -c \" cd; bash test_script.sh \" "
    done
}

run_test_on_pod "$TEST_POD"

wait
