# The .crate sources sit beside the tree (the driver copies files it
# can't unpack): unpack them into vendor/ as `cargo vendor` would, with
# an empty file list in each checksum (the .crate's hash was checked).
prepare() {
	mkdir vendor .cargo-home
	for c in ../*.crate; do
		tar -xzf "$c" -C vendor
		d=vendor/$(basename "$c" .crate)
		printf '{"files":{},"package":"%s"}' "$(sha256sum "$c" | cut -d' ' -f1)" \
			> "$d/.cargo-checksum.json"
	done
	cat > .cargo-home/config.toml <<-EOT
	[source.crates-io]
	replace-with = "vendored"
	[source.vendored]
	directory = "$PWD/vendor"
	[net]
	offline = true
	EOT
}

# Rust's musl target links statically by default; a shared library
# (librsvg-2.so, the loader) needs the dynamic C runtime.
export CARGO_HOME="$PWD/.cargo-home"
export RUSTFLAGS="-C target-feature=-crt-static"

# gdk-pixbuf's loaders are built in, so this is the only loader and
# librsvg ships the cache that names it (at its linked path).
package() {
	DESTDIR="$DESTDIR" meson install -C _build
	d=lib/gdk-pixbuf-2.0/2.10.0
	LD_LIBRARY_PATH="$DESTDIR$PREFIX/lib" gdk-pixbuf-query-loaders \
		"$DESTDIR$PREFIX/$d/loaders/libpixbufloader_svg.so" |
		sed "s|$DESTDIR||" > "$DESTDIR$PREFIX/$d/loaders.cache"
	grep -q '"svg"' "$DESTDIR$PREFIX/$d/loaders.cache" || die "No SVG loader in loaders.cache"
}
