#!/usr/bin/bash
# Start the Anaconda's Python module $1.
#
# Examples:
#   ./start-module pyanaconda.modules.boss
#   ./start-module --env LD_PRELOAD=libgomp.so.1 pyanaconda.modules.payloads
#

# Process the arguments.
while true
do
  case $1 in
    # Set up the environment.
    --env)
      export $2
      shift 2
    ;;
    # Nothing else to do.
    *)
      break
    ;;
  esac
done

# Add Anaconda addons to the PYTHONPATH.
PYTHONPATH="$PYTHONPATH:/usr/share/anaconda/addons"

# Export the modified PYTHONPATH.
export PYTHONPATH

# Get remote debugger port for a module from the configuration file
# Returns the port number for the given module, or empty if not configured
get_remote_debugger_port() {
  local module="$1"
  local config_file="/run/anaconda/remote-debugger.conf"

  # Check if config file exists
  if [ ! -f "$config_file" ]; then
    return
  fi

  # Read port from config file (format: module=port)
  local port
  port=$(grep "^${module}=" "$config_file" | cut -d'=' -f2)

  if [ -n "$port" ]; then
    echo "$port"
  fi
}

# Start a Python module in the detached mode.
PORT=$(get_remote_debugger_port "$1")

if [ -n "$PORT" ]; then
  # Start with remote debugger (debugpy)
  echo "Starting module $1 with remote debugger on port $PORT"
  python3 -m debugpy --wait-for-client --listen "0.0.0.0:$PORT" -m "$1" &
else
  # Start normally without remote debugger
  python3 -m "$1" &
fi

module_pid="$!"

# Wait for a minute in the detached mode.
sleep 60 &
timeout_pid="$!"

# If the Python module fails before the timeout, return its exit status.
# Otherwise, return 0.
wait -n "${timeout_pid}" "${module_pid}"
