#------------------------------------------------------------------------
# -*- 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
)

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
     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 0 )
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
#------------------------------------------------------------------------

add_executable( test_nds_logging
		test_nds_logging.c )
target_link_libraries( test_nds_logging
               ndsclient
               ${CMAKE_DL_LIBS} )

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

#------------------------------------------------------------------------
# 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
