cmake_minimum_required(VERSION 3.16)
project(headlines)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CONFIGURATION_TYPES ${CMAKE_BUILD_TYPE} CACHE STRING "" FORCE)

option(DIST_BUILD "Distribution build" OFF)
option(FLATPAK_BUILD "Flatpak build" OFF)

IF(DIST_BUILD)
    add_definitions(-DDIST_BUILD=1)
    message("Making Dist build")
ENDIF(DIST_BUILD)

if(CMAKE_BUILD_TYPE MATCHES Debug)
    add_definitions(-DAPP_DEBUG=1)
    message("Debug Build")
endif()

if(CMAKE_BUILD_TYPE MATCHES Release OR CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
    add_definitions(-DAPP_RELEASE=1)
    message("Release Build")
endif()

set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-O3 -g")

find_package(PkgConfig REQUIRED)
find_program(GLIB_COMPILE_RESOURCES NAMES glib-compile-resources REQUIRED)

# compile resources
file(GLOB_RECURSE RESOURCE_SOURCES src/assets/*.ui src/assets/*.css)
set(GRESOURCE_C   io.gitlab.caveman250.headlines.resource.c)
set(GRESOURCE_XML src/assets/io.gitlab.caveman250.headlines.resource.xml)
add_custom_command(
        OUTPUT ${GRESOURCE_C}
        WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/assets
        COMMAND ${GLIB_COMPILE_RESOURCES}
        ARGS
        --generate-source
        --target=${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
        ../../${GRESOURCE_XML}
        VERBATIM
        MAIN_DEPENDENCY ${GRESOURCE_XML}
        DEPENDS ${RESOURCE_SOURCES}
)

set_source_files_properties(${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C} PROPERTIES SKIP_PRECOMPILE_HEADERS ON)

add_custom_target(
        project_resources
        DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
)

file(GLOB_RECURSE SOURCE src/*.cpp src/*.h)
source_group(TREE ${CMAKE_CURRENT_SOURCE_DIR} FILES ${SOURCE})
list(FILTER SOURCE EXCLUDE REGEX ".*WebSocket.cpp$")
list(FILTER SOURCE EXCLUDE REGEX ".*WebSocket.h$")

if (WIN32)
    add_library(websocket src/util/WebSocket.cpp src/util/WebSocket.h) #split this out so we dont need to optimize the entire build in windows debug builds.
    add_executable(headlines ${SOURCE} ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C} )
else()
    add_executable(headlines src/util/WebSocket.cpp src/util/WebSocket.h ${SOURCE} ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}) #make sure websocket builds first because it takes about as long as the rest of the source files combined (urgh boost)
endif()

set_source_files_properties(
        ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
        PROPERTIES GENERATED TRUE
)
add_dependencies(headlines project_resources)

if (NOT DIST_BUILD)
    #target_compile_options(headlines PRIVATE -Wall -Wextra -pedantic -Werror -Wno-error=overlength-strings)
endif()

if (WIN32)
target_link_libraries(headlines websocket)
endif()

include_directories(src)

pkg_check_modules(JSONCPP REQUIRED jsoncpp)
include_directories(${JSONCPP_INCLUDE_DIRS})
link_libraries(${JSONCPP_LIBRARIES})
target_link_libraries(headlines ${JSONCPP_LIBRARIES})

find_package(CURL REQUIRED REQUIRED)
include_directories(${CURL_INCLUDE_DIRS})
target_link_libraries(headlines ${CURL_LIBRARIES})

pkg_check_modules(GTKMM REQUIRED gtkmm-4.0)
include_directories(${GTKMM_INCLUDE_DIRS})
link_directories(${GTKMM_LIBRARY_DIRS})
target_link_libraries(headlines ${GTKMM_LIBRARIES})
add_definitions(${GTKMM_CFLAGS_OTHER})

pkg_check_modules(MICROHTTPD REQUIRED libmicrohttpd)
include_directories(${MICROHTTPD_INCLUDE_DIRS})
target_link_libraries(headlines ${MICROHTTPD_LIBRARIES})

pkg_check_modules(SECRET REQUIRED libsecret-1)
include_directories(${SECRET_INCLUDE_DIRS})
target_link_libraries(headlines ${SECRET_LIBRARIES})

pkg_check_modules(CRYPTO REQUIRED libcrypto)
target_link_libraries(headlines ${CRYPTO_LIBRARIES})

pkg_check_modules(SSL REQUIRED libssl)
target_link_libraries(headlines ${SSL_LIBRARIES})

pkg_check_modules(ADWAITA REQUIRED libadwaita-1)
include_directories(${ADWAITA_INCLUDE_DIRS})
target_link_libraries(headlines ${ADWAITA_LIBRARIES})

find_package(LibXml2 REQUIRED)
target_link_libraries(headlines ${LIBXML2_LIBRARIES})
include_directories(headlines ${LIBXML2_INCLUDE_DIRS})

if (WIN32)
    include_directories(headlines E:/dev/msys2/home/jwood/websocketpp)
    if(CMAKE_BUILD_TYPE MATCHES Debug)
        target_compile_options(websocket PRIVATE -O1) #mingw cant compile boost files without optimization urgh
    endif()
    target_link_libraries(headlines ws2_32)
else()
    find_package(websocketpp REQUIRED)
    include_directories(headlines ${WEBSOCKETPP_INCLUDE_DIRS})
endif()

set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(headlines Threads::Threads)

target_precompile_headers(headlines PRIVATE src/pch.h)

if (WIN32)
    target_precompile_headers(websocket PRIVATE src/pch.h)
endif()

set(APP_DIR share/applications/)
set(ICON_DIR share/icons/)
set(META_DIR share/metainfo/)
execute_process(COMMAND (eval echo ~${SUDO_USER}) OUTPUT_VARIABLE USER_HOME)
set(WORKING_DIR ${USER_HOME}/.headlines/)

install(FILES ${CMAKE_SOURCE_DIR}/io.gitlab.caveman250.headlines.desktop DESTINATION ${APP_DIR})
install(FILES ${CMAKE_SOURCE_DIR}/io.gitlab.caveman250.headlines.png DESTINATION ${ICON_DIR})

install(FILES ${CMAKE_SOURCE_DIR}/io.gitlab.caveman250.headlines.metainfo.xml DESTINATION ${META_DIR})

install(TARGETS headlines DESTINATION bin)
