cmake_minimum_required(VERSION 3.26)
project(fileserver)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED YES)

add_subdirectory(lib EXCLUDE_FROM_ALL)

find_package(Threads REQUIRED)

add_custom_command(
    COMMAND ./mrpc ARGS -n src/server/mrpc/fileserver -s cpp fileserver.rs
    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
    MAIN_DEPENDENCY fileserver.rs
    OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/server/mrpc/fileserver.cxx ${CMAKE_CURRENT_SOURCE_DIR}/src/server/mrpc/fileserver.hxx
)

add_custom_command(
    COMMAND sh ARGS -c 'xxd -i -n index_html ${CMAKE_CURRENT_SOURCE_DIR}/frontend/dist/index.html > index_html.h'
    MAIN_DEPENDENCY frontend/dist/index.html
    OUTPUT index_html.h
)

add_custom_command(
    COMMAND sh ARGS -c 'xxd -i -n favicon_svg ${CMAKE_CURRENT_SOURCE_DIR}/frontend/dist/favicon.svg > favicon_svg.h'
    MAIN_DEPENDENCY frontend/dist/favicon.svg
    OUTPUT favicon_svg.h
)

add_executable(fileserver
    src/server/mrpc/fileserver.hxx
    src/server/mrpc/fileserver.cxx

    src/util/logging.hxx
    src/util/crash.hxx
    src/util/timed_mutex.hxx
    src/util/stb.cxx

    src/data/data.hxx
    src/data/data_internal.hxx
    src/data/data.cxx
    src/data/data_load.cxx
    src/data/data_save.cxx
    src/data/data_validate.cxx

    src/server/server.hxx
    src/server/server_internal.hxx
    src/server/server.cxx
    src/server/admin.cxx
    src/server/auth.cxx
    src/server/mail.cxx
    src/server/fs.cxx
    src/server/download.cxx
    src/server/upload.cxx

    src/main.cxx

    index_html.h
    favicon_svg.h
)

target_include_directories(fileserver PRIVATE include ${CMAKE_CURRENT_BINARY_DIR})
target_link_libraries(fileserver PRIVATE fileserver_libs Threads::Threads)
target_compile_options(fileserver PRIVATE -msse2)
target_link_options(fileserver PRIVATE -static)

install(TARGETS fileserver)
