#!/bin/bash

# Barcode Writer in Pure PostScript
# https://bwipp.terryburton.co.uk
#
# Copyright (c) 2004-2025 Terry Burton

set -e
set -u
set -o pipefail

TEST=$(dirname "$0")

if gs --version | grep -q '^7\.'; then
	echo "SKIPPED GS too old"
	exit 0
fi

if gs -dBATCH -dNODISPLAY -dQUIET -c '401 string { dup } forall count =' | grep -qFx 800; then
	STRING_CONTINUE_BUG=-dSTRING_CONTINUE_BUG
else
	STRING_CONTINUE_BUG=-dNO_STRING_CONTINUE_BUG
fi

run_test() {
	local resdir=$1

	if OUT=$(cd "$resdir" && gs -sNOSAFER -dNOPAUSE -dBATCH -sDEVICE=nullpage "$STRING_CONTINUE_BUG" -P ../../../"$TEST"/test.ps 2>&1); then
		if echo "$OUT" | grep -qFx 'PASS'; then
			if WARNINGS=$(echo "$OUT" | grep -F 'leak'); then
				echo "WARN"
				echo
				echo "$WARNINGS"
				echo
				return 1
			fi
			return 0
		fi
	fi

	echo "FAIL"
	echo
	echo "$OUT"
	echo
	return 1
}

echo -n "Running tests/ps_tests (unpackaged): "
run_test build/resource/Resource unpackaged
echo "PASS"

echo -n "Running tests/ps_tests (packaged): "
run_test build/packaged_resource/Resource packaged
echo "PASS"
