#!/bin/sh
# Copyright 2016 Ghislain Antony Vaillant
#
# This file is part of the packaging testsuite for ciftilib.

set -e

# Prepare the testsuite.
cp -a /usr/share/doc/libcifti-dev/examples/* "$AUTOPKGTEST_TMP"
cp ./cmake/scripts/testmd5.cmake "$AUTOPKGTEST_TMP"
cd "$AUTOPKGTEST_TMP"

# Setup the CMake project.
rm -f CMakeLists.txt
cat << EOF > CMakeLists.txt
cmake_minimum_required(VERSION 2.8.12)
project(ciftilib-tests)

enable_testing()

find_package(PkgConfig)
pkg_check_modules(PC_CIFTI REQUIRED CiftiLib)
add_library(PkgConfig::Cifti INTERFACE IMPORTED)
set_target_properties(PkgConfig::Cifti PROPERTIES
	INTERFACE_INCLUDE_DIRECTORIES "\${PC_CIFTI_INCLUDE_DIRS}"
	INTERFACE_LINK_LIBRARIES "\${PC_CIFTI_LIBRARIES}"
	INTERFACE_COMPILE_OPTIONS "\${PC_CIFTI_CFLAGS_OTHER}")

add_executable(rewrite rewrite.cxx)
target_link_libraries(rewrite PkgConfig::Cifti)

add_executable(xmlinfo xmlinfo.cxx)
target_link_libraries(xmlinfo PkgConfig::Cifti)

set(cifti_files Conte69.MyelinAndCorrThickness.32k_fs_LR.dscalar.nii
	Conte69.MyelinAndCorrThickness.32k_fs_LR.dtseries.nii
	Conte69.MyelinAndCorrThickness.32k_fs_LR.ptseries.nii
	Conte69.parcellations_VGD11b.32k_fs_LR.dlabel.nii
	ones.dscalar.nii)

list(LENGTH cifti_files num_cifti_files)
math(EXPR loop_end "\${num_cifti_files} - 1")

foreach(index RANGE \${loop_end})
	list(GET cifti_files \${index} testfile)
	add_test(info-\${testfile} xmlinfo \${CMAKE_CURRENT_SOURCE_DIR}/data/\${testfile})
	add_test(rewrite-little-\${testfile} rewrite \${CMAKE_SOURCE_DIR}/data/\${testfile} little-\${testfile} LITTLE)
	add_test(rewrite-big-\${testfile} rewrite \${CMAKE_SOURCE_DIR}/data/\${testfile} big-\${testfile} BIG)
endforeach(index RANGE \${loop_end})

EOF

# Configure, build and execute.
mkdir build && cd build
cmake .. && make && make test
