cmake_minimum_required (VERSION 3.10)
project (H4H5_UTILS_H5TOH4 C)

#
# Copyright by The HDF Group.
# All rights reserved.
#
# This file is part of H4H5TOOLS. The full H4H5TOOLS copyright notice,
# including terms governing use, modification, and redistribution, is
# contained in the COPYING file, which can be found at the root of the
# source code distribution tree. The copyright notice can also be found
# at https://www.hdfgroup.org/licenses.  If you do not have access to
# either file, you may request a copy from help@hdfgroup.org.
#

#-- Adding utility h5toh4convert
if (NOT ONLY_SHARED_LIBS)
  add_executable (h5toh4convert ${H4H5_UTILS_H5TOH4_SOURCE_DIR}/h5toh4.c)
  target_include_directories(h5toh4convert PRIVATE "${HDF4_INCLUDE_DIRS};${HDF5_INCLUDE_DIRS};${H4H5_SRC_DIR}")
  TARGET_C_PROPERTIES (h5toh4convert STATIC)
  target_link_libraries (h5toh4convert PRIVATE ${H4H5_SRC_LIB_TARGET})
  target_compile_definitions(h5toh4convert PUBLIC "$<$<BOOL:${H5_USE_HDFEOS2}>:HAVE_LIBHDFEOS>")
  SET_GLOBAL_VARIABLE (H4H5_APPS_TO_EXPORT "${H4H5_APPS_TO_EXPORT};h5toh4convert")
  set (install_targets h5toh4convert)
else ()
  add_executable (h5toh4convert-shared ${H4H5_UTILS_H5TOH4_SOURCE_DIR}/h5toh4.c)
  target_include_directories(h5toh4convert-shared PRIVATE "${HDF4_INCLUDE_DIRS};${HDF5_INCLUDE_DIRS};${H4H5_SRC_DIR}")
  TARGET_C_PROPERTIES (h5toh4convert-shared SHARED)
  target_link_libraries (h5toh4convert-shared PRIVATE ${H4H5_SRC_LIBSH_TARGET})
  target_compile_definitions(h5toh4convert-shared PUBLIC "$<$<BOOL:${H5_USE_HDFEOS2}>:HAVE_LIBHDFEOS>")
  SET_GLOBAL_VARIABLE (H4H5_APPS_TO_EXPORT "${H4H5_APPS_TO_EXPORT};h5toh4convert-shared")
  set (install_targets ${install_targets} h5toh4convert-shared)
endif ()

#add_test (NAME h5toh4convert COMMAND $<TARGET_FILE:h5toh4convert>)

if (BUILD_TESTING)

  #-- Copy all the data files from the test directory into the source directory
  set (H5H4_TEST_FILES
      tgroup
      tdset
      tattr
      tslink
      thlink
      tcompound
      tcompound-str
      tall
      tloop
      tdset2
      tcompound2
      tmany
      timage
      tsds_attr
      tsds_dim
      tstr
      tstr2
      ttable
  )
  foreach (h5h4_file ${H5H4_TEST_FILES})
    set (dest "${PROJECT_BINARY_DIR}/${h5h4_file}.h5")
    set (desth4 "${PROJECT_BINARY_DIR}/Expected/${h5h4_file}.hdf")
    #message (STATUS " Copying ${H4H5_UTILS_SRC_DIR}/testfiles/${h5h4_file} to ${dest} and ${desth4}")
    add_custom_command (
        TARGET     h5toh4convert
        POST_BUILD
        COMMAND    ${CMAKE_COMMAND}
        ARGS       -E copy_if_different ${H4H5_UTILS_SRC_DIR}/testfiles/${h5h4_file}.h5 ${dest}
    )
    add_custom_command (
        TARGET     h5toh4convert
        POST_BUILD
        COMMAND    ${CMAKE_COMMAND}
        ARGS       -E copy_if_different ${H4H5_UTILS_SRC_DIR}/testfiles/Expected/${h5h4_file}.hdf ${desth4}
    )
    add_custom_command (
        TARGET     h5toh4convert
        POST_BUILD
        COMMAND    ${CMAKE_COMMAND}
        ARGS       -E copy_if_different ${H4H5_UTILS_SRC_DIR}/testfiles/Expected/${h5h4_file}-r.hdf ${desth4}
    )
  endforeach ()

  # The converter assumes all hdf5 files has the .h5 suffix as in the form
  # of foo.h5.  It creates the corresponding hdf4 files with the .hdf suffix
  # as in the form of foo.hdf.  One exception is that if exactly two file
  # names are given, it treats the first argument as an hdf5 file and creates
  # the corresponding hdf4 file with the name as the second argument, WITOUT
  # any consideration of the suffix.  (For this test script, in order to
  # match the output hdf4 file with the expected hdf4 file, it expects the
  # second file of the two-files tests has the .hdf suffix too.)

  macro (ADD_H5H4_TEST testfile)
    add_test (
        NAME H5H4convert-${testfile}
        COMMAND $<TARGET_FILE:h5toh4convert> ${PROJECT_BINARY_DIR}/${testfile}.h5 ${PROJECT_BINARY_DIR}/${testfile}.hdf
    )
    if (NOT "${last_test}" STREQUAL "")
      set_tests_properties (H5H4convert-${testfile} PROPERTIES DEPENDS ${last_test})
    endif ()
    add_test (
        NAME H5H4convert-DIFF_${testfile}
        COMMAND ${HDF4_DIFF_EXECUTABLE} ${PROJECT_BINARY_DIR}/${testfile}.hdf ${PROJECT_BINARY_DIR}/Expected/${testfile}.hdf
    )
    set_tests_properties (H5H4convert-DIFF_${testfile} PROPERTIES DEPENDS H5H4convert-${testfile})
    set (last_test "H5H4convert-DIFF_${testfile}")
  endmacro ()

  macro (ADD_H5H4_ROOT_TEST testfile)
    add_test (
        NAME H5H4convert-r-${testfile}
        COMMAND $<TARGET_FILE:h5toh4convert> ${PROJECT_BINARY_DIR}/${testfile}.h5 -r ${PROJECT_BINARY_DIR}/${testfile}-r.hdf
    )
    if (NOT "${last_test}" STREQUAL "")
      set_tests_properties (H5H4convert-r-${testfile} PROPERTIES DEPENDS ${last_test})
    endif ()
    add_test (
        NAME H5H4convert-r-DIFF_${testfile}
        COMMAND ${HDF4_DIFF_EXECUTABLE} ${PROJECT_BINARY_DIR}/${testfile}-r.hdf ${PROJECT_BINARY_DIR}/Expected/${testfile}.hdf
    )
    set_tests_properties (H5H4convert-r-DIFF_${testfile} PROPERTIES DEPENDS H5H4convert-r-${testfile})
    set (last_test "H5H4convert-r-DIFF_${testfile}")
  endmacro ()

#H4DIFF=`echo $CC | sed -e 's/h4cc/:/' | cut -d: -f1`/'hdiff'

##############################################################################
##############################################################################
###           T H E   T E S T S                                ###
##############################################################################
##############################################################################

  # Remove any output file left over from previous test run
  add_test (
      NAME H5H4convert-clearall-objects
      COMMAND ${CMAKE_COMMAND} -E remove
          tgroup.hdf
          tdset.hdf
          tattr.hdf
          tslink.hdf
          thlink.hdf
          tcompound.hdf
          tcompound-str.hdf
          tall.hdf
          tloop.hdf
          tdset2.hdf
          tcompound2.hdf
          tmany.hdf
          timage.hdf
          tsds_attr.hdf
          tsds_dim.hdf
          tstr.hdf
          tstr2.hdf
          ttable.hdf
  )
  if (NOT "${last_test}" STREQUAL "")
    set_tests_properties (H5H4convert-clearall-objects PROPERTIES DEPENDS ${last_test})
  endif ()
  set (last_test "H5H4convert-clearall-objects")

  # Remove any output file left over from previous test run
  add_test (
      NAME H5H4convert-root-clearall-objects
      COMMAND ${CMAKE_COMMAND} -E remove
          tgroup-r.hdf
          tdset-r.hdf
          tattr-r.hdf
          tslink-r.hdf
          thlink-r.hdf
          tcompound-r.hdf
          tcompound-str-r.hdf
          tall-r.hdf
          tloop-r.hdf
          tdset2-r.hdf
          tcompound2-r.hdf
          tmany-r.hdf
          timage-r.hdf
          tsds_attr-r.hdf
          tsds_dim-r.hdf
          tstr-r.hdf
          tstr2-r.hdf
          ttable-r.hdf
  )
  if (NOT "${last_test}" STREQUAL "")
    set_tests_properties (H5H4convert-root-clearall-objects PROPERTIES DEPENDS ${last_test})
  endif ()
  set (last_test "H5H4convert-root-clearall-objects")
#$RM $OUTDIR/*.hdf $OUTDIR/*.tmp

  # tests for converting more H5 string objects.
  ADD_H5H4_TEST (tstr2)
  ADD_H5H4_TEST (timage)
  ADD_H5H4_TEST (ttable)
  ADD_H5H4_TEST (tsds_attr)
  ADD_H5H4_TEST (tsds_dim)

  #
  # Again, the test for conversion are the same as the first set of test.
  # Here, multiple conversion are done on HDF5 files at one time.
  #

#$RM $OUTDIR/*.hdf $OUTDIR/*.tmp
#CONVERT -m tgroup.h5 tdset.h5 tattr.h5 tslink.h5 thlink.h5
#CONVERT -m tcompound.h5 tall.h5
#CONVERT -m tloop.h5
#CONVERT -m tdset2.h5 tcompound2.h5 tmany.h5
#CONVERT -m tstr.h5 tstr2.h5

#$RM $OUTDIR/*.hdf $OUTDIR/*.tmp
  ADD_H5H4_ROOT_TEST (tgroup)
  ADD_H5H4_ROOT_TEST (tdset)
  ADD_H5H4_ROOT_TEST (tattr)
  ADD_H5H4_ROOT_TEST (tslink)
  ADD_H5H4_ROOT_TEST (thlink)
  ADD_H5H4_ROOT_TEST (tcompound)
  ADD_H5H4_ROOT_TEST (tcompound-str)
  ADD_H5H4_ROOT_TEST (tall)
  ADD_H5H4_ROOT_TEST (tloop)
  ADD_H5H4_ROOT_TEST (tdset2)
  ADD_H5H4_ROOT_TEST (tcompound2)
  ADD_H5H4_ROOT_TEST (tmany)
  ADD_H5H4_ROOT_TEST (tstr)
  ADD_H5H4_ROOT_TEST (tstr2)
  ADD_H5H4_ROOT_TEST (timage)
  ADD_H5H4_ROOT_TEST (ttable)
  ADD_H5H4_ROOT_TEST (tsds_attr)
  ADD_H5H4_ROOT_TEST (tsds_dim)

# to do: -m and -r together

endif ()

##############################################################################
##############################################################################
###           I N S T A L L A T I O N                                      ###
##############################################################################
##############################################################################

#-----------------------------------------------------------------------------
# Rules for Installation of tools using make Install target
#-----------------------------------------------------------------------------

foreach (exec ${install_targets})
  INSTALL_PROGRAM_PDB (${exec} ${H4H5_INSTALL_BIN_DIR} toolsapplications)
endforeach ()

install (
    TARGETS ${install_targets}
    EXPORT ${H4H5_EXPORTED_TARGETS}
    RUNTIME DESTINATION ${H4H5_INSTALL_BIN_DIR} COMPONENT toolsapplications
)
