#############################
# tools at src/tools
#############################

# See cdf/COPYRIGHT file for more info.
if(BUILD_SHARED_CDF AND WIN32)
  remove_definitions(-DDLL_EXPORT)
  remove_definitions(-DDLL_CDF)
endif()

include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include)

set(BASE_H ../include/cdf.h ../include/cdfconfig.h ../include/cdfdist.h ../include/cdflib.h ../include/cdflib64.h ../include/cdftools.h)

set(cdfcompare_FILES cdfcmp.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfcmp.h)

set(cdfconvert_FILES cdfcvt.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfcvt.h)

set(cdfdump_FILES cdfdump.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfdump.h)

set(cdfinquire_FILES cdfinq.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfinq.h)

set(cdfirsdump_FILES cdfirsdump.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfirsdump.h)

set(cdfleapsecondsinfo_FILES cdfleapsecondsinfo.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfleapsecondsinfo.h)

set(cdfmerge_FILES cdfmerge.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfmerge.h)

set(cdfstats_FILES cdfstats.c cdfstat1.c cdfstat2.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfstats.h)

set(cdfvalidate_FILES cdfvalidate.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdfvalidate.h)

set(skeletoncdf_FILES skt2cdf.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/skt2cdf.h)

set(skeletontable_FILES cdf2skt.c toolbox1.c toolbox3.c
    ${BASE_H} ../include/cdf2skt.h)

if(NOT MSVC)
  set(CURSES_NEED_NCURSES TRUE)
  set(CURSES_NEED_WIDE TRUE)
  if (NOT APPLE)
    find_package(Curses COMPONENTS system filesystem QUIET)
    if(CURSES_FOUND)
      set(HAVE_CURSES TRUE CACHE BOOL "Curse library is found.")
    else()
      set(HAVE_CURSES FALSE CACHE BOOL "Curse library is not found.")
    endif()
  else()
    set(HAVE_CURSES TRUE CACHE BOOL "Curse library is found.")
    set(NCURSES_INCLUDEDIR ${CDF_SOURCE_DIR}/macosx/ncurses/include)
  endif()
endif()

if(HAVE_CURSES OR MSVC)
 if(HAVE_CURSES)
  set(cdfedit_FILES cdfedit.c cdfedit1.c cdfedit2.c cdfedit3.c
      toolbox1.c toolbox3.c widgets.c windoz.c
      ${BASE_H} ../include/cdfedit.h ../include/widgets.h ../include/windoz.h
      ${NCURSES_INCLUDEDIR}/ncurses.h)

  set(cdfexport_FILES cdfxp.c cdfxp1.c cdfxp2.c cdfxp3.c toolbox1.c toolbox3.c
      widgets.c windoz.c 
      ${BASE_H} ../include/cdfxp.h ../include/widgets.h ../include/windoz.h
      ${NCURSES_INCLUDEDIR}/ncurses.h)
 else()
  set(cdfedit_FILES cdfedit.c cdfedit1.c cdfedit2.c cdfedit3.c
      toolbox1.c toolbox3.c widgets.c windoz.c
      ${BASE_H} ../include/cdfedit.h ../include/widgets.h ../include/windoz.h
      ${CDF_SOURCE_DIR}/win32/pdcurses/include/curses.h)

  set(cdfexport_FILES cdfxp.c cdfxp1.c cdfxp2.c cdfxp3.c 
      toolbox1.c toolbox3.c toolbox4.cpp widgets.c windoz.c 
      toolbox1.c toolbox3.c toolbox4.cpp widgets.c windoz.c 
      ${BASE_H} ../include/cdfxp.h ../include/widgets.h ../include/windoz.h
      ${CDF_SOURCE_DIR}/win32/pdcurses/include/curses.h)

#  set up for linking with dynamic libraries
   set(WINdefs WIN32 ALONE _CRT_SECURE_NO_WARNINGS)
   set(PDCURSESdefs _WINDOWS _WIN32 PDC_DLL_BUILD PDCURSES PDC_WIDE PDC_FORCE_UTF8)
 endif()
endif()

add_executable(cdfcompare ${cdfcompare_FILES})
add_executable(cdfconvert ${cdfconvert_FILES})
add_executable(cdfdump ${cdfdump_FILES})
add_executable(cdfinquire ${cdfinquire_FILES})
add_executable(cdfirsdump ${cdfirsdump_FILES})
add_executable(cdfleapsecondsinfo ${cdfleapsecondsinfo_FILES})
add_executable(cdfmerge ${cdfmerge_FILES})
add_executable(cdfstats ${cdfstats_FILES})
add_executable(cdfvalidate ${cdfvalidate_FILES})
add_executable(skeletoncdf ${skeletoncdf_FILES})
add_executable(skeletontable ${skeletontable_FILES})

if(HAVE_CURSES OR MSVC)
  add_executable(cdfedit ${cdfedit_FILES})
  add_executable(cdfexport ${cdfexport_FILES})
endif()

if(HAVE_CURSES OR MSVC)
 if(HAVE_CURSES)
	 include_directories(${NCURSES_INCLUDEDIR})
	 add_definitions(${NCURSES_CFLAGS})
 else()
  target_compile_definitions(cdfcompare PUBLIC ${WINdefs})
  target_compile_definitions(cdfconvert PUBLIC ${WINdefs})
  target_compile_definitions(cdfdump PUBLIC ${WINdefs})
  target_compile_definitions(cdfinquire PUBLIC ${WINdefs})
  target_compile_definitions(cdfirsdump PUBLIC ${WINdefs})
  target_compile_definitions(cdfleapsecondsinfo PUBLIC ${WINdefs})
  target_compile_definitions(cdfmerge PUBLIC ${WINdefs})
  target_compile_definitions(cdfstats PUBLIC ${WINdefs})
  target_compile_definitions(cdfvalidate PUBLIC ${WINdefs})
  target_compile_definitions(skeletoncdf PUBLIC ${WINdefs})
  target_compile_definitions(skeletontable PUBLIC ${WINdefs})
  target_compile_definitions(cdfedit PUBLIC ${WINdefs} ${PDCURSESdefs})
  target_compile_definitions(cdfexport PUBLIC ${WINdefs} ${PDCURSESdefs})
  target_include_directories(cdfedit PUBLIC ${CDF_SOURCE_DIR}/win32/pdcurses/include)
  target_include_directories(cdfexport PUBLIC ${CDF_SOURCE_DIR}/win32/pdcurses/include)
 endif()
endif()

if(ISLINUX)
# static
  target_link_libraries(cdfcompare cdf_static m) 
  target_link_libraries(cdfconvert cdf_static m)
  target_link_libraries(cdfdump cdf_static m)
  target_link_libraries(cdfinquire cdf_static m)
  target_link_libraries(cdfirsdump cdf_static m)
  target_link_libraries(cdfleapsecondsinfo cdf_static m)
  target_link_libraries(cdfmerge cdf_static m)
  target_link_libraries(cdfstats cdf_static m)
  target_link_libraries(cdfvalidate cdf_static m)
  target_link_libraries(skeletoncdf cdf_static m)
  target_link_libraries(skeletontable cdf_static m)
  if(HAVE_CURSES)
    target_link_libraries(cdfedit cdf_static ncursesw m)
    target_link_libraries(cdfexport cdf_static ncursesw m)
  endif()
elseif(NOT MSVC)
# dynamic
  target_link_libraries(cdfcompare cdf m) 
  target_link_libraries(cdfconvert cdf m)
  target_link_libraries(cdfdump cdf m)
  target_link_libraries(cdfinquire cdf m)
  target_link_libraries(cdfirsdump cdf m)
  target_link_libraries(cdfleapsecondsinfo cdf m)
  target_link_libraries(cdfmerge cdf m)
  target_link_libraries(cdfstats cdf m)
  target_link_libraries(cdfvalidate cdf m)
  target_link_libraries(skeletoncdf cdf m)
  target_link_libraries(skeletontable cdf m)
  if(HAVE_CURSES)
    if(NOT APPLE)
      target_link_libraries(cdfedit cdf ncursesw)
      target_link_libraries(cdfexport cdf ncursesw)
    else()
      target_link_libraries(cdfedit cdf ${CDF_SOURCE_DIR}/macosx/ncurses/lib/libncurses.a)
      target_link_libraries(cdfexport cdf ${CDF_SOURCE_DIR}/macosx/ncurses/lib/libncurses.a)
    endif()
  endif()
else()
# dynamic
string(FIND ${CMAKE_EXE_LINKER_FLAGS} "x64" is64Bit)
  if(is64Bit GREATER -1)
    target_link_libraries(cdfedit cdf ${CDF_SOURCE_DIR}/win32/pdcurses/x64/lib/pdcurses.lib ws2_32)
    target_link_libraries(cdfexport cdf ${CDF_SOURCE_DIR}/win32/pdcurses/x64/lib/pdcurses.lib ws2_32)
  else()
    target_link_libraries(cdfedit cdf ${CDF_SOURCE_DIR}/win32/pdcurses/x86/lib/pdcurses.lib ws2_32)
    target_link_libraries(cdfexport cdf ${CDF_SOURCE_DIR}/win32/pdcurses/x86/lib/pdcurses.lib ws2_32)
  endif()
  set_target_properties(cdfedit PROPERTIES LINK_FLAGS  "/NODEFAULTLIB:LIBCD /NODEFAULTLIB:Nafxcwd /NODEFAULTLIB:libcmt")
  set_target_properties(cdfexport PROPERTIES LINK_FLAGS "/NODEFAULTLIB:LIBCD /NODEFAULTLIB:Nafxcwd /NODEFAULTLIB:libcmt")
  target_link_libraries(cdfcompare cdf)
  target_link_libraries(cdfconvert cdf)
  target_link_libraries(cdfdump cdf)
  target_link_libraries(cdfinquire cdf)
  target_link_libraries(cdfirsdump cdf)
  target_link_libraries(cdfleapsecondsinfo cdf)
  target_link_libraries(cdfmerge cdf)
  target_link_libraries(cdfstats cdf)
  target_link_libraries(cdfvalidate cdf)
  target_link_libraries(skeletoncdf cdf)
  target_link_libraries(skeletontable cdf)
endif()

set(CMAKE_INSTALL_RPATH_USE_LINK_PATH FALSE)

####
# We have to do a little tweaking
# to remove the Release/ and Debug/ directories
# in MSVC builds.  This is required to get
# test scripts to work.
####
if(MSVC)
  macro(setbinprops name)
    set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY
      ${CMAKE_CURRENT_BINARY_DIR})
    set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG
      ${CMAKE_CURRENT_BINARY_DIR})
    set_target_properties(${name} PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE
      ${CMAKE_CURRENT_BINARY_DIR})
  endmacro()

  setbinprops(cdfcompare)
  setbinprops(cdfconvert)
  setbinprops(cdfdump)
  setbinprops(cdfedit)
  setbinprops(cdfexport)
  setbinprops(cdfinquire)
  setbinprops(cdfirsdump)
  setbinprops(cdfleapsecondsinfo)
  setbinprops(cdfmerge)
  setbinprops(cdfstats)
  setbinprops(cdfvalidate)
  setbinprops(skeletoncdf)
  setbinprops(skeletontable)
endif()

install(TARGETS cdfcompare DESTINATION bin COMPONENT utilities)
install(TARGETS cdfconvert DESTINATION bin COMPONENT utilities)
install(TARGETS cdfdump DESTINATION bin COMPONENT utilities)
if(HAVE_CURSES OR MSVC)
  install(TARGETS cdfedit DESTINATION bin COMPONENT utilities)
  install(TARGETS cdfexport DESTINATION bin COMPONENT utilities)
endif()
install(TARGETS cdfinquire DESTINATION bin COMPONENT utilities)
install(TARGETS cdfirsdump DESTINATION bin COMPONENT utilities)
install(TARGETS cdfleapsecondsinfo DESTINATION bin COMPONENT utilities)
install(TARGETS cdfmerge DESTINATION bin COMPONENT utilities)
install(TARGETS cdfstats DESTINATION bin COMPONENT utilities)
install(TARGETS cdfvalidate DESTINATION bin COMPONENT utilities)
install(TARGETS skeletoncdf DESTINATION bin COMPONENT utilities)
install(TARGETS skeletontable DESTINATION bin COMPONENT utilities)

