# led-controller for the desktop: the firmware's sources built with a plain C compiler, no ESP-IDF.
# Build from the repository root with `make desktop`, or here:
#   cmake -S desktop -B build-desktop && cmake --build build-desktop
# Needs www_dist/www.pack (tools/build_web.sh). Third-party code is vendored: desktop/tools/vendor.sh.
cmake_minimum_required(VERSION 3.16)
project(led_controller_desktop VERSION 0.1.0 LANGUAGES C)

set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS ON)
find_package(Threads REQUIRED)

set(ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..)
set(WWW_PACK ${ROOT}/www_dist/www.pack)
if(NOT EXISTS ${WWW_PACK})
    message(FATAL_ERROR "www_dist/www.pack is missing: run tools/build_web.sh (make web)")
endif()
# The simulated rig at /sim/ (tools/build_sim_web.sh). Optional: an empty pack when it is not built.
set(SIM_PACK ${ROOT}/www_dist/sim.pack)
if(NOT EXISTS ${SIM_PACK})
    file(WRITE ${SIM_PACK} "WWW1")
endif()

execute_process(COMMAND git -C ${ROOT} describe --tags --always --dirty
                OUTPUT_VARIABLE LED_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE ERROR_QUIET)
if(NOT LED_VERSION)
    set(LED_VERSION "desktop")
endif()

# The firmware's app sources (the same list as main/CMakeLists.txt's linux branch, plus the bridge transport)
set(MAIN_SRC
    beat.c beat_dsp.c espnow_tx.c bridge.c store.c api.c web.c envelope.c engine.c rel.c bench.c audio.c
    res_profile.c res_misc.c res_device.c res_group.c seqplay.c res_bundle.c audio_api.c patterns.c outputs.c
    ws.c ws_client.c wifi_drv.c bridge_serial.c)
list(TRANSFORM MAIN_SRC PREPEND ${ROOT}/main/)

# The host stand-ins for the board's hardware (mic, OTA, littlefs, timers) and the app_main that starts it
set(SIM_SRC sim_main.c sim_stubs.c sim_timer.c sim_rig.c)
list(TRANSFORM SIM_SRC PREPEND ${ROOT}/sim-controller/components/sim/)

set(THIRD_PARTY
    third_party/esp_http_server/src/httpd_main.c
    third_party/esp_http_server/src/httpd_parse.c
    third_party/esp_http_server/src/httpd_sess.c
    third_party/esp_http_server/src/httpd_txrx.c
    third_party/esp_http_server/src/httpd_uri.c
    third_party/esp_http_server/src/httpd_ws.c
    third_party/esp_http_server/src/util/ctrl_sock.c
    third_party/http_parser/http_parser.c
    third_party/cjson/cJSON.c)

set(PORT
    port/src/freertos.c
    port/src/esp_glue.c
    port/src/desktop_crypto.c
    port/src/www_pack.c
    port/src/sim_pack.c
    port/src/mdns_host.c
    port/src/mic_alsa.c)

add_executable(led-controller desktop_main.c ${MAIN_SRC} ${SIM_SRC} ${THIRD_PARTY} ${PORT})
# www_pack.c embeds the pack with .incbin, which CMake can't see: rebuild it when the pack changes
set_source_files_properties(port/src/www_pack.c PROPERTIES OBJECT_DEPENDS ${WWW_PACK})
set_source_files_properties(port/src/sim_pack.c PROPERTIES OBJECT_DEPENDS ${SIM_PACK})
target_include_directories(led-controller PRIVATE
    port/include
    ${ROOT}/main
    ${ROOT}/bridge/main
    ${ROOT}/sim-controller/components/sim
    ${ROOT}/sim-controller/components/sim/include
    third_party/esp_http_server/include
    third_party/esp_http_server/src
    third_party/esp_http_server/src/util
    third_party/http_parser
    third_party/cjson)
target_compile_definitions(led-controller PRIVATE
    _GNU_SOURCE
    LED_DESKTOP_MDNS
    LED_DESKTOP_ALSA
    LED_APP_VERSION="${LED_VERSION}"
    WWW_PACK="${WWW_PACK}"
    SIM_PACK="${SIM_PACK}")
# ESP-IDF force-includes the build's sdkconfig.h into every source; the desktop port has the same file
target_compile_options(led-controller PRIVATE -include ${CMAKE_CURRENT_SOURCE_DIR}/port/include/sdkconfig.h
                       -Wall -Wno-unused-function -Wno-unused-variable -Wno-unused-parameter)
find_library(ASOUND_LIB asound REQUIRED)  # ALSA capture for the microphone (libasound2-dev at build time)
target_link_libraries(led-controller PRIVATE Threads::Threads m rt ${ASOUND_LIB})
install(TARGETS led-controller RUNTIME DESTINATION bin)

# Headless install: a .deb with the program, a systemd service and its default settings (`make deb`).
# The service runs as a dynamic user with its store in /var/lib/led-controller.
set(CPACK_PACKAGING_INSTALL_PREFIX /usr)
set(CMAKE_INSTALL_PREFIX /usr)
install(FILES packaging/led-controller.service DESTINATION lib/systemd/system)
install(FILES packaging/led-controller.default DESTINATION /etc/default RENAME led-controller)
install(FILES packaging/led-bridge.udev.example DESTINATION share/doc/led-controller)
install(FILES ${ROOT}/README.md DESTINATION share/doc/led-controller RENAME README.md OPTIONAL)
install(FILES ${ROOT}/bridge/README.md DESTINATION share/doc/led-controller RENAME bridge-README.md OPTIONAL)

# The command-line tool (led-tool, a PyInstaller binary) and the LED driver firmware image (led-driver, built with
# ESP-IDF v6.1). Both are bundled into the .deb when LED_BUNDLE_TOOLS is on (`make deb` turns it on).
option(LED_BUNDLE_TOOLS "Install led-cli and the led-driver firmware image into the package" OFF)
set(LED_CLI_BIN ${ROOT}/led-tool/dist/led-cli)
set(LED_DRIVER_BIN ${ROOT}/led-driver/build-v61/led_driver.bin)
if(LED_BUNDLE_TOOLS)
    if(NOT EXISTS ${LED_CLI_BIN})
        message(FATAL_ERROR "led-tool/dist/led-cli is missing: run make led-cli")
    endif()
    if(NOT EXISTS ${LED_DRIVER_BIN})
        message(FATAL_ERROR "led-driver/build-v61/led_driver.bin is missing: build the driver with the ESP-IDF v6.1 image")
    endif()
    install(PROGRAMS ${LED_CLI_BIN} DESTINATION bin)
    install(FILES ${LED_DRIVER_BIN} DESTINATION share/led-controller/firmware)
endif()

set(CPACK_PACKAGE_NAME "led-controller")
set(CPACK_PACKAGE_VERSION "${PROJECT_VERSION}")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "LED rig controller: web UI, presets, sequences and ESP-NOW to the LED drivers")
set(CPACK_PACKAGE_CONTACT "led-controller packagers <packaging@localhost>")
set(CPACK_GENERATOR "DEB")
set(CPACK_DEBIAN_PACKAGE_SECTION "misc")
set(CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
set(CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
set(CPACK_DEBIAN_FILE_NAME DEB-DEFAULT)
set(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA ${CMAKE_CURRENT_SOURCE_DIR}/packaging/postinst
                                       ${CMAKE_CURRENT_SOURCE_DIR}/packaging/prerm
                                       ${CMAKE_CURRENT_SOURCE_DIR}/packaging/postrm
                                       ${CMAKE_CURRENT_SOURCE_DIR}/packaging/conffiles)
include(CPack)
