# SPDX-License-Identifier: GPL-3.0-or-later
# myGPIOd (c) 2020-2026 Juergen Mang <mail@jcgames.de>
# https://github.com/jcorporation/mympd

add_executable(mygpiod "")

target_include_directories(mygpiod
  PRIVATE 
    "${PROJECT_BINARY_DIR}"
    "${PROJECT_SOURCE_DIR}"
    "${LIBGPIOD_INCLUDE_DIRS}"
)

target_sources(mygpiod
  PRIVATE
    main.c
    actions/actions.c
    actions/execute.c
    actions/gpio.c
    actions/system.c
    config/config.c
    config/gpio.c
    config/hook.c
    config/input_ev.c
    config/timer_ev.c
    event_loop/event_loop.c
    event_loop/signal_handler.c
    gpio/action.c
    gpio/chip.c
    gpio/event.c
    gpio/gpio.c
    gpio/input.c
    gpio/output.c
    gpio/timer.c
    gpio/util.c
    hook/action.c
    input_ev/action.c
    input_ev/device.c
    input_ev/event_code.c
    input_ev/event_type.c
    input_ev/event.c
    lib/events.c
    lib/json_print.c
    lib/list.c
    lib/log.c
    lib/sds_extras.c
    lib/timer.c
    raspberry/vcgencmd.c
    server_socket/gpio.c
    server_socket/hook.c
    server_socket/idle.c
    server_socket/protocol.c
    server_socket/raspberry.c
    server_socket/response.c
    server_socket/socket.c
    server_socket/timerev.c
    timer_ev/action.c
    timer_ev/event.c
    timer_ev/timer_ev.c
)

set(ENV{MYGPIOD_BUILDDIR} "${CMAKE_CURRENT_BINARY_DIR}")

execute_process(COMMAND "${PROJECT_SOURCE_DIR}/build.sh" create_includes RESULT_VARIABLE RC_CREATE_INCLUDES)
if(RC_CREATE_INCLUDES GREATER 0)
  message(FATAL_ERROR "Creating includes failed")
endif()

if (MYGPIOD_DEBUG)
  target_sources(mygpiod
    PRIVATE
      server_socket/event.c
  )
endif()

if(CMAKE_BUILD_TYPE MATCHES "(Release|RelWithDebInfo|MinSizeRel)")
  target_link_options(mygpiod
    PUBLIC
      "-pie"
  )
endif()

target_link_libraries(mygpiod
  "${LIBGPIOD_LIBRARIES}"
  sds
  mygpio-common
)

if(MYGPIOD_ENABLE_HTTPD)
  target_include_directories(mygpiod
    PRIVATE 
      "${LIBMHD_INCLUDE_DIRS}"
  )
  target_sources(mygpiod
    PRIVATE
      server_http/hook.c
      server_http/httpd.c
      server_http/rest_api_gpio.c
      server_http/rest_api_raspberry.c
      server_http/rest_api_timerev.c
      server_http/rest_api.c
      server_http/util.c
      server_http/webui.c
  )
  target_link_libraries(mygpiod
    "${LIBMHD_LIBRARIES}"
  )
endif()

if (MYGPIOD_ENABLE_ACTION_MPC)
  target_include_directories(mygpiod
    PRIVATE 
      "${LIBMPDCLIENT_INCLUDE_DIRS}"
  )
  target_sources(mygpiod PRIVATE
    actions/mpc.c
  )
  target_link_libraries(mygpiod
    "${LIBMPDCLIENT_LIBRARIES}"
  )
endif()

if (MYGPIOD_ENABLE_ACTION_HTTP)
  target_include_directories(mygpiod
    PRIVATE 
      "${LIBCURL_INCLUDE_DIRS}"
  )
  target_sources(mygpiod
    PRIVATE
      actions/http.c
      actions/mympd.c
      lib/http_client.c
  )
  target_link_libraries(mygpiod
    "${CURL_LIBRARIES}"
  )
endif()

if(MYGPIOD_ENABLE_ACTION_LUA)
  target_include_directories(mygpiod
    PRIVATE 
      "${LUA_INCLUDE_DIR}"
  )
  target_sources(mygpiod
    PRIVATE
      actions/lua_async.c
      actions/lua_sync.c
      config/lua_async.c
      event_loop/eventfd_wrap.c
      event_loop/msg_queue.c
      lua/async/functions/gpio.c
      lua/async/functions/input_ev.c
      lua/async/functions/system.c
      lua/async/bytecode.c
      lua/async/queue_msg.c
      lua/sync/functions/gpio.c
      lua/sync/functions/input_ev.c
      lua/sync/functions/system.c
      lua/sync/luavm.c
      lua/util.c
  )
  if (MYGPIOD_ENABLE_ACTION_MPC)
    target_sources(mygpiod
      PRIVATE
        lua/async/functions/mpc.c
        lua/sync/functions/mpc.c
    )
  endif()
  if(MYGPIOD_ENABLE_HTTPD)
    target_sources(mygpiod
      PRIVATE
        lua/async/functions/http.c
        lua/sync/functions/http.c
    )
  endif()
  target_link_libraries(mygpiod
    "${LUA_LIBRARIES}"
  )
endif()

install(TARGETS mygpiod DESTINATION ${CMAKE_INSTALL_FULL_BINDIR})
