#!/bin/sh
set -e
scriptdir=${0%/*}
: ${PD:=/usr/bin/pd}

for t in "${@}"; do
	echo "================= building ${t} ===================="
	make -C "${t}" -f /usr/share/pd-flext/dev/Makefile.flext 2>&1

	patch=${t%/}
	patch="${scriptdir}/${patch##*/}.pd"
	if [ -n "${DEB_HOST_GNU_TYPE:-}" ]; then
		echo "cross-build detected: skip running ${t}"
		continue
	fi
	if [ ! -x "${PD}" ]; then
		echo "no runnable Pd found (${PD}): skip running ${t}"
		continue
	fi
	if [ ! -f "${patch}" ]; then
		echo "no test patch ${patch} found: skip running ${t}"
		continue
	fi

	echo "----------------- running ${t} ---------------------"
	"${PD}" -noprefs -nosound -nrt -nogui -send "pd quit 2" -path "${t}" "${patch}" 2>&1
done

