#!/bin/sh -e
# Build xpopple against various versions of poppler.

parallel="-j8"

rm -f xpopple-*.tar.gz
make dist

mkdir -p testdata
cd testdata
rm -fr log obj
mkdir -p archives inst log obj src
testdir=$(pwd)

rm -fr xpopple-* xpopple
tar xf ../xpopple-*.tar.gz
mv xpopple-* xpopple

status () {
	echo >&2 " ==> $1"
}

build_poppler () {
	version="$1"
	ver1=$(echo "$version" | cut -d. -f1)
	ver2=$(echo "$version" | cut -d. -f2)
	vernum=$(expr '(' "$ver1" '*' 1000 ')' '+' "$ver2")
	src="$testdir/src/poppler-$version"
	prefix="$testdir/inst/poppler-$version"

	if [ -e "$prefix/built" ]; then
		return
	fi

	tarball="$testdir/archives/poppler-$version.tar.xz"
	if [ ! -e "$tarball" ]; then
		if [ "$vernum" -ge 23 ]; then
			ext=".tar.xz"
		else
			ext=".tar.gz"
		fi
		url="https://poppler.freedesktop.org/poppler-$version$ext"
		status "Downloading $url"
		wget -O "$tarball" "$url"
	fi

	status "Extracting $tarball"
	rm -fr "$src"
	tar -C "$testdir/src" -xf "$tarball"

	# nss.h is provided by libc; if the build system happens to have
	# -I/usr/include earlier in the path it'll find that instead.
	shh="$src/poppler/SignatureHandler.h"
	if [ -f "$shh" ]; then
		sed -i -e 's,<nss.h>,<nss/nss.h>,' "$shh"
	fi

	status "Building $src"
	if [ "$vernum" -ge 60 ]; then
		obj="$src/obj"
		mkdir -p "$obj"
		(cd "$obj" && cmake \
			-DCMAKE_BUILD_TYPE=RelWithDebInfo \
			-DCMAKE_INSTALL_PREFIX="$prefix" \
			-DCMAKE_INSTALL_LIBDIR="$prefix/lib" \
			-DENABLE_XPDF_HEADERS=ON \
			-DENABLE_UNSTABLE_API_ABI_HEADERS=ON \
			-DENABLE_CPP=OFF \
			-DENABLE_UTILS=OFF \
			-DENABLE_GLIB=OFF \
			-DENABLE_QT4=OFF \
			-DENABLE_QT5=OFF \
			-DENABLE_QT6=OFF \
			-DBUILD_GTK_TESTS=OFF \
			-DBUILD_QT4_TESTS=OFF \
			-DBUILD_QT5_TESTS=OFF \
			-DBUILD_QT6_TESTS=OFF \
			.. \
		)
	else
		obj="$src"
		(cd "$src" && ./configure \
			--prefix="$prefix" \
			--enable-xpdf-headers \
			--disable-poppler-cpp \
			--disable-utils \
			--disable-poppler-qt4 \
			--disable-poppler-qt5 \
			--disable-gtk-test \
		)
	fi

	make -C "$obj" $parallel install
	make -C "$obj" clean
	touch "$prefix/built"
}

build_with () {
	poppler="$1"
	build_poppler "$poppler"

	poppler_prefix="$testdir/inst/poppler-$poppler"
	id="xpopple+poppler-$poppler"
	obj="$testdir/obj/$id"
	prefix="$testdir/obj/inst-$id"

	status "Building in $obj"
	rm -fr "$obj"
	mkdir -p "$obj"
	(
		cd "$obj"
		export PKG_CONFIG_PATH="$poppler_prefix/lib/pkgconfig:$PKG_CONFIG_PATH"
		echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH"

		# We must force the -I here because Poppler's SplashBitmap.h
		# does #include "poppler/GfxState.h" -- and that'll find
		# the system version if -I/usr/include happens to be earlier.
		cflags="-Wall -I$poppler_prefix/include"

		../../xpopple/configure \
			--prefix="$prefix" \
			CC="gcc $cflags" \
			CXX="g++ $cflags $cxxflags" \
			&& \
		make -k $parallel && \
		make $parallel install && \
		make clean && \
		echo "XPOPPLE-ok" || echo "XPOPPLE-FAIL"
	) 2>&1 | tee "$testdir/log/$id.log"

	rm -fr "$prefix"
}

poppler_versions="
	0.19.4
	0.20.5
	0.21.4
	0.22.5
	0.23.4
	0.24.5
	0.25.3
	0.26.5
	0.28.1
	0.29.0
	0.30.0
	0.31.0
	0.32.0
	0.33.0
	0.34.0
	0.35.0
	0.36.0
	0.37.0
	0.38.0
	0.39.0
	0.40.0
	0.41.0
	0.42.0
	0.43.0
	0.44.0
	0.45.0
	0.46.0
	0.47.0
	0.48.0
	0.49.0
	0.50.0
	0.51.0
	0.52.0
	0.53.0
	0.54.0
	0.55.0
	0.56.0
	0.57.0
	0.58.0
	0.59.0
	0.60.1
	0.61.1
	0.62.0
	0.63.0
	0.64.0
	0.65.0
	0.66.0
	0.67.0
	0.68.0
	0.69.0
	0.70.1
	0.71.0
	0.72.0
	0.73.0
	0.74.0
	0.75.0
	0.76.1
	0.77.0
	0.78.0
	0.79.0
	0.80.0
	0.81.0
	0.82.0
	0.83.0
	0.84.0
	0.85.0
	0.86.0
	0.86.1
	0.87.0
	0.88.0
	0.89.0
	0.90.1
	20.08.0
	20.09.0
	20.10.0
	20.11.0
	20.12.1
	21.01.0
	21.02.0
	21.03.0
	21.04.0
	21.05.0
	21.06.1
	21.07.0
	21.08.0
	21.09.0
	21.10.0
	21.11.0
	21.12.0
	22.01.0
	22.02.0
	22.03.0
	22.04.0
	22.05.0
	22.06.0
	22.07.0
	22.08.0
	22.09.0
	22.10.0
	22.11.0
	22.12.0
	23.01.0
	23.02.0
	23.03.0
	23.04.0
	23.05.0
	23.06.0
	23.07.0
	23.08.0
	23.09.0
	23.10.0
	23.11.0
	23.12.0
	24.01.0
	24.02.0
	24.03.0
	24.04.0
	24.05.0
	24.06.0
	24.06.1
	24.07.0
	24.08.0
	24.09.0
	24.10.0
	24.11.0
	24.12.0
	25.01.0
	25.02.0
	25.03.0
"
if [ "$#" -gt 0 ]; then
	poppler_versions="$*"
fi
for poppler_v in $poppler_versions; do \
	build_with "$poppler_v"
done

status "Summary"
grep '^XPOPPLE-' "$testdir"/log/*.log
