# This project is designed to be built outside the source tree.
# File generated by Simon K. Warfield simon.warfield@childrens.harvard.edu
# with support from NIH grant RR021885.
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)
project(gifticlib
        VERSION 0.0.1
        DESCRIPTION "Geometry format under the Neuroimaging Informatics Technology Initiative (NIfTI)."
        LANGUAGES C)

# install destinations
set(GIFTI_INSTALL_BIN_DIR "${CMAKE_INSTALL_PREFIX}/bin")
set(GIFTI_INSTALL_LIB_DIR "${CMAKE_INSTALL_PREFIX}/lib")
set(GIFTI_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_PREFIX}/include/gifti")


option(TEST_GIFTI "Enable tests" ON)
if(${TEST_GIFTI})
    enable_testing()
endif()
option(BUILD_SHARED_LIBS "Toggle building shared libraries." ON)
add_definitions(-DHAVE_ZLIB)

find_package(ITK)
if(ITK_FOUND)
# ITK provides expat and zlib.
   include(${ITK_USE_FILE})
   set(EXPAT_LIBRARIES ITKEXPAT)
   set(ZLIB_LIBRARIES ITKznz)
   set(${NIFTI_PACKAGE_PREFIX} ITK)
else()
# Otherwise, get it from elsewhere.
  find_package(EXPAT REQUIRED)
  find_package(ZLIB REQUIRED)
  ### STEP 2 -- fetch nifti_clib immediately at configuration time, so add_subdirectory can be used.
  include(FetchContent)
  # If new or changed data is needed, add that data to the https://github.com/NIFTI-Imaging/nifti-test-data repo
  # make a new release, and then update the URL and hash (shasum -a 256 <downloaded tarball>).
  FetchContent_Declare( fetch_nifti_clib_git_repo
          GIT_REPOSITORY https://github.com/NIFTI-Imaging/nifti_clib.git
          GIT_TAG        master # or <HASH>
          SOURCE_DIR     ${CMAKE_CURRENT_LIST_DIR}/nifti_clib_download # <- not standard,  remove this line to download in binary tree
          )
  FetchContent_GetProperties(fetch_nifti_clib_git_repo)
  if(NOT fetch_nifti_clib_git_repo)
      set(FETCHCONTENT_QUIET OFF)
      message(STATUS "Downloading nifti_clib from github ... please wait")
      FetchContent_Populate( fetch_nifti_clib_git_repo )
      message(STATUS "download complete.")
      add_subdirectory(${fetch_nifti_clib_git_repo_SOURCE_DIR} ${fetch_nifti_clib_git_repo_BINARY_DIR})
  endif()
endif()

# shared library API versioning -- NOT the same as the release version
set(GIFTI_SHAREDLIB_VERSION "0.0.0")
string( REGEX MATCH "^[0-9]+" GIFTI_SHAREDLIB_SOVERSION ${GIFTI_SHAREDLIB_VERSION})
set(GIFTI_LIBRARY_PROPERTIES VERSION ${GIFTI_SHAREDLIB_VERSION} SOVERSION ${GIFTI_SHAREDLIB_SOVERSION})

add_library(giftiio gifti_io.c gifti_xml.c)
set_target_properties(giftiio PROPERTIES ${GIFTI_LIBRARY_PROPERTIES})
target_link_libraries(giftiio ${EXPAT_LIBRARIES} ${ZLIB_LIBRARIES} ${NIFTI_PACKAGE_PREFIX}nifti2)

add_executable(gifti_tool gifti_tool.c)
target_link_libraries(gifti_tool giftiio ${EXPAT_LIBRARIES} ${ZLIB_LIBRARIES} ${NIFTI_PACKAGE_PREFIX}nifti2)

add_executable(gifti_test gifti_test.c)
target_link_libraries(gifti_test giftiio)

# binaries
install(TARGETS giftiio gifti_tool gifti_test
    RUNTIME DESTINATION ${GIFTI_INSTALL_BIN_DIR} COMPONENT RuntimeLibraries
    LIBRARY DESTINATION ${GIFTI_INSTALL_LIB_DIR} COMPONENT RuntimeLibraries
    ARCHIVE DESTINATION ${GIFTI_INSTALL_LIB_DIR} COMPONENT Development)

# headers
install(FILES gifti_io.h gifti_xml.h
    DESTINATION ${GIFTI_INSTALL_INCLUDE_DIR}
    COMPONENT Development)

#######################################################################
## Add the cpack configuration settings last
option(GIFTI_USE_PACKAGING "Configure the packaging options for GIFTI" OFF)
mark_as_advanced(GIFTI_USE_PACKAGING)
if(GIFTI_USE_PACKAGING)
  include(cmake/GIFTICPackConfig.cmake)
endif()

#
# Testing

# enable_testing() sets the following variable:
if(${CMAKE_TESTING_ENABLED})
  include(CTest)
  if(CMAKE_VERSION VERSION_LESS 3.11.0)
    # CMAKE VERSION 3.11.0 needed for fetching data with cmake
    message(
      FATAL_ERROR
        "ERROR:  The testing framework for gifti_clib requires CMake version 3.11.0 or greater"
      )
  endif()

  include(FetchContent) # fetch data a configure time to simplify tests If new
                        # or changed data is needed, add that data to the
                        # https://github.com/NIFTI-Imaging/nifti-test-data repo
                        # make a new release, and then update the URL and hash
                        # (shasum -a 256 <downloaded tarball>).
  fetchcontent_declare(
    gifti_test_data
    URL
    https://github.com/NIFTI-Imaging/gifti-test-data/archive/0.0.1.tar.gz
    URL_HASH
    SHA256=9b97e830ad27a82efcacb41ea3a7c573b34b7b3ca47e0c0bfb65b44f604199c2)
  fetchcontent_getproperties(gifti_test_data)
  set(FETCHCONTENT_QUIET OFF)
  fetchcontent_populate(gifti_test_data)
endif()

set(TEST_SCRIPT_DIR ${CMAKE_CURRENT_LIST_DIR}/regress_tests/cmake_test_scripts)
set(TEST_DATA ${gifti_test_data_SOURCE_DIR}/gifti_regress_data)
# Do all regression tests (add_test will only execute if TEST_GIFTI was set)
add_test(NAME gifti_ver COMMAND $<TARGET_FILE:gifti_tool> -gifti_ver)
add_test(NAME gifti_tool_ver COMMAND $<TARGET_FILE:gifti_tool> -ver)
add_test(NAME gifti_help COMMAND $<TARGET_FILE:gifti_tool> -help)
add_test(NAME gifti_gifti_hist COMMAND $<TARGET_FILE:gifti_tool> -gifti_hist)
add_test(NAME gifti_hist COMMAND $<TARGET_FILE:gifti_tool> -hist)

add_test(NAME gifti_disp_hdr
         COMMAND $<TARGET_FILE:gifti_tool> 
                 -disp_hdr 
                 -infiles ${TEST_DATA}/anat0.nii)
add_test(NAME gifti_disp_nim
         COMMAND $<TARGET_FILE:gifti_tool> 
                 -disp_nim 
                 -infiles ${TEST_DATA}/anat0.nii)
add_test(NAME gifti_disp_ext
         COMMAND $<TARGET_FILE:gifti_tool> 
                 -disp_ext 
                 -infiles ${TEST_DATA}/anat0.nii)
add_test(NAME gifti_header_check
         COMMAND $<TARGET_FILE:gifti_tool> 
                 -check_hdr 
                 -infiles ${TEST_DATA}/anat0.nii)
add_test(NAME gifti_nim_check
         COMMAND $<TARGET_FILE:gifti_tool> 
                 -check_nim 
                 -infiles ${TEST_DATA}/anat0.nii)
add_test(NAME gifti_pial_short_verbose
        COMMAND $<TARGET_FILE:gifti_tool> 
                 -check_nim 
                 -infile ${TEST_DATA}/ascii.pial.short.gii)
add_test(NAME gifti_show_color_table_gifti
        COMMAND $<TARGET_FILE:gifti_tool> 
                -show_gifti
                -infile ${TEST_DATA}/color.table.gii)
add_test(NAME gifti_show_surfs
        COMMAND $<TARGET_FILE:gifti_tool> 
                -show_gifti
                -infile ${TEST_DATA}/b64gz.ts.3.gii)



if(UNIX) # unix needed to run shell scripts
 
 foreach(test_stubname no_data_flag no_updates_flag high_verbosity copy_surfs
  b64_check buf_size encode indent compress other_formats new_dsets fix_errors
  modify_and_compare approx label_table col_maj_1 col_maj_2 extern_data)
  
  add_test(
    NAME gifti_${test_stubname}
    COMMAND bash -xeu ${TEST_SCRIPT_DIR}/${test_stubname}.sh
            $<TARGET_FILE:gifti_tool>
            ${TEST_DATA})
  endforeach()
endif()
