#
# Find package HDF5
#

include( ExternalProject )

IF( WIN32 )
  MESSAGE( "Deflating libhdf5" )
  SET( LIBHDF5_VER 1.10.0-patch1 )
  SET( LIBHDF5DIR ${CMAKE_CURRENT_BINARY_DIR}/../_packages/libhdf5 )
  
  GET_TARGET_PROPERTY( ZLIB_INC_DIR zlibstatic INTERFACE_INCLUDE_DIRECTORIES )
  GET_TARGET_PROPERTY( ZLIB_LIB zlibstatic IMPORTED_LOCATION )
  
  EXTERNALPROJECT_ADD( libhdf5_ext
    URL ${CMAKE_SOURCE_DIR}/3rdparty/LibHdf5/hdf5-${LIBHDF5_VER}.tar.gz
    PREFIX libhdf5
    BINARY_DIR ${LIBHDF5DIR}
    INSTALL_COMMAND ""
    CMAKE_ARGS
      -DCMAKE_BUILD_TYPE:String=${CMAKE_BUILD_TYPE}	
      -DBUILD_SHARED_LIBS:Bool=OFF
      -DBUILD_TESTING:Bool=Off
      -DHDF5_BUILD_CPP_LIB:Bool=Off
      -DHDF5_DISABLE_COMPILER_WARNINGS:String=ON
      -DHDF5_ENABLE_ALL_WARNINGS:BOOL=OFF
      -DHDF5_BUILD_EXAMPLES:Bool=Off
      -DHDF5_BUILD_HL_LIB:Bool=OFF
      -DHDF5_BUILD_TOOLS:Bool=OFF
      -DHDF5_ENABLE_DEPCRICATED_SYMBOLS:Bool=On 
      -DZLIB_USE_EXTERNAL:STRING=ON
      -DHDF5_PACKAGE_EXTLIBS:BOOL=ON
      -DHDF5_ENABLE_Z_LIB_SUPPORT:BOOL=ON
      -DZLIB_FOUND:STRING=ON
      -DZLIB_LIBRARY:Filepath=${ZLIB_LIB}
      -DZLIB_INCLUDE_DIR:Path=${ZLIB_INC_DIR} )

  ADD_CUSTOM_COMMAND( TARGET libhdf5_ext POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/../_packages/libhdf5/H5pubconf.h ${CMAKE_CURRENT_BINARY_DIR}/libhdf5/src/libhdf5_ext/src/H5pubconf.h )

  SET( LIBHDF5_INCLUDE_DIRS ${CMAKE_CURRENT_BINARY_DIR}/libhdf5/src/libhdf5_ext/src )
  FILE( MAKE_DIRECTORY ${LIBHDF5_INCLUDE_DIRS} )
  ADD_LIBRARY( LIBHDF5::LIBHDF5STATIC STATIC IMPORTED GLOBAL )
  ADD_DEPENDENCIES( LIBHDF5::LIBHDF5STATIC libhdf5_ext )
  SET_PROPERTY( TARGET LIBHDF5::LIBHDF5STATIC PROPERTY IMPORTED_LOCATION ${LIBHDF5DIR}/bin/${CMAKE_BUILD_TYPE}/libhdf5_D.lib )
  SET_PROPERTY( TARGET LIBHDF5::LIBHDF5STATIC PROPERTY IMPORTED_LOCATION_DEBUG ${LIBHDF5DIR}/bin/${CMAKE_BUILD_TYPE}/libhdf5_D.lib )  
  SET_PROPERTY( TARGET LIBHDF5::LIBHDF5STATIC PROPERTY IMPORTED_LOCATION_RELEASE ${LIBHDF5DIR}/bin/${CMAKE_BUILD_TYPE}/libhdf5.lib )
  SET_PROPERTY( TARGET LIBHDF5::LIBHDF5STATIC PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${LIBHDF5_INCLUDE_DIRS} )
  SET_PROPERTY( TARGET LIBHDF5::LIBHDF5STATIC PROPERTY INTERFACE_COMPILE_DEFINITIONS LIBHDF5_STATIC )
  ADD_DEPENDENCIES( libhdf5_ext zlibstatic )

ELSE()

  FIND_PACKAGE( HDF5 1.10.0 EXACT REQUIRED NO_MODULE )

ENDIF()
