# syntax=docker/dockerfile:1.7
# =============================================================================
# Gitea with fhirworx theme compiled in.
#
# Clones go-gitea/gitea at the pinned tag, drops our theme CSS into
# web_src/css/themes/, then runs Gitea's own webpack+go build so the theme is
# baked into `bindata`. Runtime stage is the same alpine+dumb-init layout as
# the upstream rootless image.
# =============================================================================

ARG GITEA_VERSION=v1.25.4
# BUILD_TAG must be unique per build; defaults to current epoch when unset.
# Gitea uses main.Version to construct the `?v=...` query string on every
# asset URL. Without changing it, browsers cache theme-fhirworx.css for 6h
# (Cache-Control: max-age=21600) — so theme edits never reach the user
# until the URL key changes. Setting it per-build forces every cache layer
# (browser, CDN, Cloudflare) to refetch.
ARG BUILD_TAG

# ---- build stage ------------------------------------------------------------
FROM docker.io/library/golang:1.25-alpine3.22 AS build
ARG GITEA_VERSION
ARG BUILD_TAG
ENV GOPROXY=https://proxy.golang.org,direct \
    GOSUMDB=sum.golang.org \
    TAGS="bindata timetzdata sqlite sqlite_unlock_notify" \
    CGO_ENABLED=1

RUN apk add --no-cache build-base git nodejs npm \
    && npm install -g pnpm@10

WORKDIR /src
RUN git clone --depth 1 --branch ${GITEA_VERSION} \
    https://github.com/go-gitea/gitea.git . \
    && git log -1 --format='%H %s'

# Drop fhirworx theme into Gitea's theme dir before the frontend build so
# webpack/bindata pick it up.
COPY theme/theme-fhirworx.css      web_src/css/themes/theme-fhirworx.css
COPY theme/theme-fhirworx-dark.css web_src/css/themes/theme-fhirworx-dark.css

# Replace Gitea's source SVGs with fhirworx branding. tools/generate-images.ts
# reads these to produce logo.svg/.png, favicon.svg/.png, apple-touch-icon.png,
# avatar_default.png — all baked into bindata.
COPY brand/logo.svg     assets/logo.svg
COPY brand/favicon.svg  assets/favicon.svg

# Full build. Order matters: deps → generate-images (uses our brand SVGs) →
# webpack frontend → go binary with bindata embedding everything in public/.
RUN --mount=type=cache,target=/root/.cache/go-build \
    --mount=type=cache,target=/go/pkg/mod \
    --mount=type=cache,target=/root/.local/share/pnpm/store \
    make deps-frontend \
 && make generate-images \
 && BUILD_TAG="${BUILD_TAG:-$(date +%s)}" \
    && export LDFLAGS="-X 'main.Version=${GITEA_VERSION}-fhirworx.${BUILD_TAG}'" \
    && echo "Building with version: ${GITEA_VERSION}-fhirworx.${BUILD_TAG}" \
    && make clean-all build LDFLAGS="${LDFLAGS}" \
 && go build contrib/environment-to-ini/environment-to-ini.go

# Upstream rootless overlay files (entrypoint, setup, gitea wrapper).
RUN chmod 755 docker/rootless/usr/local/bin/docker-entrypoint.sh \
              docker/rootless/usr/local/bin/docker-setup.sh \
              docker/rootless/usr/local/bin/gitea \
              /src/gitea \
              /src/environment-to-ini

# ---- runtime stage ----------------------------------------------------------
FROM docker.io/library/alpine:3.22
LABEL org.opencontainers.image.source="https://github.com/go-gitea/gitea"
LABEL fhirworx.theme="fhirworx"

EXPOSE 2222 3000

RUN apk add --no-cache bash ca-certificates dumb-init gettext git curl gnupg openssh-keygen \
    && addgroup -S -g 1000 git \
    && adduser -S -H -D -h /var/lib/gitea/git -s /bin/bash -u 1000 -G git git \
    && mkdir -p /var/lib/gitea /etc/gitea \
    && chown git:git /var/lib/gitea /etc/gitea

COPY --from=build /src/docker/rootless /
COPY --from=build --chown=root:root /src/gitea /app/gitea/gitea
COPY --from=build --chown=root:root /src/environment-to-ini /usr/local/bin/environment-to-ini

USER 1000:1000
ENV GITEA_WORK_DIR=/var/lib/gitea \
    GITEA_CUSTOM=/var/lib/gitea/custom \
    GITEA_TEMP=/tmp/gitea \
    TMPDIR=/tmp/gitea \
    GITEA_APP_INI=/etc/gitea/app.ini \
    HOME=/var/lib/gitea/git

VOLUME ["/var/lib/gitea", "/etc/gitea"]
WORKDIR /var/lib/gitea
ENTRYPOINT ["/usr/bin/dumb-init", "--", "/usr/local/bin/docker-entrypoint.sh"]
CMD []
