#/bin/bash

set -u
set -e
trap onexit INT
trap onexit TERM
trap onexit EXIT

onexit()
{
	if [ -d $OUTDIR ]; then
		rm -rf $OUTDIR
	fi
}

runme()
{
	echo \*\*\* $* | sed "s@$EXEDIR@\$EXEDIR@g" | sed "s@$IMGDIR@\$IMGDIR@g" |
		sed "s@$JAVA@\$JAVA@g" | sed "s@$OUTDIR@\$OUTDIR@g"
	"$@"
}

IMGDIR=@CMAKE_SOURCE_DIR@/testimages
OUTDIR=`mktemp -d /tmp/__tjdecomptest_output.XXXXXX`
EXEDIR=@CMAKE_BINARY_DIR@
JAVA="@Java_JAVA_EXECUTABLE@"
JAVAARGS="-cp $EXEDIR/java/turbojpeg.jar -Djava.library.path=$EXEDIR"
TJDECOMP=$EXEDIR/tjdecomp
JAVAARG=

if [ -d $OUTDIR ]; then
	rm -rf $OUTDIR
fi
mkdir -p $OUTDIR

while [ $# -gt 0 ]; do
	case "$1" in
	-java)
		JAVAARG=-java
		TJDECOMP="$JAVA $JAVAARGS TJDecomp"
		;;
	esac
	shift
done

exec >$EXEDIR/test/tjdecomptest$JAVAARG.log

echo "EXEDIR: $EXEDIR"
echo "IMGDIR: $IMGDIR"
echo "OUTDIR: $OUTDIR"
echo

SUBSAMPOPT=(444 422 440 420 411 441 410)
SAMPOPT=(1x1 2x1 1x2 2x2 4x1 1x4 4x2)

for precision in 8 12; do
	if [ $precision -le 8 ]; then
		RGBIMG=$IMGDIR/testorig.ppm
		GRAYIMG=$IMGDIR/testorig.pgm
	else
		RGBIMG=$IMGDIR/monkey16.ppm
		GRAYIMG=$IMGDIR/monkey16.pgm
	fi

	for sampi in {0..6}; do
		runme $EXEDIR/cjpeg -pre $precision -sa ${SAMPOPT[$sampi]} \
			-outf $OUTDIR/`basename $RGBIMG .ppm`-${SUBSAMPOPT[$sampi]}.jpg $RGBIMG
	done
	runme $EXEDIR/cjpeg -pre $precision \
		-outf $OUTDIR/`basename $GRAYIMG .pgm`-gray.jpg $GRAYIMG
	echo

	for subsamp in ${SUBSAMPOPT[*]} gray; do
		for croparg in "" "-cr 14x14+23+23" "-cr 21x21+4+4" "-cr 18x18+13+13" \
			"-cr 21x21+0+0" "-cr 24x26+20+18"; do
			if [[ "$croparg" != "" && "$subsamp" = "410" ]]; then
				continue
			fi
			for scalearg in "" "-s 16/8" "-s 15/8" "-s 14/8" "-s 13/8" "-s 12/8" \
				"-s 11/8" "-s 10/8" "-s 9/8" "-s 7/8" "-s 6/8" "-s 5/8" "-s 4/8" \
				"-s 3/8" "-s 2/8" "-s 1/8"; do
				if [[ ("$scalearg" = "-s 1/8" || "$scalearg" = "-s 2/8" || \
					"$scalearg" = "-s 3/8") && "$croparg" != "" ]]; then
					continue
				fi
				for nsarg in "" "-nos"; do
					if [[ "$nsarg" = "-nos" && "$subsamp" != "422" && \
						"$subsamp" != "420" && "$subsamp" != "440" ]]; then
						continue
					fi
					for dctarg in "" "-dc fa"; do
						if [[ "$dctarg" = "-dc fa" && \
							("$scalearg" != "-s 4/8" || \
								("$subsamp" != "420" && "$subsamp" != "410")) && \
							"$scalearg" != "" ]]; then
							continue
						fi
						if [ "$subsamp" = "gray" ]; then
							basename=`basename $GRAYIMG .pgm`
							runme $TJDECOMP $croparg $dctarg $nsarg $scalearg \
								$OUTDIR/${basename}-$subsamp.jpg \
								$OUTDIR/${basename}-tjdecomp.pgm
							runme $EXEDIR/djpeg $croparg $dctarg $nsarg $scalearg \
								-outf $OUTDIR/${basename}-djpeg.pgm \
								$OUTDIR/${basename}-$subsamp.jpg
							$EXEDIR/test/md5sum $OUTDIR/${basename}-tjdecomp.pgm \
								$OUTDIR/${basename}-djpeg.pgm | sed "s@$OUTDIR@\$OUTDIR@g"
							cmp $OUTDIR/${basename}-tjdecomp.pgm \
								$OUTDIR/${basename}-djpeg.pgm
							rm $OUTDIR/${basename}-tjdecomp.pgm $OUTDIR/${basename}-djpeg.pgm
							echo

							runme $TJDECOMP $croparg $dctarg $nsarg $scalearg \
								-r $OUTDIR/${basename}-$subsamp.jpg \
								$OUTDIR/${basename}-tjdecomp.ppm
							runme $EXEDIR/djpeg $croparg $dctarg $nsarg $scalearg \
								-rg -outf $OUTDIR/${basename}-djpeg.ppm \
								$OUTDIR/${basename}-$subsamp.jpg
							$EXEDIR/test/md5sum $OUTDIR/${basename}-tjdecomp.ppm \
								$OUTDIR/${basename}-djpeg.ppm | sed "s@$OUTDIR@\$OUTDIR@g"
							cmp $OUTDIR/${basename}-tjdecomp.ppm \
								$OUTDIR/${basename}-djpeg.ppm
							rm $OUTDIR/${basename}-tjdecomp.ppm $OUTDIR/${basename}-djpeg.ppm
							echo
						else
							basename=`basename $RGBIMG .ppm`
							runme $TJDECOMP $croparg $dctarg $nsarg $scalearg \
								$OUTDIR/${basename}-$subsamp.jpg \
								$OUTDIR/${basename}-tjdecomp.ppm
							runme $EXEDIR/djpeg $croparg $dctarg $nsarg $scalearg \
								-outf $OUTDIR/${basename}-djpeg.ppm \
								$OUTDIR/${basename}-$subsamp.jpg
							$EXEDIR/test/md5sum $OUTDIR/${basename}-tjdecomp.ppm \
								$OUTDIR/${basename}-djpeg.ppm | sed "s@$OUTDIR@\$OUTDIR@g"
							cmp $OUTDIR/${basename}-tjdecomp.ppm \
								$OUTDIR/${basename}-djpeg.ppm
							rm $OUTDIR/${basename}-tjdecomp.ppm $OUTDIR/${basename}-djpeg.ppm
							echo

							if [[ "$nsarg" = "" ]]; then
								runme $TJDECOMP $croparg $dctarg $nsarg $scalearg \
									-g $OUTDIR/${basename}-$subsamp.jpg \
									$OUTDIR/${basename}-tjdecomp.pgm
								runme $EXEDIR/djpeg $croparg $dctarg $nsarg $scalearg \
									-gr -outf $OUTDIR/${basename}-djpeg.pgm \
									$OUTDIR/${basename}-$subsamp.jpg
								$EXEDIR/test/md5sum $OUTDIR/${basename}-tjdecomp.pgm \
									$OUTDIR/${basename}-djpeg.pgm | sed "s@$OUTDIR@\$OUTDIR@g"
								cmp $OUTDIR/${basename}-tjdecomp.pgm \
									$OUTDIR/${basename}-djpeg.pgm
								rm $OUTDIR/${basename}-tjdecomp.pgm \
									$OUTDIR/${basename}-djpeg.pgm
								echo
							fi
						fi
					done
				done
			done
		done
		rm $OUTDIR/${basename}-$subsamp.jpg
	done
done

for precision in {2..16}; do
	if [ $precision -le 8 ]; then
		RGBIMG=$IMGDIR/testorig.ppm
		GRAYIMG=$IMGDIR/testorig.pgm
	else
		RGBIMG=$IMGDIR/monkey16.ppm
		GRAYIMG=$IMGDIR/monkey16.pgm
	fi

	basename=`basename $RGBIMG .ppm`
	runme $EXEDIR/cjpeg -pre $precision -l 1 \
		-outf $OUTDIR/${basename}-rgb.jpg $RGBIMG
	echo

	runme $TJDECOMP $OUTDIR/${basename}-rgb.jpg \
		$OUTDIR/${basename}-tjdecomp.ppm
	runme $EXEDIR/djpeg -outf $OUTDIR/${basename}-djpeg.ppm \
		$OUTDIR/${basename}-rgb.jpg
	$EXEDIR/test/md5sum $OUTDIR/${basename}-tjdecomp.ppm \
		$OUTDIR/${basename}-djpeg.ppm | sed "s@$OUTDIR@\$OUTDIR@g"
	cmp $OUTDIR/${basename}-tjdecomp.ppm $OUTDIR/${basename}-djpeg.ppm
	rm $OUTDIR/${basename}-tjdecomp.ppm $OUTDIR/${basename}-djpeg.ppm
	rm $OUTDIR/${basename}-rgb.jpg
	echo

	basename=`basename $GRAYIMG .pgm`
	runme $EXEDIR/cjpeg -pre $precision -l 1 \
		-outf $OUTDIR/${basename}-gray.jpg $GRAYIMG
	echo

	runme $TJDECOMP $OUTDIR/${basename}-gray.jpg \
		$OUTDIR/${basename}-tjdecomp.pgm
	runme $EXEDIR/djpeg -outf $OUTDIR/${basename}-djpeg.pgm \
		$OUTDIR/${basename}-gray.jpg
	$EXEDIR/test/md5sum $OUTDIR/${basename}-tjdecomp.pgm \
		$OUTDIR/${basename}-djpeg.pgm | sed "s@$OUTDIR@\$OUTDIR@g"
	cmp $OUTDIR/${basename}-tjdecomp.pgm $OUTDIR/${basename}-djpeg.pgm
	rm $OUTDIR/${basename}-tjdecomp.pgm $OUTDIR/${basename}-djpeg.pgm
	rm $OUTDIR/${basename}-gray.jpg
	echo
done

echo "GREAT SUCCESS!"
