# Maintainer: Celeste <cielesti@protonmail.com>
maintainer="Celeste <cielesti@protonmail.com>"
pkgname=soupault
pkgver=4.11.0
pkgrel=0
pkgdesc="Static website generator based on HTML element tree rewriting"
url="https://soupault.app/"
# loongarch64: unmet availability conditions, e.g. 'sys-ocaml-version = "5.3.0"
arch="all !loongarch64"
license="MIT"
makedepends="
	dune
	ocaml5-compiler-libs
	opam-monorepo
	"
subpackages="$pkgname-doc"
source="https://github.com/PataphysicalSociety/soupault/archive/refs/tags/$pkgver/soupault-$pkgver.tar.gz
	soupault.opam.locked
	"

export OPAMROOT="${OPAMROOT:-"$srcdir/opam-root"}"
export OPAMLOGS="${OPAMLOGS:-"$srcdir/opam-logs"}"

_opam_opts="-v -y --cli=2.2 --no-self-upgrade"
_lockfile_name="$pkgname.opam.locked"
_monorepo_opts="-v --lockfile=$_lockfile_name"

_minimal_update=true
recreate() {
	_minimal_update=false
}

opam_init() {
	opam init $_opam_opts \
		--compiler=ocaml-system \
		--disable-sandboxing \
		--no-setup
}

opam_lock() {
	[ -f "$OPAMROOT"/config ] || {
		opam_init

		opam repository $_opam_opts \
			add dune-universe \
			git+https://github.com/dune-universe/opam-overlays.git
	}

	cd "$startdir"
	[ -d "$builddir" ] || abuild unpack
	msg "Freezing $pkgname dependencies"

	# Resolve deps and update $pkgname.opam.locked, unless
	# recreate() is called, in which case create it afresh.
	(
		cd "$builddir" || {
			error 'Is $builddir set correctly?'
			return 1
		}
		opam update $_opam_opts

		if ! want_check; then
			_monorepo_opts="$_monorepo_opts --build-only"
		fi
		if $_minimal_update; then
			cp -v "$srcdir"/$_lockfile_name .

			_monorepo_opts="$_monorepo_opts --minimal-update"
		fi
		opam-monorepo lock $_monorepo_opts

		mv -v $_lockfile_name "$startdir"/
	)

	if ! abuild checksum; then
		die "Failed to update checksum, run 'abuild checksum' manually"
	fi
}

prepare() {
	default_prepare

	[ -f "$OPAMROOT"/config ] || opam_init
	ln -svf "$srcdir"/$_lockfile_name "$builddir"/
	opam-monorepo pull $_monorepo_opts --yes
}

build() {
	dune build --release --verbose
}

check() {
	dune runtest --build-dir=.testenv
}

package() {
	dune install --destdir="$pkgdir" --sections=bin,doc

	mkdir -vp "$pkgdir"/usr/share/doc/$pkgname
	cp -vr examples sample-site "$pkgdir"/usr/share/doc/$pkgname/
}

sha512sums="
0404890e42626986914e22376ee5c2998290e03164ce1a6ff4d5510633a2f6783c9b6a8f545a96222d93ab24fc057af5d8aa494d3d5320ec3bcd23fc3eedec7b  soupault-4.11.0.tar.gz
33feaeb7915a71aeb3f3cd2084ab13fe1bb1148867c864e3e3bb669b030a34d248f64aa76f29b37923853e6943787faaefbb6010f474f59f63d3480fe7146416  soupault.opam.locked
"
