#!/bin/bash
set -e

pkg=libgiftiio0

export LC_ALL=C.UTF-8
if [ "${AUTOPKGTEST_TMP}" = "" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/${pkg}-test.XXXXXX)
  trap "rm -rf ${AUTOPKGTEST_TMP}" 0 INT QUIT ABRT PIPE TERM
fi

cp -a /usr/share/doc/${pkg}/examples/* "${AUTOPKGTEST_TMP}"

cd "${AUTOPKGTEST_TMP}"

# Note the .gii file is an xml file recording the endianness of the system
# on which it has been created, thus altering the check sum.  Other values
# should not change.
BYTE_ORDER="$(lscpu | sed -n 's/^Byte Order: \+//p')"
if [ "$BYTE_ORDER" = "Little Endian" ]
then
	echo "0e12f6982c15198137cde8def420f9d5  created.gii" >> checksums
elif [ "$BYTE_ORDER" = "Big Endian" ]
then
	echo "aec3bec6d80e76f10b4e33f6470cc3cb  created.gii" >> checksums
else
	echo "error: unknown byte order: '$BYTE_ORDER'" >&2
	exit 1
fi

echo -e "\e[93m\e[1mRunning Tests....\e[0m"

gifti_test -infile new.gii -show

gifti_tool -new_dset -new_numDA 3 -new_dtype NIFTI_TYPE_INT16  -new_intent NIFTI_INTENT_TTEST  -new_ndim 2 -new_dims 5 2 0 0 0 0 -mod_add_data -write_gifti created.gii

md5sum --check checksums

rm created.gii

echo -e "\e[92m\e[1mPASS\e[0m"

