#!/usr/bin/env bash
set -euo pipefail
if [[ -n "${PRODU2_TD_ROOT:-}" ]]; then
  td_root="${PRODU2_TD_ROOT}"
elif [[ -x /data/prod2u/toolchain/TD_Release_2026.1_SP2_NL/bin/td ]]; then
  td_root=/data/prod2u/toolchain/TD_Release_2026.1_SP2_NL
else
  td_root=/data/anlogic/TD_Release_2026.1_SP2_NL
fi
if [[ ! -x "$td_root/bin/td" ]]; then
  candidate=/data/prod2u/toolchain/TD_Release_2026.1_SP2_NL/TD_Release_2026.1_SP2_NL
  if [[ -x "$candidate/bin/td" ]]; then td_root="$candidate"; fi
fi
td_runtime="${PRODU2_TD_RUNTIME_LIB:-/data/prod2u/td-runtime/lib}"
td_runtime_root="${PRODU2_TD_RUNTIME_ROOT:-/data/prod2u/td-runtime/root}"
export LD_LIBRARY_PATH="${td_root}/lib:${td_root}/lib/Qt/lib:/workspace/lib:${td_runtime}:${td_runtime_root}/usr/lib/x86_64-linux-gnu:${td_runtime_root}/lib/x86_64-linux-gnu"
export QT_PLUGIN_PATH="${td_root}/lib/Qt/plugins"
export QT_QPA_PLATFORM=offscreen
# The vendor td.sh overwrites LD_LIBRARY_PATH and drops the persistent
# libusb runtime above. Execute the same vendor binary directly so the
# subprocess-scoped dependency set remains intact.
exec "${td_root}/bin/td" "$@"
