#!/bin/sh
# Copyright (C) 2010-2026 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/lib64/python3.13/site-packages:$PYTHONPATH"
else
  PYTHONPATH="/usr/lib64/python3.13/site-packages"
fi
export PYTHONPATH
if test -z "$OMP_PROC_BIND"; then
  export OMP_PROC_BIND=false
fi
if test -z "$OMP_NUM_THREADS"; then
  export OMP_NUM_THREADS=1
fi

espresso_parse_cmake_boolean_value() {
  local varname="${2}"
  local value=""
  if [ "${3}" = "1" ]; then
    value="ON"
  elif [ "${3}" = "0" ]; then
    value="OFF"
  fi
  if [ "${1}" != "#define" ] || [ "${value}" = "" ]; then
    echo "error: failed to parse CMake variable ${varname} (using '$(ps -hp $$ | awk '{print $5}')' and CMake 4.2.3)" >&2
    exit 2
  fi
  eval "${varname}='${value}'"
}

espresso_parse_cmake_boolean_value \#define ESPRESSO_WARNINGS_ARE_ERRORS 0
espresso_parse_cmake_boolean_value \#define ESPRESSO_MPIEXEC_GUARD_SINGLETON_NUMA 1
espresso_parse_cmake_boolean_value \#define ESPRESSO_ADD_OMPI_SINGLETON_WARNING 1
espresso_parse_cmake_boolean_value \#define ESPRESSO_BUILD_WITH_UBSAN 0
espresso_parse_cmake_boolean_value \#define ESPRESSO_BUILD_WITH_ASAN 0
espresso_parse_cmake_boolean_value \#define ESPRESSO_BUILD_WITH_MSAN 0

# Open MPI 4.x cannot run in singleton mode on some NUMA systems
if [ "${ESPRESSO_ADD_OMPI_SINGLETON_WARNING}" = "ON" ] && [ "${ESPRESSO_MPIEXEC_GUARD_SINGLETON_NUMA}" = "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" ] && [ "${ESPRESSO_BUILD_WITH_ASAN}" = "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" ] && [ "${ESPRESSO_BUILD_WITH_UBSAN}" = "ON" ] && [ "${ESPRESSO_BUILD_WITH_ASAN}" != "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 [ "${ESPRESSO_BUILD_WITH_UBSAN}" = "ON" ]; then
  export UBSAN_OPTIONS="print_stacktrace=1 suppressions=\"/home/abuild/rpmbuild/BUILD/espresso-5.0.0-build/espresso-5.0.0/maintainer/CI/ubsan.supp\" $UBSAN_OPTIONS"
  if [ "${ESPRESSO_WARNINGS_ARE_ERRORS}" = "ON" ]; then
    export UBSAN_OPTIONS="halt_on_error=1 $UBSAN_OPTIONS"
  fi
fi
if [ "${ESPRESSO_BUILD_WITH_ASAN}" = "ON" ]; then
  ASAN_OPTIONS="protect_shadow_gap=0 allocator_may_return_null=1 $ASAN_OPTIONS"
  if [ "${ESPRESSO_WARNINGS_ARE_ERRORS}" = "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 [ "${ESPRESSO_BUILD_WITH_MSAN}" = "ON" ] && [ "${ESPRESSO_WARNINGS_ARE_ERRORS}" = "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-5.0.0-build/espresso-5.0.0/build/testsuite/" --rcfile="/home/abuild/rpmbuild/BUILD/espresso-5.0.0-build/espresso-5.0.0/.coveragerc" "$@"
        ;;
    --valgrind)
        shift
        exec valgrind --leak-check=full "/usr/bin/python3.13" "$@"
        ;;
    --cuda-gdb)
        shift
        exec cuda-gdb --args "/usr/bin/python3.13" "$@"
        ;;
    --cuda-sanitizer)
        shift
        exec compute-sanitizer --leak-check full "/usr/bin/python3.13" "$@"
        ;;
    --kernprof)
        shift
        exec "/usr/bin/python3.13" $(which kernprof) --line-by-line --view "$@"
        ;;
    --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-sanitizer=*)
        options="${1#*=}"
        shift
        exec compute-sanitizer ${options} "/usr/bin/python3.13" "$@"
        ;;
    --kernprof=*)
        options="${1#*=}"
        shift
        exec "/usr/bin/python3.13" $(which kernprof) ${options} "$@"
        ;;
    *)
        exec "/usr/bin/python3.13" "$@"
        ;;
esac
