# Build the AppImage on Ubuntu 22.04 as per Readme.md
FROM ubuntu:22.04 AS builder

ARG DEBIAN_FRONTEND=noninteractive
ARG TASK_VERSION=3.44.1

# Base build deps

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
      curl \
      pkg-config build-essential cmake ninja-build \
      libglib2.0-dev chafa libchafa-dev \
      software-properties-common \
      file unzip
      

# Ensure Bun's global bin is on PATH
ENV BUN_INSTALL="/root/.bun"
ENV PATH="${BUN_INSTALL}/bin:/root/.local/bin:/root/.cargo/bin:${PATH}"

# Install uv, then the exact Meson version
RUN curl -LsSf https://astral.sh/uv/0.8.4/install.sh | sh && \
    uv --version && \
    uv tool install "meson==1.9.0" && \
    meson --version

RUN uv tool install "go-task-bin==${TASK_VERSION}" && \
    task --version

# Install Bun
RUN curl -fsSL https://bun.sh/install | bash -s "bun-v1.2.21" && \
    ln -sf /root/.bun/bin/bun /usr/local/bin/bun && \
    bun --version


WORKDIR /app
COPY . .

# Build the AppImage (retry once as per Readme note)
RUN task publish-podman || (echo "publish failed, retrying..." && task publish-podman)

# Collect any produced AppImage(s) to a stable location
RUN mkdir -p /out && \
    find /app -maxdepth 4 -type f -name "term.everything*.AppImage" -exec cp -v {} /out/ \; && \
    ls -l /out

# Minimal final stage containing only the artifacts
FROM ubuntu:22.04 AS appimage
COPY --from=builder /out /out