#!/bin/sh

keep=no

for option; do
	case "$option" in
		--keep)
			keep=yes
			;;
	esac
done

EXEEXT=@EXEEXT@
VERSION=@GIT_HEAD_VERSION@
CC="@CC@"
CFLAGS="--std=gnu99"
srcdir="@srcdir@"

if [ $keep = "yes" ]; then
	OUTDIR="opencl_test.$VERSION"
	mkdir "$OUTDIR" || exit 1
else
	if test "x$TMPDIR" = "x"; then
		TMPDIR=/tmp
	fi
	OUTDIR=`mktemp -d $TMPDIR/ppcg.XXXXXXXXXX` || exit 1
fi

run_tests () {
	subdir=$1
	ppcg_options=$2

	echo Test with PPCG options \'$ppcg_options\'
	mkdir ${OUTDIR}/${subdir} || exit 1
	for i in $srcdir/tests/*.c; do
		echo $i
		name=`basename $i`
		name="${name%.c}"
		out_c="${OUTDIR}/${subdir}/$name.ppcg.c"
		out="${OUTDIR}/${subdir}/$name.ppcg$EXEEXT"
		options="--target=opencl --opencl-no-use-gpu $ppcg_options"
		functions="$srcdir/tests/${name}_opencl_functions.cl"
		if test -f $functions; then
			options="$options --opencl-include-file=$functions"
			options="$options --opencl-compiler-options=-I."
		fi
		./ppcg$EXEEXT $options $i -o "$out_c" || exit
		$CC $CFLAGS -I "$srcdir" "$srcdir/ocl_utilities.c" -lOpenCL \
			-I. "$out_c" -o "$out" || exit
		$out || exit
	done
}

run_tests default
run_tests embed --opencl-embed-kernel-code

for i in $srcdir/examples/*.c; do
	echo $i
	name=`basename $i`
	name="${name%.c}"
	exe_ref="${OUTDIR}/$name.ref$EXEEXT"
	gen_ocl="${OUTDIR}/$name.ppcg.c"
	exe_ocl="${OUTDIR}/$name.ppcg$EXEEXT"
	output_ref="${OUTDIR}/$name.ref.out"
	output_ocl="${OUTDIR}/$name.ppcg.out"
	$CC $CFLAGS $i -o $exe_ref || exit
	./ppcg$EXEEXT --target=opencl --opencl-no-use-gpu $i -o "$gen_ocl" || \
		exit
	$CC $CFLAGS -I "$srcdir" "$srcdir/ocl_utilities.c" -lOpenCL \
		"$gen_ocl" -o "$exe_ocl" || exit
	$exe_ref > $output_ref || exit
	$exe_ocl > $output_ocl || exit
	cmp $output_ref $output_ocl || exit
done

if [ $keep = "no" ]; then
	rm -r "${OUTDIR}"
fi
