# syntax=docker/dockerfile:1.7

ARG OPENSUSE_IMAGE=opensuse/tumbleweed
ARG DOCKER_CLI_VERSION=29-cli

FROM docker:${DOCKER_CLI_VERSION} AS docker-cli

FROM ${OPENSUSE_IMAGE} AS dev

ARG NODEJS_MAJOR=22
ARG NODEJS_REPO=auto
ARG PI_WEB_EXTRA_ZYPPER_PACKAGES=""
ARG PI_WEB_UID=1000
ARG PI_WEB_GID=1000

SHELL ["/bin/bash", "-o", "pipefail", "-c"]

ENV NODE_ENV=development \
  PATH=/workspace/node_modules/.bin:$PATH \
  NPM_CONFIG_UPDATE_NOTIFIER=false \
  NPM_CONFIG_CACHE=/data/npm-cache \
  HOME=/data/home \
  XDG_CONFIG_HOME=/data/config \
  PI_WEB_DATA_DIR=/data/pi-web \
  PI_WEB_SESSIOND_SOCKET=/data/pi-web/sessiond.sock \
  PI_CODING_AGENT_DIR=/data/pi-agent \
  HOSTEXEC_IMAGE=alpine:3.22 \
  SHELL=/bin/bash \
  TERM=xterm-256color

COPY docker/internal/image/install-opensuse-base /usr/local/sbin/install-pi-web-opensuse-base
RUN chmod 0755 /usr/local/sbin/install-pi-web-opensuse-base \
  && install-pi-web-opensuse-base

WORKDIR /workspace

COPY package.json package-lock.json ./
COPY scripts/install-git-hooks.mjs scripts/install-git-hooks.mjs
# Keep an immutable dependency seed outside /workspace, which is hidden by the
# checkout bind mount at runtime. A cached generation is added after custom
# image hooks so it identifies the final dependency tree.
RUN npm ci \
  && install -d -m 0755 /opt/pi-web-dev-dependencies \
  && cp package.json package-lock.json /opt/pi-web-dev-dependencies/ \
  && chmod -R a+rwX /workspace/node_modules \
  && mv /workspace/node_modules /opt/pi-web-dev-dependencies/node_modules \
  && ln -s /opt/pi-web-dev-dependencies/node_modules /workspace/node_modules \
  && ln -sf /opt/pi-web-dev-dependencies/node_modules/.bin/pi /usr/local/bin/pi \
  && npm cache clean --force \
  && chmod -R a+rwX /data \
  && chmod 0777 /workspace

COPY --chmod=0755 docker/internal/dev/sync-node-modules /usr/local/sbin/pi-web-dev-sync-node-modules

COPY --from=docker-cli /usr/local/bin/docker /usr/local/bin/docker
COPY --from=docker-cli /usr/local/libexec/docker/cli-plugins /usr/local/libexec/docker/cli-plugins
COPY docker/internal/bin/hostexec /usr/local/bin/hostexec
COPY docker/pi-web-docker /usr/local/bin/pi-web-docker
RUN chmod 0755 /usr/local/bin/hostexec /usr/local/bin/pi-web-docker

COPY docker/custom-image.d/ /tmp/pi-web-custom-image.d/
# Image hooks use the temporary /workspace/node_modules symlink. Leave an empty
# directory afterward so Compose can mount and populate the dependency volume.
RUN bash -euxo pipefail -c '\
  shopt -s nullglob; \
  for script in /tmp/pi-web-custom-image.d/*.sh; do \
    echo "Running PI WEB custom image hook: ${script}"; \
    bash "${script}"; \
  done; \
  rm -rf /tmp/pi-web-custom-image.d; \
  test -L /workspace/node_modules; \
  rm /workspace/node_modules; \
  install -d -m 0777 /workspace/node_modules; \
  zypper clean --all; \
  rm -rf /var/cache/zypp/* \
'
# Cache the generation with the completed seed. Changes to any preceding layer,
# including custom image hooks, rerun this step and refresh the named volume.
RUN node -e 'process.stdout.write(`${require("node:crypto").randomUUID()}\n`)' > /opt/pi-web-dev-dependencies/generation

EXPOSE 8504 8505

ENTRYPOINT ["tini", "--"]
CMD ["npm", "run", "dev"]
