#!/bin/sh
# Copyright (C) 2010-2022 The ESPResSo project
#
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.
#

if test -n "$PYTHONPATH"; then
  PYTHONPATH="/usr//usr/lib64/python3.13/site-packages:$PYTHONPATH"
else
  PYTHONPATH="/usr//usr/lib64/python3.13/site-packages"
fi
export PYTHONPATH

# Open MPI 4.x cannot run in singleton mode on some NUMA systems
if [ "ON" = "ON" ] && [ "ON" = "ON" ]; then
  if [ -z "${OMPI_COMM_WORLD_SIZE}" ] && [ "${OMPI_MCA_hwloc_base_binding_policy}" = "numa" ]; then
    if test -f /proc/cpuinfo && grep --quiet -P "^[Mm]odel name[ \t]*:[ \t]+AMD (EPYC|Ryzen)( |$)" /proc/cpuinfo; then
      echo "warning: if Open MPI fails to set processor affinity, set environment variable OMPI_MCA_hwloc_base_binding_policy to \"none\" or \"l3cache\""
    fi
  fi
fi

if [ "GNU" != "GNU" ] && [ "OFF" = "ON" ]; then
  asan_lib=$("/usr/bin/c++" /dev/null -### -o /dev/null -fsanitize=address 2>&1 | grep -o '[" ][^" ]*libclang_rt.asan[^" ]*[^s][" ]' | sed 's/[" ]//g' | sed 's/\.a$/.so/g')
  export DYLD_INSERT_LIBRARIES="$asan_lib"
  for lib in $asan_lib; do
      test -f $lib && LD_PRELOAD="$lib $LD_PRELOAD"
  done
fi
if [ "GNU" != "GNU" ] && [ "OFF" = "ON" ] && [ "OFF" != "ON" ]; then
  ubsan_lib=$("/usr/bin/c++" /dev/null -### -o /dev/null -fsanitize=undefined 2>&1 | grep -o '[" ][^" ]*libclang_rt.ubsan[^" ]*[^s][" ]' | sed 's/[" ]//g' | sed 's/\.a$/.so/g')
  for lib in $ubsan_lib; do
    test -f $lib && LD_PRELOAD="$lib $LD_PRELOAD"
  done
fi
export LD_PRELOAD
if [ "OFF" = "ON" ]; then
  export UBSAN_OPTIONS="print_stacktrace=1 suppressions=\"/home/abuild/rpmbuild/BUILD/espresso-4.2.2-build/espresso/maintainer/CI/ubsan.supp\" $UBSAN_OPTIONS"
  if [ "OFF" = "ON" ]; then
    export UBSAN_OPTIONS="halt_on_error=1 $UBSAN_OPTIONS"
  fi
fi
if [ "OFF" = "ON" ]; then
  ASAN_OPTIONS="protect_shadow_gap=0 allocator_may_return_null=1 $ASAN_OPTIONS"
  if [ "OFF" = "ON" ]; then
    ASAN_OPTIONS="halt_on_error=1 $ASAN_OPTIONS"
  fi
  if [ "$1" = "--leaks" ]; then
    shift
  else
    ASAN_OPTIONS="$ASAN_OPTIONS detect_leaks=0"
  fi
  export ASAN_OPTIONS
fi
if [ "" = "ON" ] && [ "OFF" = "ON" ]; then
  export MSAN_OPTIONS="halt_on_error=1 $MSAN_OPTIONS"
fi

case "$1" in
    --gdb)
        shift
        [ "/usr/bin/python3.13" = "IPYTHON_EXECUTABLE-NOTFOUND" ] && exec gdb -ex "set print thread-events off" -ex "set exec-wrapper sh -c 'exec \"IPYTHON_EXECUTABLE-NOTFOUND\" \"\$@\"'" --args "/usr/bin/python3.13" "$@"
        exec gdb --args "/usr/bin/python3.13" "$@"
        ;;
    --lldb)
        shift
        exec lldb -- "/usr/bin/python3.13" "$@"
        ;;
    --coverage)
        shift
        exec /usr/bin/python3.13 -m coverage run --source=/home/abuild/rpmbuild/BUILD/espresso-4.2.2-build/espresso/build/testsuite/ --rcfile="/home/abuild/rpmbuild/BUILD/espresso-4.2.2-build/espresso/.coveragerc" "$@"
        ;;
    --valgrind)
        shift
        exec valgrind --leak-check=full "/usr/bin/python3.13" "$@"
        ;;
    --cuda-gdb)
        shift
        exec cuda-gdb --args "/usr/bin/python3.13" "$@"
        ;;
    --cuda-memcheck)
        shift
        exec cuda-memcheck "/usr/bin/python3.13" "$@"
        ;;
    --gdb=*)
        options="${1#*=}"
        shift
        [ "/usr/bin/python3.13" = "IPYTHON_EXECUTABLE-NOTFOUND" ] && exec gdb -ex "set print thread-events off" -ex "set exec-wrapper sh -c 'exec \"IPYTHON_EXECUTABLE-NOTFOUND\" \"\$@\"'" ${options} --args "/usr/bin/python3.13" "$@"
        exec gdb ${options} --args "/usr/bin/python3.13" "$@"
        ;;
    --lldb=*)
        options="${1#*=}"
        shift
        exec lldb ${options} -- "/usr/bin/python3.13" "$@"
        ;;
    --valgrind=*)
        options="${1#*=}"
        shift
        exec valgrind ${options} "/usr/bin/python3.13" "$@"
        ;;
    --cuda-gdb=*)
        options="${1#*=}"
        shift
        exec cuda-gdb ${options} --args "/usr/bin/python3.13" "$@"
        ;;
    --cuda-memcheck=*)
        options="${1#*=}"
        shift
        exec cuda-memcheck ${options} "/usr/bin/python3.13" "$@"
        ;;
    *)
        exec "/usr/bin/python3.13" "$@"
        ;;
esac
