#
# Build package LibMatIo
#

include( ExternalProject )

IF (APPLE)
	# Look for the header file.
	FIND_PATH(MATIO_INCLUDE_DIR NAMES matio.h)
	MARK_AS_ADVANCED(MATIO_INCLUDE_DIR)

	# Look for the library.
	FIND_LIBRARY(MATIO_LIBRARY NAMES matio)
	MARK_AS_ADVANCED(MATIO_LIBRARY)

	# handle the QUIETLY and REQUIRED arguments and set MATIO_FOUND to TRUE if 
	# all listed variables are TRUE
	INCLUDE(FindPackageHandleStandardArgs)
	FIND_PACKAGE_HANDLE_STANDARD_ARGS(MATIO DEFAULT_MSG MATIO_LIBRARY MATIO_INCLUDE_DIR)

	IF(MATIO_FOUND)
	  SET(MATIO_LIBRARIES ${MATIO_LIBRARY} ${HDF5_LIBRARIES})
	  SET(MATIO_INCLUDE_DIRS ${MATIO_INCLUDE_DIR} ${HDF5_INCLUDE_DIR})
	ELSE(MATIO_FOUND)
	  SET(MATIO_LIBRARIES)
	  SET(MATIO_INCLUDE_DIRS)
	ENDIF(MATIO_FOUND)  
	include_directories(set(LibArchive_INCLUDE_DIR "/usr/local/opt/libarchive/include"))
	set(LibMatio_INCLUDE_DIR "/usr/local/opt/libmatio/include")
ELSE()
                       SET( LIBMATIO_VER 1.5.10 )

	EXECUTE_PROCESS( 
	  COMMAND ${CMAKE_COMMAND} -E tar xzf ${CMAKE_SOURCE_DIR}/3rdparty/LibMatIo/matio-${LIBMATIO_VER}.tar.gz
	  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} )

	# patch mat73 bug
	#EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_SOURCE_DIR}/3rdparty/LibMatIo/matio-${LIBMATIO_VER}-patch/mat73.c ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/src/mat73.c)

	# patch files generated by ./configure
	IF( WIN32 )

	  EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/visual_studio/matioConfig.h ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/src/matioConfig.h)
	  EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/visual_studio/matio_pubconf.h ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/src/matio_pubconf.h)

                          ELSE()

	  EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy  ${CMAKE_SOURCE_DIR}/3rdparty/LibMatIo/matio-${LIBMATIO_VER}-linux-patch/matioConfig.h ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/src/matioConfig.h)
	  EXECUTE_PROCESS(COMMAND ${CMAKE_COMMAND} -E copy  ${CMAKE_SOURCE_DIR}/3rdparty/LibMatIo/matio-${LIBMATIO_VER}-linux-patch/matio_pubconf.h ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/src/matio_pubconf.h)

	ENDIF()

	SET( MATIO_INC ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/src )
	FILE( GLOB MATIO_SRC ${CMAKE_CURRENT_BINARY_DIR}/matio-${LIBMATIO_VER}/src/*.c )

	IF( WIN32 )

	  ADD_LIBRARY( libmatio_static STATIC ${MATIO_SRC} )

	  # disable warnings
	  SET_PROPERTY( TARGET libmatio_static APPEND_STRING PROPERTY COMPILE_FLAGS " /W0")

	  TARGET_LINK_LIBRARIES( libmatio_static
	    zlibstatic
	    LIBHDF5::LIBHDF5STATIC )

	  TARGET_INCLUDE_DIRECTORIES( libmatio_static PUBLIC ${MATIO_INC} )

	  TARGET_COMPILE_DEFINITIONS( libmatio_static
	          PUBLIC REPLACE_GETOPT 
	          PUBLIC MAT73 
	          PUBLIC HAVE_HDF5
	          PUBLIC HAVE_ZLIB
	          PUBLIC H5_NO_DEPRECATED_SYMBOLS
	          PUBLIC H5_BUILT_AS_STATIC_LIB )

                          ELSE()

	  ADD_LIBRARY( libmatio SHARED ${MATIO_SRC} )

	  TARGET_LINK_LIBRARIES( libmatio
	    zlib
	    hdf5-static )

	  # disable warnings
	  SET_PROPERTY( TARGET libmatio APPEND_STRING PROPERTY COMPILE_FLAGS " -w")

	  TARGET_INCLUDE_DIRECTORIES( libmatio PUBLIC ${MATIO_INC} )

	  TARGET_COMPILE_DEFINITIONS( libmatio
	          PUBLIC REPLACE_GETOPT 
	          PUBLIC MAT73 
	          PUBLIC HAVE_HDF5
	          PUBLIC HAVE_ZLIB
	          PUBLIC H5_NO_DEPRECATED_SYMBOLS 
	          PUBLIC H5_BUILT_AS_STATIC_LIB )
	ENDIF()
ENDIF()
