Bootstrap: docker
From: python@sha256:543d6cace00ffc96bc95d332493bb28a4332c6dd614aab5fcbd649ae8a7953d9

%labels
    Maintainer "PHAC NML"
    Description "Self-contained HyRISE runtime build (no prebuilt OCI image dependency)"
    Source "https://github.com/phac-nml/HyRISE"

%arguments
    HYRISE_PIP_SPEC=hyrise
    POSTALIGN_REF=6bcc53044ff6c73d97d3a56b0643fe6ba8bc862f
    SIERRALOCAL_REF=428ebf9e7908cdf5a5aa5ab192c11ddd457aa132
    MULTIQC_VERSION=1.33

%help
    This definition builds a HyRISE runtime directly from Python packages and
    pinned upstream tools, instead of inheriting from docker://ghcr.io/phac-nml/hyrise:latest.

    Build command:
      apptainer build hyrise.sif src/hyrise/hyrise.def

    Build behavior:
      - If this definition is built from a HyRISE source checkout, local source
        is copied and installed (no PyPI required for HyRISE itself).
      - Otherwise, HyRISE is installed from HYRISE_PIP_SPEC (default: 'hyrise').

    Optional override (for private indexes or custom specs):
      apptainer build --build-arg HYRISE_PIP_SPEC='hyrise==0.2.1' hyrise.sif src/hyrise/hyrise.def

%setup
    set -eu

    ROOTFS="${APPTAINER_ROOTFS:-${SINGULARITY_ROOTFS:-}}"
    BUILDDEF="${APPTAINER_BUILDDEF:-${SINGULARITY_DEFFILE:-}}"

    if [ -z "${ROOTFS}" ] || [ -z "${BUILDDEF}" ]; then
        exit 0
    fi

    DEF_DIR="$(dirname "${BUILDDEF}")"

    # Try common locations so extracted defs (e.g. ./container_build/hyrise.def)
    # still detect a local source checkout when building from repository root.
    REPO_ROOT=""
    for candidate in "${DEF_DIR}" "${DEF_DIR}/.." "${DEF_DIR}/../.." "$(pwd)"; do
        RESOLVED="$(cd "${candidate}" 2>/dev/null && pwd || true)"
        if [ -n "${RESOLVED}" ] && [ -f "${RESOLVED}/pyproject.toml" ] && [ -d "${RESOLVED}/src/hyrise" ]; then
            REPO_ROOT="${RESOLVED}"
            break
        fi
    done

    if [ -n "${REPO_ROOT}" ] && [ -f "${REPO_ROOT}/pyproject.toml" ] && [ -d "${REPO_ROOT}/src/hyrise" ]; then
        mkdir -p "${ROOTFS}/opt/hyrise-src"
        cp -a "${REPO_ROOT}/pyproject.toml" "${ROOTFS}/opt/hyrise-src/pyproject.toml"
        [ -f "${REPO_ROOT}/setup.py" ] && cp -a "${REPO_ROOT}/setup.py" "${ROOTFS}/opt/hyrise-src/setup.py"
        [ -f "${REPO_ROOT}/README.md" ] && cp -a "${REPO_ROOT}/README.md" "${ROOTFS}/opt/hyrise-src/README.md"
        [ -f "${REPO_ROOT}/LICENSE" ] && cp -a "${REPO_ROOT}/LICENSE" "${ROOTFS}/opt/hyrise-src/LICENSE"
        [ -f "${REPO_ROOT}/AUTHORS.md" ] && cp -a "${REPO_ROOT}/AUTHORS.md" "${ROOTFS}/opt/hyrise-src/AUTHORS.md"
        [ -f "${REPO_ROOT}/MANIFEST.in" ] && cp -a "${REPO_ROOT}/MANIFEST.in" "${ROOTFS}/opt/hyrise-src/MANIFEST.in"
        cp -a "${REPO_ROOT}/src" "${ROOTFS}/opt/hyrise-src/src"
    fi

%environment
    export PIP_NO_CACHE_DIR=1
    export PIP_DISABLE_PIP_VERSION_CHECK=1
    export PYTHONDONTWRITEBYTECODE=1
    export PYTHONUNBUFFERED=1

%post
    set -eux

    export DEBIAN_FRONTEND=noninteractive
    apt-get update
    apt-get install --yes --no-install-recommends \
        build-essential \
        git \
        ca-certificates \
        tini
    rm -rf /var/lib/apt/lists/*

    python -m pip install --upgrade pip setuptools wheel

    python -m pip install "git+https://github.com/hivdb/post-align.git@{{ POSTALIGN_REF }}"
    python -m pip install "git+https://github.com/PoonLab/sierra-local.git@{{ SIERRALOCAL_REF }}"
    if [ -f /opt/hyrise-src/pyproject.toml ] && [ -d /opt/hyrise-src/src/hyrise ]; then
        echo "Installing HyRISE from copied local source at /opt/hyrise-src"
        python -m pip install /opt/hyrise-src
    else
        echo "Installing HyRISE from Python package spec: {{ HYRISE_PIP_SPEC }}"
        python -m pip install "{{ HYRISE_PIP_SPEC }}"
    fi
    python -m pip install "multiqc=={{ MULTIQC_VERSION }}"
    python -m pip check

    mkdir -p /data /root/.config/hyrise /root/.local/share/hyrise

%runscript
    exec /usr/bin/tini -- hyrise "$@"

%startscript
    exec /usr/bin/tini -- hyrise "$@"

%test
    hyrise --help
    python -m hyrise --help
