set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR})

add_sources_from_current_dir(utils
  utils/block_signal.c
  utils/cloexec.c
  utils/dputs.c
  utils/filename.c
  utils/fontspec.c
  utils/getticks.c
  utils/get_username.c
  utils/nonblock.c
  utils/pollwrap.c
  utils/signal.c
  # We want the ISO C implementation of ltime(), because we don't have
  # a local better alternative
  ../utils/ltime.c)

set(CURSES_NEED_WIDE TRUE)
find_package(Curses)
if(CURSES_FOUND)
  set(CURSES_WIDE TRUE)
else()
  set(CURSES_NEED_WIDE FALSE)
  find_package(Curses)
  set(CURSES_WIDE FALSE)
endif()
if(NOT CURSES_FOUND)
  message(FATAL_ERROR "Cannot build IPBT without a curses library")
endif()
set(CURSES_HAVE_CURSES_H ${CURSES_HAVE_CURSES_H} PARENT_SCOPE)
set(CURSES_HAVE_NCURSES_H ${CURSES_HAVE_NCURSES_H} PARENT_SCOPE)
set(CURSES_HAVE_NCURSES_CURSES_H ${CURSES_HAVE_NCURSES_CURSES_H} PARENT_SCOPE)
set(CURSES_HAVE_NCURSES_NCURSES_H ${CURSES_HAVE_NCURSES_NCURSES_H} PARENT_SCOPE)
set(CURSES_WIDE ${CURSES_WIDE} PARENT_SCOPE)

add_library(ipbtlib OBJECT
  ${CMAKE_SOURCE_DIR}/ipbtsrc/read.c
  ${CMAKE_SOURCE_DIR}/ipbtsrc/common.c
  unicode.c
  ${CMAKE_SOURCE_DIR}/settings.c
  ${CMAKE_SOURCE_DIR}/stubs/noprint.c
  ${CMAKE_SOURCE_DIR}/stubs/notiming.c)

add_executable(ipbt
  ipbt.c
  $<TARGET_OBJECTS:ipbtlib>)
be_list(ipbt ipbt)
target_include_directories(ipbt PRIVATE ${CURSES_INCLUDE_DIRS})
target_compile_options(ipbt PRIVATE ${CURSES_CFLAGS})
target_link_libraries(ipbt guiterminal charset utils ${CURSES_LIBRARIES})
installed_program(ipbt)

add_executable(ipbt-dump
  ${CMAKE_SOURCE_DIR}/ipbtsrc/dump.c
  $<TARGET_OBJECTS:ipbtlib>)
be_list(ipbt-dump ipbt-dump)
target_link_libraries(ipbt-dump guiterminal charset utils)
installed_program(ipbt-dump)
