# Maintainer: bigbes <bigbes@gmail.com>
#
# Built by builds.sr.ht (.build.yml) and published to our own apk repo at
# repo.bigb.es/alpine/v3.22/bigbes. The srht deployment installs it from there
# instead of cloning and compiling this repo inside its Dockerfile.
#
# pkgver is READ FROM THE ENVIRONMENT, not sed-ed into this file. The `version`
# task of .build.yml runs one `git describe` and exports PKGVER as whichever of
# three shapes that produces: the tag (`0.2.0`), the tag plus commits since it
# (`0.2.0_git7`, because Alpine's grammar rejects `v0.2.0-7-gabc1234` and `_git`
# sorts after the release), or `0.0.<commit count>` when there are no tags at
# all — which is this repository today. Do not restate one of the three here as
# if it were the rule; see docs/ci.md#version.
#
# Nothing rewrites a tracked file, and that is not a style choice: Go records
# vcs.modified in every binary it builds inside a repository, it reads that flag
# from `git status --porcelain`, and a CI task that patched this line would
# therefore stamp "+dirty" into every packaged binary for the life of the apk.
# Measured on go1.26.5; .gitignore covers the rest of what abuild writes into
# the checkout.
#
# A local `abuild` has no PKGVER and builds 0.0.0, which is what a package built
# by hand honestly is.
pkgname=diff.sr.ht
pkgver="${PKGVER:-0.0.0}"
pkgrel=0
pkgdesc="Stateless diff/compare viewer for a sourcehut instance"
url="https://sourcecraft.dev/bigbes/sr-ht-compare"
arch="x86_64"
license="MIT"
# !check      — the suites are run by the `test` task of .build.yml, which runs
#               BEFORE this package is built. They do NOT need a live git.sr.ht
#               API — that claim was here for a year and was never true: every
#               suite in this tree is hermetic (gitx/fixture_test.go builds a
#               bare repo with the local git, web/web_test.go is httptest plus
#               ecoretest). This option is therefore a promise that they ran
#               elsewhere, not a statement that they are unrunnable, and it is
#               only as good as that task staying fatal.
# !tracedeps  — CGO_ENABLED=0, so there are no shared-object deps to trace
options="!check !tracedeps"

# No source= : CI builds the checkout it was handed, so abuild works in place
# rather than fetching a tarball. builddir points at the repo root, which is the
# directory holding this APKBUILD.
source=""
builddir="$startdir"

build() {
	cd "$builddir"
	# abuild redirects GOCACHE into $tmpdir (wiped after packaging), and an
	# upstream typo makes GOMODCACHE follow GOCACHE's value rather than its
	# own. Re-pin both to the stable home locations here — after abuild's own
	# exports — so CI's cache_restore/cache_save tasks see them survive.
	export GOCACHE="$HOME/.cache/go-build"
	export GOMODCACHE="$HOME/go/pkg/mod"
	# CSS strictly before the binary: web/ go:embed-s static/, so a stylesheet
	# built afterwards would never make it into the binary. The shared scss
	# partials are assembled at ASSETS/scss by the `scss` task of .build.yml —
	# no apk ships them.
	#
	# The frontend bundle is NOT built here: web/static/bundle.<hash>.js is
	# committed, and building it would want npm and a network. `make bundle` is
	# an upgrade step, run by hand.
	make css ASSETS=/usr/share/sourcehut

	# web/static/main.min.<hash>.css is still COMMITTED in this revision, and
	# `make css` has just replaced it. These two lines are how the first CI run
	# reports whether the pipeline reproduces the committed bytes: a clean
	# `git status` here means the committed file can be dropped from the tree,
	# and anything else names the difference. See docs/ci.md#css.
	#
	# `|| true` because a diagnostic must never be the reason a build fails.
	git status --porcelain -- web/static || true
	sha256sum web/static/main.min.*.css || true

	# -modcacherw matters beyond convenience: without it the module cache is
	# extracted read-only, and the CI cache tarball made from it can't be
	# unpacked on the next build (mkdir into 0555 dirs fails).
	CGO_ENABLED=0 make build GOFLAGS="-trimpath -modcacherw"

	# `go build` succeeds perfectly well with an unstyled static/ — //go:embed
	# takes the directory, not the file — so a `make css` that produced nothing
	# would ship an unstyled service and fail nothing. This is the gate, and it
	# runs before anything is staged.
	make check-css
}

package() {
	cd "$builddir"
	# The Makefile honours DESTDIR; ASSETS must stay the real runtime path so
	# the binary's static-dir glob resolves after install.
	#
	# `install-files` and not `install`, because this function must not compile.
	# abuild runs package() in a FRESH abuild process under fakeroot, which
	# re-sources this file and never calls build() — so the GOCACHE/GOMODCACHE
	# pins above are gone here, $(BIN) is .PHONY and `install` depends on
	# `build`, and `make install` would therefore relink the binary from a cold
	# cache. The apk would then ship a second compilation that nothing in this
	# file has looked at. `install -Dm755` on a missing comparesrht is a fatal
	# error naming the file, so a package() reached without a build() says so.
	make install-files DESTDIR="$pkgdir" PREFIX=/usr ASSETS=/usr/share/sourcehut
}
