# Contributor: Celeste <cielesti@protonmail.com>
# Maintainer: Celeste <cielesti@protonmail.com>
pkgname=soupault
pkgver=4.10.0
pkgrel=0
pkgdesc="Static website generator based on HTML element tree rewriting"
url="https://soupault.app/"
arch="all"
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
	}

	[ -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="
5c29333bf90874f0cf3d6d9ac7b27a8428c9174bb05e5a173a2e486783ad57a073e26b0a360ca066d7ec7d65f7289a8029da743241005411d9d4bae71966b555  soupault-4.10.0.tar.gz
f5ec0e01e5fe5a1c50b7205dca3512a632452bbeafd67c6aa206e1847f5a4fe55a86644d6fccf55714d1a2614e859121dda85d3c75882e4ca04220c314a015c0  soupault.opam.locked
"
