#!/bin/sh
# Check that find_package(NIFTI) works and that the imported targets link.
set -e

if [ -z "${AUTOPKGTEST_TMP}" ] ; then
  AUTOPKGTEST_TMP=$(mktemp -d /tmp/nifti-cmake-test.XXXXXX)
  trap 'rm -rf "${AUTOPKGTEST_TMP}"' 0 INT QUIT ABRT PIPE TERM
fi

cd "${AUTOPKGTEST_TMP}"

cat > CMakeLists.txt <<EOF
cmake_minimum_required(VERSION 4.0)
project(niftitest C)
find_package(NIFTI REQUIRED)
add_executable(niftitest niftitest.c)
target_link_libraries(niftitest NIFTI::niftiio NIFTI::nifticdf)
EOF

cat > niftitest.c <<EOF
#include <nifti1_io.h>

int main(void)
{
    nifti_image *nim = nifti_simple_init_nim();
    if (!nim)
        return 1;
    nifti_image_free(nim);
    return 0;
}
EOF

cmake -S . -B build
cmake --build build
./build/niftitest
echo PASS
