# This file is part of the Owl Positioning System (OwlPS) project.
# It is subject to the copyright notice and license terms in the
# COPYRIGHT.t2t file found in the top-level directory of this
# distribution and at
#   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
# No part of the OwlPS Project, including this file, may be copied,
# modified, propagated, or distributed except according to the terms
# contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
# distributed along with this file, either separately or by replacing
# this notice by the COPYRIGHT.t2t file's contents.

### Options ###

option(OWLPS_LISTENER_USES_CONFIG_FILE
  "Enable support for configuration files"
  on)

option(OWLPS_LISTENER_USES_PTHREAD
  "Enable support for POSIX threads (mandatory for the autocalibration)"
  on)

option(OWLPS_LISTENER_KEEPS_MONITOR
  "Enable -K (keep monitor mode on); requires POSIX threads support"
  off)


### Linked libraries ###

# Make sure the pcap library is installed
find_package(Pcap)
if (NOT PCAP_FOUND)
  message(WARNING
    "OwlPS Listener dependency missing: libPcap")
  return()
endif()

include_directories(${PCAP_INCLUDE_DIRS})
set(EXTRA_LIBS ${EXTRA_LIBS} ${PCAP_LIBRARY})

if (OWLPS_LISTENER_USES_CONFIG_FILE)
  if (CONFUSE_FOUND)
    include_directories(${CONFUSE_INCLUDE_DIR})
    set(EXTRA_LIBS ${EXTRA_LIBS} ${CONFUSE_LIBRARY})
  else()
    message(WARNING
      "Confuse is not available: disabling OWLPS_LISTENER_USES_CONFIG_FILE")
    set(OWLPS_LISTENER_USES_CONFIG_FILE off CACHE BOOL
      "No libConfuse: disable OWLPS_LISTENER_USES_CONFIG_FILE" FORCE)
  endif()
endif()

if (OWLPS_LISTENER_USES_PTHREAD)
  if (HAVE_PTHREADS)
    set(EXTRA_LIBS ${EXTRA_LIBS} ${CMAKE_THREAD_LIBS_INIT})
  else()
    message(WARNING
      "POSIX threads are not available: disabling OWLPS_LISTENER_USES_PTHREAD")
    set(OWLPS_LISTENER_USES_PTHREAD off CACHE BOOL
      "No pthreads: disable OWLPS_LISTENER_USES_PTHREAD" FORCE)
  endif()
endif()

# Check if iwlib is available
find_package(Iwlib)
if (OWLPS_LISTENER_KEEPS_MONITOR)
  if (IWLIB_FOUND)
    include_directories(${IWLIB_INCLUDE_DIR})
    set(EXTRA_LIBS ${EXTRA_LIBS} ${IWLIB_LIBRARY})
  else()
    message(WARNING
      "iwlib is not available: disabling OWLPS_LISTENER_KEEPS_MONITOR")
    set(OWLPS_LISTENER_KEEPS_MONITOR off CACHE BOOL
      "No iwlib: disable OWLPS_LISTENER_KEEPS_MONITOR" FORCE)
  endif()
endif()


### Generate configuration header ###

configure_file(
  owlps-listener-config.h.in
  "${GENERATED_INCLUDE}/owlps-listener-config.h")


### Targets ###

add_executable(owlps-listenerd owlps-listenerd.c)
target_link_libraries(owlps-listenerd
  libowlps.so
  libowlps-client.so
  ${EXTRA_LIBS})
install(
  TARGETS owlps-listenerd
  DESTINATION bin)

add_executable(owlps-listenerd.semistatic EXCLUDE_FROM_ALL
  owlps-listenerd.c)
target_link_libraries(owlps-listenerd.semistatic
  libowlps.a
  libowlps-client.a
  ${EXTRA_LIBS})
# Add the semistatic target to the global list of semistatic targets
list(APPEND OWLPS_SEMISTATIC_TARGETS owlps-listenerd.semistatic)
set(OWLPS_SEMISTATIC_TARGETS ${OWLPS_SEMISTATIC_TARGETS} PARENT_SCOPE)

add_executable(owlps-listenerd.static EXCLUDE_FROM_ALL
  owlps-listenerd.c)
set_target_properties(owlps-listenerd.static
  PROPERTIES LINK_FLAGS -static)
target_link_libraries(owlps-listenerd.static
  libowlps.a
  libowlps-client.a
  ${EXTRA_LIBS})
# Add the static target to the global list of static targets
list(APPEND OWLPS_STATIC_TARGETS owlps-listenerd.static)
set(OWLPS_STATIC_TARGETS ${OWLPS_STATIC_TARGETS} PARENT_SCOPE)

# Add compile flags for the above targets
set_target_properties(
  owlps-listenerd
  owlps-listenerd.semistatic
  owlps-listenerd.static
  PROPERTIES COMPILE_FLAGS ${EXE_FLAGS})
