#! /usr/bin/env bash
################################################################################

################################################################################

cmd_dir=$(dirname "$0") || exit 1
source "$cmd_dir"/base_utilities || exit 1
source "$cmd_dir"/test_utilities || exit 1

debug_level="${JAS_DEBUG_LEVEL:-0}"
if [ "$debug_level" -ge 1 ]; then
	set -xv
	imginfo_debug_level=20
fi
#imginfo_debug_level=20

################################################################################

set_source_and_build_dirs || panic "cannot set source and build directories"

imginfo="$abs_top_builddir/src/app/imginfo"
jasper="$abs_top_builddir/src/app/jasper"
export IMGINFO_COMMAND="$imginfo"
export JASPER_COMMAND="$jasper"

data_dir="$abs_top_srcdir/data"

################################################################################

verbose=0
if [ $# -ge 1 ]; then
	verbose=1
fi

################################################################################

formats=($("$jasper" --enable-all-formats --list-enabled-formats)) || \
  panic "cannot get supported formats"
if [ "$verbose" -ge 2 ]; then
	echo "The following formats are supported:"
	for format in "${formats[@]}"; do
		echo "    $format"
	done
fi
has_jpg=$(is_supported_format jpg) || \
  panic "cannot determine if JPG is supported format"
has_mif=$(is_supported_format mif) || \
  panic "cannot determine if MIF is supported format"
has_png=$(is_supported_format png) || \
  panic "cannot determine if PNG is supported format"
if [ "$verbose" -ge 1 ]; then
	echo "JPG: $has_jpg"
	echo "MIF: $has_mif"
	echo "PNG: $has_png"
fi

failed_tests=()

image_name=
while IFS= read -r line; do
	if [ -z "$line" ]; then
		continue
	fi

	file=$(awk '{print $1;}' <<< "$line") || panic "cannot get file"

	expected_format=$(awk '{print $2;}' <<< "$line") || \
	  panic "cannot get expected format"
	expected_num_components=$(awk '{print $3;}' <<< "$line") || \
	  panic "cannot get expected number of components"
	expected_width=$(awk '{print $4;}' <<< "$line") || \
	  panic "cannot get expected width"
	expected_height=$(awk '{print $5;}' <<< "$line") || \
	  panic "cannot get expected height"
	expected_depth=$(awk '{print $6;}' <<< "$line") || \
	  panic "cannot get expected depth"
	expected_size=$(awk '{print $7;}' <<< "$line") || \
	  panic "cannot get expected size"

	skip=0
	case "$expected_format" in
	jpg)
		if [ "$has_jpg" -eq 0 ]; then
			skip=1
		fi
		;;
	mif)
		if [ "$has_mif" -eq 0 ]; then
			skip=1
		fi
		;;
	esac
	if [ "$skip" -ne 0 ]; then
		echo "SKIPPING: unsupported format ($expected_format)"
		continue
	fi

	imginfo_options=()
	imginfo_options+=(--enable-all-formats)
	if [ -n "$imginfo_debug_level" ]; then
		imginfo_options+=(--debug-level "$imginfo_debug_level")
	fi
	image_file="$data_dir/$file"
	if [ "$verbose" -ge 2 ]; then
		echo "Running $imginfo < $image_file"
	fi
	buffer=$("$imginfo" "${imginfo_options[@]}" < "$image_file")
	status=$?
	if [ "$status" -ne 0 ]; then
		failed_tests+=("$file")
		echo "imginfo failed for $file ($status)"
		continue
	fi
	actual_format=$(awk '{print $1;}' <<< "$buffer") || \
	  actual_format=
	actual_num_components=$(awk '{print $2;}' <<< "$buffer") || \
	  actual_num_components=
	actual_width=$(awk '{print $3;}' <<< "$buffer") || \
	  actual_width=
	actual_height=$(awk '{print $4;}' <<< "$buffer") || \
	  actual_height=
	actual_depth=$(awk '{print $5;}' <<< "$buffer") || \
	  actual_depth=
	actual_size=$(awk '{print $6;}' <<< "$buffer") || \
	  actual_size=
	if [ -z "$actual_format" -o \
	  -z "$actual_num_components" -o \
	  -z "$actual_width" -o \
	  -z "$actual_height" -o \
	  -z "$actual_depth" -o \
	  -z "$actual_size" \
	  ]; then
		failed_tests+=("$file")
		echo "cannot parse imginfo output for $file"
		continue
	fi
	if [ "$actual_format" != "$expected_format" -o \
	  "$actual_num_components" -ne "$expected_num_components" -o \
	  "$actual_width" -ne "$expected_width" -o \
	  "$actual_height" -ne "$expected_height" -o \
	  "$actual_depth" -ne "$expected_depth" -o \
	  "$actual_size" -ne "$expected_size" \
	  ]; then
		echo "FAILED TEST: $file"
		echo "   EXPECTED: $expected_format $expected_num_components $expected_width $expected_height $expected_depth $expected_size"
		echo "     ACTUAL: $buffer"
		failed_tests+=("$file")
	fi

done <<< "

images/example.mif mif 2 32 64 8 4096
images/feep2.pnm pnm 1 24 7 1 21
images/feep.pnm pnm 1 24 7 1 21
images/goldenears.bmp bmp 3 128 96 8 36864
images/goldenears_gray.jpg jpg 1 128 96 8 12288
images/goldenears_gray.ras ras 1 128 96 8 12288
images/goldenears.jpg jpg 3 128 96 8 36864
images/goldenears.pnm pnm 3 128 96 8 36864
images/goldenears.ras ras 3 128 96 8 36864
images/signed_12.pgx pgx 1 8 8 12 96
images/signed_16.pgx pgx 1 8 8 16 128
images/signed_8.pgx pgx 1 8 8 8 64
images/small_16x1.pnm pnm 1 16 1 8 16
images/small_1x1.pnm pnm 1 1 1 8 1
images/stawamuschief_gray.pnm pnm 1 199 149 8 29651
images/stawamuschief.pnm pnm 3 199 149 8 88953
images/test.pnm pnm 1 8 8 12 96

test/good/105-PoC1.jp2 jp2 2 1 32 8 288
test/good/109-PoC.jp2 jp2 1 32 32 4 512
test/good/jasper-nullptr-jpc_pi_destroy.jp2 jpc 1 20 20 8 400
test/other/pr325-bad-icc-profile.jp2 jp2 3 400 300 8 360000

"

if [ "${#failed_tests[@]}" -gt 0 ]; then
	echo "The following tests failed:"
	for failed_test in "${failed_tests[@]}"; do
		echo "    $failed_test"
	done
	panic "one or more tests failed"
fi
