#############################
# cdf tests at cdf_tests
#############################

macro(ADD_CDF_DLL testprog)
  add_custom_command (TARGET ${testprog} POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E copy_if_different
      $<TARGET_FILE:cdf> ${CMAKE_CURRENT_BINARY_DIR}
)
endmacro()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/include)
set(RPATH ${CMAKE_CURRENT_BINARY_DIR}/liblib)

set(qst2c_src qst2c.c ../src/include/cdf.h)
set(qst2cOrig_src qst2cOrig.c ../src/include/cdf.h)
set(qst2ic_src qst2ic.c ../src/include/cdf.h)
set(qst2cutf8_src qst2cutf8.c ../src/include/cdf.h)
set(qst2icEPOCH16_src qst2icEPOCH16.c ../src/include/cdf.h)
set(qst2icTT2000_src qst2icTT2000.c ../src/include/cdf.h)
set(qst2Version_src qst2Version.c ../src/include/cdf.h)

set(qst2c qst2c.c ../src/include/cdf.h)
set(qst2cOrig qst2cOrig.c ../src/include/cdf.h)
set(qst2ic qst2ic.c ../src/include/cdf.h)
set(qst2cutf8 qst2cutf8.c ../src/include/cdf.h)
set(qst2icEPOCH16 qst2icEPOCH16.c ../src/include/cdf.h)
set(qst2icTT2000 qst2icTT2000.c ../src/include/cdf.h)

if(BUILD_STATIC_CDF)
  add_executable(qst2c ${qst2c_src})
  add_executable(qst2cOrig ${qst2cOrig_src})
  add_executable(qst2ic ${qst2ic_src})
  add_executable(qst2cutf8 ${qst2cutf8_src})
  add_executable(qst2icEPOCH16 ${qst2icEPOCH16_src})
  add_executable(qst2icTT2000 ${qst2icTT2000_src})
  add_executable(qst2Version ${qst2Version_src})
endif()

if(BUILD_SHARED_CDF)
  add_executable(qst2cS ${qst2c_src})
  add_executable(qst2cOrigS ${qst2cOrig_src})
  add_executable(qst2icS ${qst2ic_src})
  add_executable(qst2cutf8S ${qst2cutf8_src})
  add_executable(qst2icEPOCH16S ${qst2icEPOCH16_src})
  add_executable(qst2icTT2000S ${qst2icTT2000_src})
  add_executable(qst2VersionS ${qst2Version_src})
endif()

if(NOT ISMSVC)
  if(BUILD_STATIC_CDF)
    target_link_libraries(qst2c cdf_static m) 
    target_link_libraries(qst2cOrig cdf_static m)
    target_link_libraries(qst2ic cdf_static m)
    target_link_libraries(qst2cutf8 cdf_static m)
    target_link_libraries(qst2icEPOCH16 cdf_static m)
    target_link_libraries(qst2icTT2000 cdf_static m)
    target_link_libraries(qst2Version cdf_static m)
  endif()

  if(BUILD_SHARED_CDF)
    target_link_libraries(qst2cS PUBLIC cdf m) 
    target_link_libraries(qst2cOrigS PUBLIC cdf m)
    target_link_libraries(qst2icS PUBLIC cdf m)
    target_link_libraries(qst2cutf8S PUBLIC cdf m)
    target_link_libraries(qst2icEPOCH16S PUBLIC cdf m)
    target_link_libraries(qst2icTT2000S PUBLIC cdf m)
    target_link_libraries(qst2VersionS PUBLIC cdf m)
  endif()
else()
  if(BUILD_STATIC_CDF)
    set(CMAKE_EXE_LINKER_FLAGS
	  "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB:LIBCMT")
    target_link_libraries(qst2c cdf_static) 
    target_link_libraries(qst2cOrig cdf_static)
    target_link_libraries(qst2ic cdf_static)
    target_link_libraries(qst2cutf8 cdf_static)
    target_link_libraries(qst2icEPOCH16 cdf_static)
    target_link_libraries(qst2icTT2000 cdf_static)
    target_link_libraries(qst2Version cdf_static)
  endif()

  if(BUILD_SHARED_CDF)
    target_link_libraries(qst2cS PUBLIC cdf) 
    target_link_libraries(qst2cOrigS PUBLIC cdf)
    target_link_libraries(qst2icS PUBLIC cdf)
    target_link_libraries(qst2cutf8S PUBLIC cdf)
    target_link_libraries(qst2icEPOCH16S PUBLIC cdf)
    target_link_libraries(qst2icTT2000S PUBLIC cdf)
    target_link_libraries(qst2VersionS PUBLIC cdf)
  endif()
endif()

if(TEST_FORTRAN)
  if(NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/CDF.INC)
    file(COPY_FILE ${CDF_SOURCE_DIR}/src/include/cdf.inc ${CMAKE_CURRENT_BINARY_DIR}/CDF.INC)
  endif()

  set(qst2f_src qst2f.f CDF.INC)
  set(qst2fOrig_src qst2fOrig.f CDF.INC)
  set(qst2if_src qst2if.f CDF.INC)
  set(qst2futf8_src qst2futf8.f CDF.INC)
  set(qst2ifEPOCH16_src qst2ifEPOCH16.f CDF.INC)
  set(qst2ifTT2000_src qst2ifTT2000.f CDF.INC)

  include_directories(${CMAKE_CURRENT_SOURCE_DIR})
  if(LINUX OR CYGWIN)
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -fsecond-underscore")
  endif()
  if(SOLARIS)
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -ext_names=fsecond-underscore")
  endif()

  check_Fortran_compiler_flag("-fallow-argument-mismatch" HAS_ALLOW_ARGUMENT_MISMATCH)
  if(HAS_ALLOW_ARGUMENT_MISMATCH)
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -w -fallow-argument-mismatch")
  endif()

  check_Fortran_compiler_flag("-mismatch_all" HAS_MISMATCH_ALL)
  if(HAS_MISMATCH_ALL)
    set(CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} -mismatch_all")
  endif()

  if(BUILD_STATIC_CDF)
    add_executable(qst2f ${qst2f_src})
    add_executable(qst2fOrig ${qst2fOrig_src})
    add_executable(qst2if ${qst2if_src})
    add_executable(qst2futf8 ${qst2futf8_src})
    add_executable(qst2ifEPOCH16 ${qst2ifEPOCH16_src})
    add_executable(qst2ifTT2000 ${qst2ifTT2000_src})

    set_target_properties(qst2f PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2fOrig PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2if PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2futf8 PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2ifEPOCH16 PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2ifTT2000 PROPERTIES LINKER_LANGUAGE Fortran)

    if(NOT ISMSVC)
      target_link_libraries(qst2f cdf_static m)
      target_link_libraries(qst2fOrig cdf_static m)
      target_link_libraries(qst2if cdf_static m)
      target_link_libraries(qst2futf8 cdf_static m)
      target_link_libraries(qst2ifEPOCH16 cdf_static m)
      target_link_libraries(qst2ifTT2000 cdf_static m)
    else()
      target_link_libraries(qst2f cdf_static)
      target_link_libraries(qst2fOrig cdf_static)
      target_link_libraries(qst2if cdf_static)
      target_link_libraries(qst2futf8 cdf_static)
      target_link_libraries(qst2ifEPOCH16 cdf_static)
      target_link_libraries(qst2ifTT2000 cdf_static)
    endif()
  endif()

  if(BUILD_SHARED_CDF)
    add_executable(qst2fS ${qst2f_src})
    add_executable(qst2fOrigS ${qst2fOrig_src})
    add_executable(qst2ifS ${qst2if_src})
    add_executable(qst2futf8S ${qst2futf8_src})
    add_executable(qst2ifEPOCH16S ${qst2ifEPOCH16_src})
    add_executable(qst2ifTT2000S ${qst2ifTT2000_src})

    set_target_properties(qst2fS PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2fOrigS PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2ifS PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2futf8S PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2ifEPOCH16S PROPERTIES LINKER_LANGUAGE Fortran)
    set_target_properties(qst2ifTT2000S PROPERTIES LINKER_LANGUAGE Fortran)

    if(NOT ISMSVC)
      target_link_libraries(qst2fS PUBLIC cdf m)
      target_link_libraries(qst2fOrigS PUBLIC cdf m)
      target_link_libraries(qst2ifS PUBLIC cdf m)
      target_link_libraries(qst2futf8S PUBLIC cdf m)
      target_link_libraries(qst2ifEPOCH16S PUBLIC cdf m)
      target_link_libraries(qst2ifTT2000S PUBLIC cdf m)
    else()
      target_link_libraries(qst2fS PUBLIC cdf)
      target_link_libraries(qst2fOrigS PUBLIC cdf)
      target_link_libraries(qst2ifS PUBLIC cdf)
      target_link_libraries(qst2futf8S PUBLIC cdf)
      target_link_libraries(qst2ifEPOCH16S PUBLIC cdf)
      target_link_libraries(qst2ifTT2000S PUBLIC cdf)
    endif()
  endif()

endif()

if(ISMSVC)
  if(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/../liblib/Release/dllcdf.dll)
	  set(DYLIB ${CMAKE_CURRENT_BINARY_DIR}/../liblib/Release)
  elseif(EXISTS ${CMAKE_CURRENT_BINARY_DIR}/../liblib/Debug/dllcdf.dll)
    set(DYLIB ${CMAKE_CURRENT_BINARY_DIR}/../liblib/Debug)
  endif()
else()
  set(DYLIB ${CMAKE_CURRENT_BINARY_DIR}/../liblib)
endif()

if(BUILD_STATIC_CDF)
  add_test(NAME qst2c COMMAND qst2c)
  add_test(NAME qst2cOrig COMMAND qst2cOrig)
  add_test(NAME qst2ic COMMAND qst2ic)
  add_test(NAME qst2cutf8 COMMAND qst2cutf8)
  add_test(NAME qst2icEPOCH16 COMMAND qst2icEPOCH16)
  add_test(NAME qst2icTT2000 COMMAND qst2icTT2000)
  add_test(NAME qst2Version COMMAND qst2Version)
endif()

if(BUILD_SHARED_CDF)
  add_test(NAME qst2cS COMMAND qst2cS WORKING_DIRECTORY ${DYLIB})
  set_tests_properties(qst2cS PROPERTIES ENVIRONMENT_MODIFICATION
	"PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2cS>>")
  add_test(NAME qst2cOrigS COMMAND qst2cOrigS WORKING_DIRECTORY ${DYLIB})
  set_tests_properties(qst2cOrigS PROPERTIES ENVIRONMENT_MODIFICATION
	"PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2cOrigS>>")
  add_test(NAME qst2icS COMMAND qst2icS WORKING_DIRECTORY ${DYLIB})
  set_tests_properties(qst2icS PROPERTIES ENVIRONMENT_MODIFICATION
	"PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2icS>>")
  add_test(NAME qst2cutf8S COMMAND qst2cutf8S WORKING_DIRECTORY ${DYLIB})
  set_tests_properties(qst2cutf8S PROPERTIES ENVIRONMENT_MODIFICATION
	"PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2cutf8S>>")
  add_test(NAME qst2icEPOCH16S COMMAND qst2icEPOCH16S WORKING_DIRECTORY ${DYLIB})
  set_tests_properties(qst2icEPOCH16S PROPERTIES ENVIRONMENT_MODIFICATION
	"PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2icEPOCH16S>>")
  add_test(NAME qst2icTT2000S COMMAND qst2icTT2000S WORKING_DIRECTORY ${DYLIB})
  set_tests_properties(qst2icTT2000S PROPERTIES ENVIRONMENT_MODIFICATION
	"PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2icTT2000S>>")
  add_test(NAME qst2VersionS COMMAND qst2VersionS WORKING_DIRECTORY ${DYLIB})
  set_tests_properties(qst2VersionS PROPERTIES ENVIRONMENT_MODIFICATION
	"PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2VersionS>>")
endif()

if(TEST_FORTRAN)
  if(BUILD_STATIC_CDF)
    add_test(NAME qst2f COMMAND qst2f)
    add_test(NAME qst2fOrig COMMAND qst2fOrig)
    add_test(NAME qst2if COMMAND qst2if)
    add_test(NAME qst2futf8 COMMAND qst2futf8)
    add_test(NAME qst2ifEPOCH16 COMMAND qst2ifEPOCH16)
    add_test(NAME qst2ifTT2000 COMMAND qst2ifTT2000)
  endif()

  if(BUILD_SHARED_CDF)
    add_test(NAME qst2fS COMMAND qst2fS WORKING_DIRECTORY ${DYLIB})
    set_tests_properties(qst2fS PROPERTIES ENVIRONMENT_MODIFICATION
	  "PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2fS>>")
    add_test(NAME qst2fOrigS COMMAND qst2fOrigS WORKING_DIRECTORY ${DYLIB})
    set_tests_properties(qst2fOrigS PROPERTIES ENVIRONMENT_MODIFICATION
	  "PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2fOrigS>>")
    add_test(NAME qst2ifS COMMAND qst2ifS WORKING_DIRECTORY ${DYLIB})
    set_tests_properties(qst2ifS PROPERTIES ENVIRONMENT_MODIFICATION
	  "PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2ifS>>")
    add_test(NAME qst2futf8S COMMAND qst2futf8S WORKING_DIRECTORY ${DYLIB})
    set_tests_properties(qst2futf8S PROPERTIES ENVIRONMENT_MODIFICATION
	  "PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2futf8S>>")
    add_test(NAME qst2ifEPOCH16S COMMAND qst2ifEPOCH16S WORKING_DIRECTORY ${DYLIB})
    set_tests_properties(qst2ifEPOCH16S PROPERTIES ENVIRONMENT_MODIFICATION
	  "PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2ifEPOCH16S>>")
    add_test(NAME qst2ifTT2000S COMMAND qst2ifTT2000S WORKING_DIRECTORY ${DYLIB})
    set_tests_properties(qst2ifTT2000S PROPERTIES ENVIRONMENT_MODIFICATION
	  "PATH=path_list_prepend:$<$<BOOL:${WIN32}>:$<TARGET_RUNTIME_DLL_DIRS:qst2ifTT2000S>>")
  endif()
endif()

if(dll_files)
  file(GLOB copied_files ${CMAKE_CURRENT_BINARY_DIR}/*.dll)
  file(REMOVE_RECURES ${copied_files})
endif()
