# agentbox image: a container with SSH, a VNC desktop (XFCE), Firefox and the tools of agentbox.toml.
# Built by `agentbox build`: BASE_IMAGE, PACKAGES and APT_REPOS come from the user's agentbox.toml;
# the build context is the package's assets/ directory plus tools/ (the [[tools]] scripts, generated).

ARG BASE_IMAGE=docker.io/library/ubuntu:26.04
FROM ${BASE_IMAGE}

ENV DEBIAN_FRONTEND=noninteractive \
    LANG=en_US.UTF-8 \
    LC_ALL=en_US.UTF-8 \
    NPM_CONFIG_UPDATE_NOTIFIER=false \
    NPM_CONFIG_FUND=false

# RUN instructions use /bin/sh: podman builds OCI images, which ignore the SHELL instruction.
# Keep RUN lines POSIX; anything that needs bash goes into a script run with `bash script.sh`.

# Build args are declared right before the step that uses them, so that changing
# one only invalidates the build cache from that step on.

# 0. apt mirror ([build] apt_mirror in the config; empty = the base image's default sources)
ARG APT_MIRROR=""
COPY scripts/set_apt_mirror.sh /opt/agentbox/scripts/
RUN bash /opt/agentbox/scripts/set_apt_mirror.sh

# 1. Base packages: openssh-server / tigervnc / XFCE desktop / sudo
COPY base.list /opt/agentbox/base.list
COPY scripts/apt_update.sh scripts/install_pkg_list.sh scripts/install_base.sh /opt/agentbox/scripts/
RUN bash /opt/agentbox/scripts/install_base.sh /opt/agentbox/base.list

# 2. Firefox from Mozilla's APT repository (the distribution's package is a snap wrapper)
COPY scripts/install_firefox.sh /opt/agentbox/scripts/
RUN bash /opt/agentbox/scripts/install_firefox.sh

# 3. Create the user (UID/GID match the host, for userns keep-id)
ARG CONTAINER_USER=agent
ARG USER_UID=1000
ARG USER_GID=1000
COPY scripts/create_user.sh /opt/agentbox/scripts/
RUN bash /opt/agentbox/scripts/create_user.sh

# 4. Third-party apt repositories ([[apt_repos]]) and extra packages (packages) from agentbox.toml
ARG APT_REPOS=""
ARG PACKAGES=""
COPY scripts/add_apt_repos.sh scripts/install_packages.sh /opt/agentbox/scripts/
RUN bash /opt/agentbox/scripts/add_apt_repos.sh \
 && bash /opt/agentbox/scripts/install_packages.sh ${PACKAGES}

# 5. [[tools]] with run_as = "root", then those with run_as = "user" (in config order)
COPY scripts/install_tools.sh /opt/agentbox/scripts/
COPY tools/root/ /opt/agentbox/tools/root/
RUN bash /opt/agentbox/scripts/install_tools.sh root /opt/agentbox/tools/root
COPY tools/user/ /opt/agentbox/tools/user/
RUN bash /opt/agentbox/scripts/install_tools.sh user /opt/agentbox/tools/user

# 6. Defaults for ~/.claude and ~/.codex (they are usually bind-mounted, which hides the image's copy)
COPY scripts/save_defaults.sh /opt/agentbox/scripts/
RUN bash /opt/agentbox/scripts/save_defaults.sh

# 7. Runtime files: sshd config, entrypoint, VNC xstartup
COPY rootfs/ /
COPY vnc/xstartup /opt/agentbox/vnc/xstartup
RUN chmod 755 /usr/local/bin/entrypoint.sh /opt/agentbox/vnc/xstartup \
 && chmod 644 /etc/ssh/sshd_config.d/10-agentbox.conf \
 && mkdir -p /etc/agentbox \
 && echo "AGENTBOX_USER=${CONTAINER_USER}" > /etc/agentbox/container.env \
 && chmod 644 /etc/agentbox/container.env

LABEL agentbox.user="${CONTAINER_USER}" \
      agentbox.uid="${USER_UID}" \
      agentbox.gid="${USER_GID}"

WORKDIR /home/${CONTAINER_USER}

EXPOSE 22 5901

ENTRYPOINT ["/usr/local/bin/entrypoint.sh"]
