#------------------------------------------------------------------------
# -*- mode: cmake -*-
#------------------------------------------------------------------------
#------------------------------------------------------------------------
# global variables
#------------------------------------------------------------------------
set(POST_INSTALL "")
set( ndsclient_srcs
     nds_auth.c
     nds_log.c
     nds_logging.c
     nds_os.c
     nds1_access.c
     nds2_access.c
     daqc_access.c
     daqc_internal.c
     daqc_listener.c
     daqc_private.c
     trench.c
     daq_bsd_string.c
)

set( ndsclient_hdrs
     channel.h
     daqc.h
     daqc_response.h
     nds_logging.h
     trench.h
)

set( ndsclient_pvt_hdrs
     daqc_internal.h
     daqc_listener.h
     daqc_private.h
     daq_bsd_string.h
     nds_auth.h
     nds1.h
     nds2.h
)

set( optionalLibs )

#------------------------------------------------------------------------
# Find some pieces that will be needed
#------------------------------------------------------------------------
include( CheckIncludeFiles )

find_package(Threads)

find_package(SASL)
if( SASL_FOUND )
  include_directories( ${SASL_INCLUDE_DIR} )
  set(optionalLibs ${SASL_LIBRARY} ${optionalLibs})
  set(HAVE_SASL 1)
endif( SASL_FOUND )
set(SASL_FOUND ${SASL_FOUND} PARENT_SCOPE)

find_package(GSSAPI)
if( GSSAPI_FOUND )
  include_directories( ${GSSAPI_INCLUDE_DIR} )
  set(optionalLibs ${GSSAPI_LIBRARY} ${optionalLibs})
endif( GSSAPI_FOUND )
if( WIN32 )
  SET(optionalLibs ${optionalLibs} Ws2_32)
endif( WIN32 )

#------------------------------------------------------------------------
#
#------------------------------------------------------------------------

check_symbol_exists(closesocket "winsock2.h;ws2def.h" HAVE_CLOSESOCKET)
check_symbol_exists(snprintf_s "stdio.h" HAVE_SNPRINTF_S)
check_symbol_exists(sprintf_s  "stdio.h" HAVE_SPRINTF_S)
check_symbol_exists(vsnprintf_s "stdio.h" HAVE_VSNPRINTF_S)

check_symbol_exists(asprintf "stdio.h" HAVE_ASPRINTF)
check_symbol_exists(vasprintf "stdio.h" HAVE_VASPRINTF)
check_symbol_exists(va_copy "stdarg.h" HAVE_VA_COPY)
check_symbol_exists(__va_copy "stdarg.h" HAVE___VA_COPY)

#------------------------------------------------------------------------
# Generate the system configuration file
#------------------------------------------------------------------------

include_directories( ${CMAKE_CURRENT_BINARY_DIR} )
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/config_cmake.h.in
                ${CMAKE_CURRENT_BINARY_DIR}/daq_config.h )
add_definitions( "-DHAVE_CONFIG_H=1" )

#------------------------------------------------------------------------
# These are the pieces that will be built
#------------------------------------------------------------------------
cmake_policy(PUSH)
cmake_policy(SET CMP0003 NEW)
add_library(ndsclient SHARED ${ndsclient_srcs})
target_link_libraries(ndsclient ${CMAKE_THREAD_LIBS_INIT})
set( install_libs ndsclient )
add_library(ndsclientStatic STATIC ${ndsclient_srcs})
target_link_libraries(ndsclientStatic ${CMAKE_THREAD_LIBS_INIT})
if( UNIX )
  set( install_libs ${install_libs} ndsclientStatic )
  set_target_properties(ndsclientStatic PROPERTIES OUTPUT_NAME ndsclient)
endif( UNIX )
cmake_policy(POP)

target_link_libraries(ndsclient ${optionalLibs})

set( NDSCLIENT_SOVERSION_CURRENT  4 )
set( NDSCLIENT_SOVERSION_REVISION 1 )
set( NDSCLIENT_SOVERSION_AGE      3 )

if ( UNIX )
  nds_libtool_version_calculation( NDSCLIENT
    ${NDSCLIENT_SOVERSION_CURRENT}
    ${NDSCLIENT_SOVERSION_REVISION}
    ${NDSCLIENT_SOVERSION_AGE} )
  set_target_properties( ndsclient PROPERTIES
    VERSION ${NDSCLIENT_VERSION}
    SOVERSION ${NDSCLIENT_SOVERSION} )
endif ( UNIX )

#------------------------------------------------------------------------
# Testing - CTest
#------------------------------------------------------------------------
#------------------------------------------------------------------------
function(build_test command)
    add_executable(${command} tests/${command}.cc)
    target_include_directories(${command} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
    target_requires_cpp11( ${command} PUBLIC )
    target_link_libraries(${command} ndsclient ${CMAKE_DL_LIBS})
endfunction(build_test)
function(replay_server_test_nobuild name command json)
  if( PYTHON_EXECUTABLE )
    if( WIN32 )
      set(CMD_PATH ${EXECUTABLE_OUTPUT_PATH})
    else( WIN32 )
      set(CMD_PATH ${CMAKE_CURRENT_BINARY_DIR})
    endif( WIN32 )
    if ( REPLAY_BLOB_CACHE_DIR )
      set_nds_protocols(protocols ${name})
      foreach ( proto ${protocols} )
        set(test_name "${name}${proto}_")
        add_test(
          NAME ${test_name}
          COMMAND
          ${PROG_ENV} "REPLAY_BLOB_REMOTE_CACHE=${REPLAY_BLOB_CACHE_DIR}"
          "${PYTHON_EXECUTABLE}" "${PROG_NDS_TESTER}" ${NDS_TESTER_FLAGS}
          --replay-server
          --replay-json-filename ${json}
          --replay-protocol ${proto}
          "$<TARGET_FILE:${command}>" ${ARGN} )
        set_tests_properties( ${test_name}
          PROPERTIES
          SKIP_RETURN_CODE 77
          TIMEOUT 180 )
        set( NDS_CUSTOM_MEMCHECK_IGNORE
          ${NDS_CUSTOM_MEMCHECK_IGNORE} ${test_name}
          CACHE INTERNAL "" )
      endforeach ( proto )
    endif ( REPLAY_BLOB_CACHE_DIR )
  endif( PYTHON_EXECUTABLE )
endfunction(replay_server_test_nobuild)

build_test( test_daq_availability_check )
replay_server_test_nobuild(
  _daq_nds2_test_daq_availability_check_
  test_daq_availability_check
  test_daq_availability_check.json
  )

add_executable( test_nds_logging
		tests/test_nds_logging.c )
target_include_directories( test_nds_logging PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR} )
target_link_libraries( test_nds_logging
               ndsclient
               ${CMAKE_DL_LIBS} )

add_executable( test_bsd_string
        tests/test_main.cc
        tests/test_bsd_string.cc
        daq_bsd_string.c)
target_requires_cpp11( test_bsd_string PUBLIC )
target_include_directories( test_bsd_string PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}
        ${CMAKE_CURRENT_SOURCE_DIR}/tests )
target_link_libraries( test_bsd_string PUBLIC catch )

add_test( NAME NDSLoggingParse
	  COMMAND test_nds_logging --parse )
add_test( NAME NDSLoggingQuietInit
	  COMMAND test_nds_logging --quiet-init )
add_test( NAME NDSBsdStringsTest
      COMMAND test_bsd_string )

#------------------------------------------------------------------------
# Create the package configure files
#------------------------------------------------------------------------
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/nds2-client.cfg.in
                ${CMAKE_CURRENT_BINARY_DIR}/nds2-client.cfg
		@ONLY )
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/nds2-client.pc.in
                ${CMAKE_CURRENT_BINARY_DIR}/nds2-client.pc @ONLY)
configure_file( ${CMAKE_CURRENT_SOURCE_DIR}/nds2-client-uninstalled.pc.cmake
                ${CMAKE_CURRENT_BINARY_DIR}/nds2-client-uninstalled.pc )
install(
  FILES ${CMAKE_CURRENT_BINARY_DIR}/nds2-client.pc
  DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
  COMPONENT Development
)
#========================================================================
# Installation - CPack
#========================================================================
install(
  TARGETS ${install_libs}
  RUNTIME
    DESTINATION ${RUNTIME_DESTINATION}
    COMPONENT applications
  LIBRARY
    DESTINATION ${LIBRARY_DESTINATION}
    COMPONENT applications
  ARCHIVE
    DESTINATION ${CMAKE_INSTALL_LIBDIR}
    COMPONENT Development
)

install(
  FILES ${ndsclient_hdrs}
  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
  COMPONENT Development
)

if( NOT WIN32)
  install(
    FILES ${CMAKE_CURRENT_BINARY_DIR}/nds2-client.cfg
    DESTINATION ${CMAKE_INSTALL_FULL_SYSCONFDIR}/${PROJECT_NAME}
    COMPONENT applications
    )
endif( NOT WIN32 )

nds_post_install( )

# Thanks to https://cmake.org/cmake/help/v3.0/variable/CMAKE_DL_LIBS.html
# and http://stackoverflow.com/questions/1274529/building-project-involving-cmake-how-do-i-make-it-aware-of-libraries
