set(cdflib_LIBS)
set(cdflibstatic_LIBS)
set(CDF_INSTALL_LIBRARIES)
set(csharp_LIBS)

if(MSVC)
  #  string(REPLACE "W3" "W0" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  string(REPLACE "/W3" "" CMAKE_C_FLAGS "${CMAKE_C_FLAGS}")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /D_CRT_SECURE_NO_WARNINGS /O2")
endif()

##############################################
# Add target objects/modules based on options.
##############################################

if(USE_ZLIB-NG)
  if(BUILD_SHARED_CDF)
    set(cdflib_LIBS ${cdflib_LIBS} zlib-ng)
  endif()
  if(BUILD_STATIC_CDF)
    set(cdflibstatic_LIBS ${cdflibstatic_LIBS} zlibstatic-ng)
  endif()
  if(BUILD_Csharp)
    set(csharp_LIBS ${csharp_LIBS} zlib-ng)
  endif()
else()
  if(BUILD_SHARED_CDF)
    set(cdflib_LIBS ${cdflib_LIBS} zlib-orig)
  endif()
  if(BUILD_STATIC_CDF)
    set(cdflibstatic_LIBS ${cdflibstatic_LIBS} zlibstatic-orig)
  endif()
  if(BUILD_Csharp)
    set(csharp_LIBS ${csharp_LIBS} zlib-orig)
  endif()
endif()

if(BUILD_SHARED_CDF)
  set(cdflib_LIBS ${cdflib_LIBS} CDF_SHARED)
endif()

if(BUILD_STATIC_CDF)
  set(cdflibstatic_LIBS ${cdflibstatic_LIBS} CDF_STATIC)
endif()

set(LARGS)
if(BUILD_SHARED_CDF)
  foreach(LIBS ${cdflib_LIBS})
    set(LARGS ${LARGS} $<TARGET_OBJECTS:${LIBS}>)
  endforeach()

  add_library(cdf SHARED ${LARGS})
  set(CDF_INSTALL_LIBRARIES cdf)
endif()

set(LARGS)
if(BUILD_STATIC_CDF)
  foreach(LIBS ${cdflibstatic_LIBS})
    set(LARGS ${LARGS} $<TARGET_OBJECTS:${LIBS}>)
  endforeach()

  add_library(cdf_static STATIC ${LARGS})
  set(CDF_INSTALL_LIBRARIES ${CDF_INSTALL_LIBRARIES} cdf_static)
endif()

if (TARGET cdf)
  set_target_properties(cdf PROPERTIES EXPORT_NAME cdf)
else()
  set_target_properties(cdf_static PROPERTIES EXPORT_NAME cdf)
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/include)
if(USE_ZLIB-NG)
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/include/zlib-ng/${ARCHDIR})
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/include/zlib-ng/arch/generic)
else()
  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../src/lib/zlib)
endif()

foreach(CDF_INSTALL_LIBRARY ${CDF_INSTALL_LIBRARIES})
  target_compile_definitions(${CDF_INSTALL_LIBRARY} PUBLIC CDF_NATIVE_API)
  target_include_directories(${CDF_INSTALL_LIBRARY} PUBLIC
        "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR};${CMAKE_CURRENT_SOURCE_DIR}>"
        "$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>")
endforeach()

if(WIN32)
  if(BUILD_SHARED_CDF)
    # Shared library
    set_target_properties(cdf PROPERTIES OUTPUT_NAME dllcdf)
    target_compile_definitions(cdf PRIVATE _USRDLL DLLCDF_EXPORTS BUILDINGforDLL)
  endif()
  if(BUILD_STATIC_CDF)
    # Static library
    if(MSVC)
get_target_property(co cdf_static COMPILE_OPTIONS)
      set_target_properties(cdf_static PROPERTIES OUTPUT_NAME libcdf
	                                          COMPILE_OPTIONS "$<$<CONFIG:Debug>:/MTd>$<$<CONFIG:Release>:/MT>")
      target_compile_definitions(cdf_static PRIVATE _LIB)
get_target_property(co cdf_static COMPILE_OPTIONS)
    else()
      set_target_properties(cdf_static PROPERTIES OUTPUT_NAME cdf)
    endif()
  endif()
else()
  # On unix-like platforms the library is almost always called libcdf
  set_target_properties(${CDF_INSTALL_LIBRARIES} PROPERTIES OUTPUT_NAME cdf)
endif()

if(BUILD_SHARED_CDF)
  set_target_properties(cdf PROPERTIES DEFINE_SYMBOL DLLCDF)
  set_target_properties(cdf PROPERTIES SOVERSION 3)

  if(NOT CYGWIN)
    set_target_properties(cdf PROPERTIES VERSION ${CDF_FULL_VERSION})
  endif()

  if(UNIX)
    if(HAVE_NO_INTERPOSITION)
      set_target_properties(cdf PROPERTIES COMPILE_FLAGS "-fno-semantic-interposition")
    endif()
    if(NOT APPLE AND NOT CMAKE_SYSTEM_NAME STREQUAL AIX)
    endif()
  endif()
  if(MSYS)
    # Suppress version number from shared library name
    set(CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION 3)
  elseif(WIN32)
    # Creates zlib1.dll when building shared library version
    set_target_properties(cdf PROPERTIES SUFFIX ".dll")
  endif()
endif()


file(GLOB CUR_EXTRA_DIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
set(CUR_EXTRA_DIST ${CUR_EXTRA_DIST} CMakeLists.txt )
ADD_EXTRA_DIST("${CUR_EXTRA_DIST}")

if(MOD_CDF_NAME)
  set_target_properties(cdf PROPERTIES LIBRARY_OUTPUT_NAME ${CDF_LIB_NAME})
  set_target_properties(cdf PROPERTIES ARCHIVE_OUTPUT_NAME ${CDF_LIB_NAME})
  set_target_properties(cdf PROPERTIES RUNTIME_OUTPUT_NAME ${CDF_LIB_NAME})
endif()

#####
# JNI
#####

if (BUILD_JNI)
  set(LARGS)
  set(jnilib_LIBS ${cdflib_LIBS} JNI_CORE)
  foreach(LIBS ${jnilib_LIBS})
    set(LARGS ${LARGS} $<TARGET_OBJECTS:${LIBS}>)
  endforeach()
  add_library(cdfNativeLibrary SHARED ${LARGS})
  set(CDF_INSTALL_LIBRARIES ${CDF_INSTALL_LIBRARIES} cdfNativeLibrary)

  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../javacdf/jni)
  set_target_properties(cdfNativeLibrary PROPERTIES DEFINE_SYMBOL cdfNativeLibrary)

  if(NOT CYGWIN)
    set_target_properties(cdfNativeLibrary PROPERTIES VERSION ${CDF_FULL_VERSION})
  endif()
  set_target_properties(cdfNativeLibrary PROPERTIES SOVERSION 3)
  if(MSYS)
    # Suppress version number from shared library name
    set(CMAKE_SHARED_LIBRARY_NAME_WITH_VERSION 3)
  elseif(WIN32)
    # Creates .dll when building shared library version
    set_target_properties(cdfNativeLibrary PROPERTIES SUFFIX ".dll")
    target_compile_definitions(cdfNativeLibrary PRIVATE _USRDLL DLLCDF_EXPORTS JAVACDF_EXPORTS BUILDINGforDLL)
  endif()

endif()

########
# Csharp
########

if (BUILD_Csharp)
  set(LARGS)
  set(csharp_LIBS ${csharp_LIBS} CDF_Csharp_CORE)
  foreach(LIBS ${csharp_LIBS})
    set(LARGS ${LARGS} $<TARGET_OBJECTS:${LIBS}>)
  endforeach()
  add_library(cdfCsharpLibrary SHARED ${LARGS})
  set(CDF_INSTALL_LIBRARIES ${CDF_INSTALL_LIBRARIES} cdfCsharpLibrary)

  include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../win32/csharp)

  set_target_properties(cdfCsharpLibrary PROPERTIES OUTPUT_NAME dllcdfcsharp)
  set_target_properties(cdfCsharpLibrary PROPERTIES DEFINE_SYMBOL dllcdfcsharp)
  set_target_properties(cdfCsharpLibrary PROPERTIES SUFFIX ".dll")

endif()

########################################
# Add dependencies required for linking.
########################################

set(TLL_LIBS "")

set(TLL_LIBS ${TLL_LIBS} ${HAVE_LIBM})

if(NOT WIN32)
  if(NOT APPLE)
    if(CMAKE_DL_LIBS)
      TARGET_LINK_LIBRARIES(cdf ${CMAKE_DL_LIBS})
    endif()
  endif()
endif()

set(CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES} ${TLL_LIBS})
if(MSVC)
  set_target_properties(cdf PROPERTIES
    LINK_FLAGS_DEBUG " /NODEFAULTLIB:MSVCRT"
  )
endif()

if(NOT MSVC)
  if(BUILD_SHARED_CDF)
    set_target_properties(cdf PROPERTIES LINK_FLAGS -shared)
  endif()
endif()

if(${CMAKE_VERSION} VERSION_LESS 2.8.12)
  install(
    TARGETS ${CDF_INSTALL_LIBRARIES} EXPORT cdf
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT libraries
    PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    )
else()
    install(
    TARGETS ${CDF_INSTALL_LIBRARIES} EXPORT cdf
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries
    ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT libraries
    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT libraries
    INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    )
endif()

set(ALL_TLL_LIBS ${TLL_LIBS} PARENT_SCOPE)

########
# script
########

if(NOT MSVC)
  configure_file(${CDF_SOURCE_DIR}/src/definitions/definitions.B.cmakein
                 ${CMAKE_CURRENT_LIST_DIR}/src/definitions.B @ONLY)
  configure_file(${CDF_SOURCE_DIR}/src/definitions/definitions.C.cmakein
                 ${CMAKE_CURRENT_LIST_DIR}/src/definitions.C @ONLY)
  configure_file(${CDF_SOURCE_DIR}/src/definitions/definitions.K.cmakein
                 ${CMAKE_CURRENT_LIST_DIR}/src/definitions.K @ONLY)

  install(FILES ${CMAKE_CURRENT_LIST_DIR}/src/definitions.B
          DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
  install(FILES ${CMAKE_CURRENT_LIST_DIR}/src/definitions.C
          DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
  install(FILES ${CMAKE_CURRENT_LIST_DIR}/src/definitions.K
          DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
endif()

#add_custom_command(
#    TARGET clean
#    POST_BUILD
#    COMMAND ${CMAKE_COMMAND} -E rm -f "${CMAKE_CURRENT_LIST_DIR}/src/definitions.*"
#    COMMENT "Removing temporary files after installation"
#)

######
# help
######

install(DIRECTORY ${CDF_SOURCE_DIR}/src/help DESTINATION 
        ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}/cdf
        PATTERN CVS EXCLUDE)

if(MSVC)
  ##########
  # terminfo
  ##########

# install(DIRECTORY ${CDF_SOURCE_DIR}/win32/terminfo DESTINATION 
#         ${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}
#         PATTERN CVS EXCLUDE)
endif()

######
# Jar
######

#if(MSVC)
  install(FILES ${CDF_SOURCE_DIR}/cdfjava/classes/cdfjava.jar
	  DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
  install(FILES ${CDF_SOURCE_DIR}/cdfjava/classes/cdfml.jar
	  DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
  install(FILES ${CDF_SOURCE_DIR}/cdfjava/classes/cdfjson.jar
	  DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
  install(FILES ${CDF_SOURCE_DIR}/cdfjava/classes/gson-2.8.6.jar
	  DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
  install(FILES ${CDF_SOURCE_DIR}/cdfjava/classes/javax.json-1.0.4.jar
	  DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
  if(MSVC)
    install(FILES ${CDF_SOURCE_DIR}/cdfjava/cdftools/CDFToolsDriver.jar
            DESTINATION ${CMAKE_INSTALL_PREFIX})
  else()
    install(FILES ${CDF_SOURCE_DIR}/cdfjava/cdftools/CDFToolsDriver.jar
            DESTINATION ${CMAKE_INSTALL_PREFIX}/lib)
  endif()
#endif()

########
# Others
########

install(FILES ${CDF_SOURCE_DIR}/Welcome.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${CDF_SOURCE_DIR}/CDFLeapSeconds.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${CDF_SOURCE_DIR}/CDF_copyright.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${CDF_SOURCE_DIR}/CHANGES.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${CDF_SOURCE_DIR}/Release_notes.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${CDF_SOURCE_DIR}/README_cdf_tools.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${CDF_SOURCE_DIR}/HOWTO_run-CDF-XML-tools.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${CDF_SOURCE_DIR}/HOWTO_run-CDF-Java-tools.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})
install(FILES ${CDF_SOURCE_DIR}/HOWTO_run-CDF-Json-tools.txt
	DESTINATION ${CMAKE_INSTALL_PREFIX})

if(MSVC)
  install(FILES ${CDF_SOURCE_DIR}/win32/checkleapseconds.bat
	  DESTINATION ${CMAKE_INSTALL_PREFIX})
  install(FILES ${CDF_SOURCE_DIR}/win32/checkleapseconds.ps1
	  DESTINATION ${CMAKE_INSTALL_PREFIX})
  install(FILES ${CDF_SOURCE_DIR}/win32/setenv.bat
	  DESTINATION ${CMAKE_INSTALL_PREFIX})
  install(FILES ${CDF_SOURCE_DIR}/win32/setenv.ps1
	  DESTINATION ${CMAKE_INSTALL_PREFIX})
  install(FILES ${CDF_SOURCE_DIR}/win32/showenv.ps1
          DESTINATION ${CMAKE_INSTALL_PREFIX})
  install(FILES ${CDF_SOURCE_DIR}/win32/toUTF8.ps1
          DESTINATION ${CMAKE_INSTALL_PREFIX})

  string(FIND ${CMAKE_EXE_LINKER_FLAGS} "x64" is64Bit)
  if(is64Bit GREATER -1)
    install(FILES ${CDF_SOURCE_DIR}/win32/pdcurses/x64/bin/pdcurses.dll
	    DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
  else()
    install(FILES ${CDF_SOURCE_DIR}/win32/pdcurses/x86/bin/pdcurses.dll
	    DESTINATION ${CMAKE_INSTALL_PREFIX}/bin)
  endif()
  if(BUILD_Csharp)
    install(DIRECTORY ${CDF_SOURCE_DIR}/win32/csharp/bin DESTINATION
            ${CMAKE_INSTALL_PREFIX} PATTERN CVS EXCLUDE)
    file(GLOB BAT_FILES "${CDF_SOURCE_DIR}/win32/csharp/*.bat")
    install(FILES ${BAT_FILES}
	    DESTINATION ${CMAKE_INSTALL_PREFIX}/csharp)
    file(GLOB QST_FILES "${CDF_SOURCE_DIR}/win32/csharp/Qst*")
    install(FILES ${QST_FILES}
	    DESTINATION ${CMAKE_INSTALL_PREFIX}/csharp)
    file(GLOB TXT_FILES "${CDF_SOURCE_DIR}/win32/csharp/HOWTO*.txt")
    install(FILES ${TXT_FILES}
	    DESTINATION ${CMAKE_INSTALL_PREFIX}/csharp)
    file(GLOB LSI_FILES "${CDF_SOURCE_DIR}/win32/csharp/CDFLeapSecondsInfo.*")
    install(FILES ${LSI_FILES}
	    DESTINATION ${CMAKE_INSTALL_PREFIX}/csharp)
  endif()
endif()

install(CODE "
  message(STATUS \"Executing final installation step...\")
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/Welcome.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/Release_notes.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/CDF_copyright.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/CHANGES.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/CDFLeapSeconds.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/lib)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/bin)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/include)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/HOWTO_run-CDF-Java-tools.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/HOWTO_run-CDF-Json-tools.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/HOWTO_run-CDF-XML-tools.txt)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/HOWTO_run-previous-version.txt)
")

if(MSVC)
 install(CODE "
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/CDFToolsDriver.jar)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/checkleapseconds.bat)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/checkleapseconds.ps1)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/setenv.bat)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/setenv.ps1)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/showenv.ps1)
  execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/toUTF8.ps1)
 ")
 if(BUILD_Csharp)
  install(CODE "
   execute_process(COMMAND ${CMAKE_COMMAND} -E touch_nocreate ${CMAKE_INSTALL_PREFIX}/csharp)
  ")
 endif()
endif()

file(GLOB CUR_EXTRA_DIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} {CMAKE_CURRENT_SOURCE_DIR}/*.h ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
set(CUR_EXTRA_DIST ${CUR_EXTRA_DIST} CMakeLists.txt)
