cmake_minimum_required(VERSION 3.7)
project(doit LANGUAGES C)

if(NOT CMAKE_SYSTEM_NAME MATCHES "Windows")
  # On Unix, build the doit client.

  add_executable(doitclient doitclient.c doitlib.c)
  add_executable(protocol-test doitlib.c)
  target_compile_definitions(protocol-test PRIVATE TESTMODE)

  set(GENERATED_SOURCES_DIR ${CMAKE_CURRENT_BINARY_DIR}${CMAKE_FILES_DIRECTORY})
  include_directories(${GENERATED_SOURCES_DIR})
  configure_file(cmake.h.in ${GENERATED_SOURCES_DIR}/cmake.h)

  include(CheckSymbolExists)
  check_symbol_exists(realpath "limits.h;stdlib.h" HAVE_REALPATH)

  # Installation
  include(GNUInstallDirs)
  if(CMAKE_VERSION VERSION_LESS 3.14)
    # CMake 3.13 and earlier required an explicit install destination.
    install(TARGETS doitclient RUNTIME DESTINATION bin)
  else()
    # 3.14 and above selects a sensible default, which we should avoid
    # overriding here so that end users can override it using
    # CMAKE_INSTALL_BINDIR.
    install(TARGETS doitclient)
  endif()
endif()

if(CMAKE_SYSTEM_NAME MATCHES "Windows")
  # On Windows, build the server.

  if(CMAKE_C_COMPILER_ID MATCHES "MSVC" OR
     CMAKE_C_COMPILER_FRONTEND_VARIANT MATCHES "MSVC")
    set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} /nologo /C1252")
  else()
    set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} -c1252")
  endif()

  add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
  add_executable(doit doit.c listener.c doitlib.c msgbuf.c winerror.c doit.rc)
  target_link_libraries(doit gdi32.lib user32.lib wsock32.lib shell32.lib)
  set_target_properties(doit PROPERTIES WIN32_EXECUTABLE ON)
endif()
