# Define the current source locations
set(CURRENT_HEADER_DIR ${CMAKE_SOURCE_DIR}/include/leddevice)
set(CURRENT_SOURCE_DIR ${CMAKE_SOURCE_DIR}/libsrc/leddevice)

if(ENABLE_DEV_NETWORK)
	find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Network REQUIRED)
endif()

if(ENABLE_DEV_SERIAL)
	find_package(Qt${QT_VERSION_MAJOR} COMPONENTS SerialPort REQUIRED)
endif()

include_directories(
	dev_hid
	dev_net
	dev_other
	dev_serial
	dev_spi
	dev_rpi_pwm
	dev_tinker
)

file (GLOB Leddevice_SOURCES
	"${CURRENT_HEADER_DIR}/*.h"
	"${CURRENT_SOURCE_DIR}/*.h"
	"${CURRENT_SOURCE_DIR}/*.cpp"
	"${CURRENT_SOURCE_DIR}/dev_other/*.h"
	"${CURRENT_SOURCE_DIR}/dev_other/*.cpp"
)

if(ENABLE_OSX OR WIN32)
	list(REMOVE_ITEM Leddevice_SOURCES "${CURRENT_SOURCE_DIR}/dev_other/LedDevicePiBlaster.h")
	list(REMOVE_ITEM Leddevice_SOURCES "${CURRENT_SOURCE_DIR}/dev_other/LedDevicePiBlaster.cpp")
endif()

if(ENABLE_DEV_NETWORK)
	file (GLOB Leddevice_NETWORK_SOURCES "${CURRENT_SOURCE_DIR}/dev_net/*.h" "${CURRENT_SOURCE_DIR}/dev_net/*.cpp")
endif()

if(ENABLE_DEV_SERIAL)
	file (GLOB Leddevice_SERIAL_SOURCES "${CURRENT_SOURCE_DIR}/dev_serial/*.h" "${CURRENT_SOURCE_DIR}/dev_serial/*.cpp")
endif()

if(ENABLE_DEV_SPI)
	file (GLOB Leddevice_SPI_SOURCES "${CURRENT_SOURCE_DIR}/dev_spi/*.h" "${CURRENT_SOURCE_DIR}/dev_spi/*.cpp")
endif()

if(ENABLE_DEV_TINKERFORGE)
	file (GLOB Leddevice_TINKER_SOURCES "${CURRENT_SOURCE_DIR}/dev_tinker/*.h" "${CURRENT_SOURCE_DIR}/dev_tinker/*.cpp")
endif()

if(ENABLE_DEV_USB_HID)
	find_package(libusb-1.0 REQUIRED)
	include_directories(
		${CMAKE_SOURCE_DIR}/include/hidapi
		${LIBUSB_1_INCLUDE_DIRS}
	)
	file (GLOB Leddevice_USB_HID_SOURCES "${CURRENT_SOURCE_DIR}/dev_hid/*.h" "${CURRENT_SOURCE_DIR}/dev_hid/*.cpp")
endif()

if(ENABLE_DEV_WS281XPWM)
	file (GLOB Leddevice_PWM_SOURCES "${CURRENT_SOURCE_DIR}/dev_rpi_pwm/*.h" "${CURRENT_SOURCE_DIR}/dev_rpi_pwm/*.cpp")
endif()

set(LedDevice_RESOURCES ${CURRENT_SOURCE_DIR}/LedDeviceSchemas.qrc)

set(Leddevice_SOURCES
	${Leddevice_SOURCES}
	${LedDevice_RESOURCES}
	${Leddevice_NETWORK_SOURCES}
	${Leddevice_PWM_SOURCES}
	${Leddevice_SERIAL_SOURCES}
	${Leddevice_SPI_SOURCES}
	${Leddevice_TINKER_SOURCES}
	${Leddevice_USB_HID_SOURCES}
)

# auto generate header file that include all available leddevice headers
# auto generate cpp file for register() calls
file (WRITE "${CMAKE_BINARY_DIR}/LedDevice_headers.h" "#pragma once\n\n//this file is autogenerated, don't touch it\n\n")
file (WRITE "${CMAKE_BINARY_DIR}/LedDevice_register.cpp" "//this file is autogenerated, don't touch it\n\n")
foreach(f ${Leddevice_SOURCES})
	# message (STATUS "Add led device: ${f}")
	if("${f}" MATCHES "dev_.*/Led.evice.+h$")
		GET_FILENAME_COMPONENT(fname ${f} NAME)
		file (APPEND "${CMAKE_BINARY_DIR}/LedDevice_headers.h" "#include \"${fname}\"\n")
		string(SUBSTRING ${fname} 9 -1 dname)
		string(REPLACE ".h" "" dname "${dname}")
		file (APPEND "${CMAKE_BINARY_DIR}/LedDevice_register.cpp" "REGISTER(${dname});\n")
	endif()
endforeach()

add_library(leddevice ${CMAKE_BINARY_DIR}/LedDevice_headers.h ${Leddevice_SOURCES})

target_link_libraries(leddevice
	hyperion
	hyperion-utils
	${CMAKE_THREAD_LIBS_INIT}
)

if(WIN32)
	target_link_libraries(leddevice ws2_32)
endif()

if(ENABLE_DEV_NETWORK)
	target_link_libraries(leddevice Qt${QT_VERSION_MAJOR}::Network ssdp)

	if(NOT DEFAULT_USE_SYSTEM_MBEDTLS_LIBS)
		if(MBEDTLS_LIBRARIES)
			include_directories(${MBEDTLS_INCLUDE_DIR})
			target_link_libraries(leddevice ${MBEDTLS_LIBRARIES})
			target_include_directories(leddevice PRIVATE ${MBEDTLS_INCLUDE_DIR})
		endif (MBEDTLS_LIBRARIES)
	endif()

	string(REGEX MATCH "[0-9]+|-([A-Za-z0-9_.]+)" MBEDTLS_MAJOR ${MBEDTLS_VERSION})
	if(MBEDTLS_MAJOR EQUAL "3")
		target_compile_definitions(leddevice PRIVATE USE_MBEDTLS3)
		if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
			target_compile_features(leddevice PRIVATE cxx_std_20)
		endif()
	endif()
endif()

if(ENABLE_DEV_SERIAL)
	target_link_libraries(leddevice Qt${QT_VERSION_MAJOR}::SerialPort)
endif()

if(ENABLE_DEV_TINKERFORGE)
	target_link_libraries(leddevice tinkerforge)
endif()

if(ENABLE_DEV_WS281XPWM)
	target_include_directories(leddevice PUBLIC "${CMAKE_SOURCE_DIR}/dependencies/external/rpi_ws281x")
	target_link_libraries(leddevice ws281x)
endif()

if(ENABLE_DEV_USB_HID)
	if(APPLE)
		target_link_libraries(leddevice ${LIBUSB_1_LIBRARIES} hidapi-mac)
	else()
		target_link_libraries(leddevice ${LIBUSB_1_LIBRARIES} hidapi-libusb)
	endif()
	if(NOT WIN32)
		include(CheckCSourceCompiles)
		# Glibc compatibilty check
		check_c_source_compiles("
			#include <time.h>
			#include <sys/time.h>

			int main() {
				struct timespec t;
				return clock_gettime(CLOCK_REALTIME, &t);
			}
			" GLIBC_HAS_CLOCK_GETTIME)
		if(NOT GLIBC_HAS_CLOCK_GETTIME)
			target_link_libraries(leddevice rt)
		endif()
	endif()
endif()

if(ENABLE_MDNS)

 	target_link_libraries(leddevice mdns)
endif()