# Junipero-branded DocuSeal.
#
# Layers a stylesheet, logo files and a few small template overrides on top of
# the official image. Nothing in DocuSeal is recompiled, so upgrading is a
# matter of changing DOCUSEAL_VERSION and DOCUSEAL_DIGEST together and
# rebuilding.
#
# Licence notes (DocuSeal is AGPL-3.0 with an additional term):
#  - The "Powered by DocuSeal" attribution must stay in the UI. Nothing here
#    removes or hides it.
#  - This is a modified version, so its source (this folder) must be offered
#    to anyone who uses it over the network, signers included.

# The base is pinned by digest as well as tag, so a re-tagged upstream image
# cannot reach production unnoticed. The digest is the multi-architecture index
# for the tag: `docker buildx imagetools inspect docuseal/docuseal:<version>`.
ARG DOCUSEAL_VERSION=3.3.8
ARG DOCUSEAL_DIGEST=sha256:d67335c0cb3142818a39878ab67a0d0af2a653220e2203b042796dd1321a4a9c
FROM docuseal/docuseal:${DOCUSEAL_VERSION}@${DOCUSEAL_DIGEST}

# Stylesheet, logos, fonts and favicons. Served by the app from /app/public.
COPY --chown=docuseal:docuseal public/ /app/public/

# Template overrides: header logo, navbar title, email layout.
COPY --chown=docuseal:docuseal overrides/app/views/ /app/app/views/

# Three behaviour patches (see each file's header): PDF dates carry an explicit
# UTC marker, the combined PDF is signed once, and the PDFs carry Junipero
# branding (audit log header and signature reason).
COPY --chown=docuseal:docuseal overrides/config/initializers/ /app/config/initializers/

# Load the brand stylesheet on every page. The line is appended to DocuSeal's
# shared head template rather than replacing the file, so upstream changes to
# that template still come through on upgrade.
# The link carries a short hash of the stylesheet, because the app serves
# files under /app/public with a long cache lifetime: without it, browsers
# keep showing the old styles after a branding change.
RUN v="$(sha256sum /app/public/brand/junipero.css | cut -c1-12)" \
      && printf '\n<link rel="stylesheet" href="/brand/junipero.css?v=%s">\n' "$v" \
      >> /app/app/views/layouts/_head_tags.html.erb

# Tab title and favicons. Pages that set no title of their own fall back to
# "DocuSeal" in two upstream templates, so a default of "JuniperoSign" is put
# in front of the head template (which renders both). The favicon links get the
# same kind of hash as the stylesheet, so browsers drop the old icon. Each step
# checks that the upstream text it relies on is still there, so an upgrade that
# changes it fails the build instead of silently showing "DocuSeal" again.
RUN f=/app/app/views/layouts/_head_tags.html.erb \
      && grep -q "content_for(:html_title) ||" "$f" \
      && { printf '<%% content_for(:html_title, "JuniperoSign") unless content_for?(:html_title) %%>\n'; cat "$f"; } > /tmp/head && cat /tmp/head > "$f" && rm /tmp/head \
      && m=/app/app/views/shared/_meta.html.erb \
      && v="$(cat /app/public/favicon* /app/public/apple-* | sha256sum | cut -c1-12)" \
      && grep -q 'href="/favicon.svg"' "$m" \
      && sed -i -E "s#href=\"/(favicon[^\"]*|apple-icon-180x180\\.png)\"#href=\"/\\1?v=$v\"#" "$m" \
      && test "$(grep -c "?v=$v" "$m")" -eq 5

# Keep DocuSeal's attribution on the staff pages. Our navbar override replaces
# the "DocuSeal" name there, so the standard "Powered by DocuSeal" line is
# added as a footer to the staff layout. Signer pages already carry it.
RUN printf '\n<footer class="jx-attribution"><%%= render "shared/powered_by" %%></footer>\n' \
      >> /app/app/views/shared/_body_scripts.html.erb

# Offer the source of this modified version to everyone who uses it (AGPL
# section 13). The build context (this folder, minus .dockerignore entries) is
# packed into an archive that the attribution line links to.
COPY . /tmp/junipero-sign-source/
RUN tar -czf /app/public/brand/junipero-sign-source.tar.gz -C /tmp junipero-sign-source \
      && chown docuseal:docuseal /app/public/brand/junipero-sign-source.tar.gz \
      && rm -rf /tmp/junipero-sign-source
