#!/bin/sh
# Portable launcher for pimd-prep.
# The tar.gz distribution preserves this file's executable bit. If permissions
# are lost (for example after ZIP extraction), run it as: sh prep/pimd-prep

if ! command -v python3 >/dev/null 2>&1; then
    echo "ERROR: Python 3 is required to run pimd-prep." >&2
    echo "Please install Python 3 (Python 3.11 or later is recommended) and try again." >&2
    exit 127
fi

SCRIPT_DIR=$(CDPATH= cd -P "$(dirname "$0")" >/dev/null 2>&1 && pwd)
if [ -z "$SCRIPT_DIR" ]; then
    echo "ERROR: could not determine the pimd-prep installation directory." >&2
    exit 1
fi
PACKAGE_PARENT=$(dirname "$SCRIPT_DIR")

# Make the adjacent prep package importable without installation.
if [ -n "${PYTHONPATH:-}" ]; then
    PYTHONPATH="$PACKAGE_PARENT:$PYTHONPATH"
else
    PYTHONPATH="$PACKAGE_PARENT"
fi
export PYTHONPATH

exec python3 -m prep "$@"
