#!/usr/bin/env bash

print_error() {
echo "
---------------------------------- PYTHON ERROR ---------------------------------
   Has RELION been provided a Python interpreter with the correct environment?
   The interpreter can be passed to RELION either during Cmake configuration by
     using the Cmake flag -DPYTHON_EXE_PATH=<path/to/python/interpreter>.
   NOTE: For some modules TORCH_HOME needs to be set to find pretrained models
---------------------------------------------------------------------------------

  Using python executable: $1
"
}

# Set the Python executable path
python_executable="/opt/conda/conda-bld/relion_1774678849677/_build_env/bin/python3"
torch_home="/opt/conda/conda-bld/relion_1774678849677/_h_env_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placehold_placeh/share/.cache/torch"
relion_home="/opt/conda/conda-bld/relion_1774678849677/work/build"

# Check if the python executable exists
if [ ! -x "$python_executable" ]; then
    # Check for default python executable
    python_executable=$(command -v python)

    if [ -z "$python_executable" ]; then
        print_error "$python_executable"
        exit 1
    fi
fi

# Run the Python script with forwarded arguments
if [ -n "$torch_home" ]; then
  TORCH_HOME="$torch_home" "$python_executable" "$relion_home"/bin/relion_trace_amyloids.py "$@"
else
  "$python_executable" "$relion_home"/bin/relion_trace_amyloids.py "$@"
fi

# Check the return status of the python command
if [ $? -ne 0 ]; then
    print_error "$python_executable"
    exit 2
fi

# Exit
exit 0

