cmake_minimum_required(VERSION 3.7)
project(ubinstall-gtk)

find_package(PkgConfig REQUIRED)

pkg_check_modules(GTK REQUIRED gtk+-3.0)
include_directories(${GTK_INCLUDE_DIRS})
link_directories(${GTK_LIBRARY_DIRS})
add_definitions(${GTK_CFLAGS_OTHER})

pkg_check_modules(VTE291 REQUIRED vte-2.91)
include_directories(${VTE291_INCLUDE_DIRS})
link_directories(${VTE291_LIBRARY_DIRS})
add_definitions(${VTE291_CFLAGS_OTHER})

pkg_check_modules(JSON REQUIRED json-c)
include_directories(${JSON_INCLUDE_DIRS})
link_directories(${JSON_LIBRARY_DIRS})
add_definitions(${JSON_CFLAGS_OTHER})

find_library(WEBKIT_LIBRARIES_FOUND webkit2gtk-4.0 webkit2gtk-web-extension-4.0)

option(WEBKIT_FOUND "No" OFF)
if(WEBKIT_LIBRARIES_FOUND)
        option(WEBKIT_FOUND "Yes" ON)
        PKG_CHECK_MODULES(WEBKIT REQUIRED webkit2gtk-4.0 webkit2gtk-web-extension-4.0)
        include_directories(${WEBKIT_INCLUDE_DIRS})
        link_directories(${WEBKIT_LIBRARY_DIRS})
        add_definitions(${WEBKIT_CFLAGS_OTHER})
endif()

set(GRESOURCE_C   resources.c)
set(GRESOURCE_XML gresource.xml)

find_program(GLIB_COMPILE_RESOURCES NAMES glib-compile-resources REQUIRED)  
add_custom_target(GLADE ubinstall-gtk.glade)

set(DEPENDFILES 
                ../images/keyboard.png
                ../images/keyboard-ru.png
                ../images/map-time-zone.png
                ../images/slide-0.png
                ../images/slide-1.png
                ../images/slide-2.png
                ../images/slide-3.png
                ../images/slide-4.png
                ../images/slide-5.png
                ../images/slide-6.png
                ../images/slide-7.png
                ../images/slide-8.png
                ../images/slide-9.png
                ../images/slide-10.png
                ../images/slide-11.png
                ../images/slide-12.png
                ../images/slide-12.png
                ../images/near_install_disk.png
                ../images/in_part_install_disk.png
                ../images/clear_install_disk.png
                ../images/language.png
                ../ubinstall-gtk.glade
                ../ubinstall-gtk-language.glade
                ../ubinstall-gtk-keyboard.glade
                ../gresource.xml
                ../ubinstall-gtk.css
                ../modules.csv
        )

file(COPY ${DEPENDFILES} DESTINATION ${CMAKE_CURRENT_BINARY_DIR})

add_custom_command(
    OUTPUT ${GRESOURCE_C}
    WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
    COMMAND ${GLIB_COMPILE_RESOURCES}
    ARGS
        --generate-source
        --target=${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
        ${GRESOURCE_XML}
    VERBATIM
    MAIN_DEPENDENCY ${GRESOURCE_XML}
    DEPENDS
        ${GLADE}
)
add_custom_target(
    dummy-resource
    DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
)

add_definitions(-DVTE_INCLUDE)

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror -Wmissing-declarations -fdiagnostics-color=always \
	-O2 -pipe -fno-plt -fexceptions \
        -Wp,-D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security \
        -fstack-clash-protection -fcf-protection")
        
set(SOURCE_FILES
        ubinstall-gtk.c
        ubinstall-gtk.h
        ubl-strings.h
        )

set(LIBRARIES
        ${GTK_LIBRARIES}
        ${WEBKIT_LIBRARIES}
        ${UTILS_LIBRARIES}
        ${VTE291_LIBRARIES}
        ${JSON_LIBRARIES}
        pthread
        ublsettings
        ublsettings-gtk3
        ublsettingsui-gtk3
        )


add_executable(${PROJECT_NAME} ${SOURCE_FILES} ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C})
target_link_libraries(${PROJECT_NAME} PUBLIC ${LIBRARIES})
target_include_directories(${PROJECT_NAME} PUBLIC ${PROJECT_BINARY_DIR})
set_source_files_properties(
    ${CMAKE_CURRENT_BINARY_DIR}/${GRESOURCE_C}
    PROPERTIES GENERATED TRUE
)
install(TARGETS ${PROJECT_NAME} DESTINATION bin)
add_dependencies(${PROJECT_NAME} dummy-resource)