pkg_check_modules(fuse REQUIRED IMPORTED_TARGET fuse3)

set(SRCS
    buildbox.cc
    client.cc
)

add_executable(buildbox-fuse ${SRCS})
target_precompile_headers(buildbox-fuse REUSE_FROM common)
target_include_directories(buildbox-fuse PRIVATE ".")
install(TARGETS buildbox-fuse RUNTIME DESTINATION bin)
target_link_libraries(buildbox-fuse
    PkgConfig::fuse
    common)

option(FUSE_TESTS_SKIP_REMOTE "Skip remote fuse tests")
option(FUSE_TESTS_SKIP_XATTR "Skip xattr fuse tests")
option(SKIP_BUILD_FUSE_CHMOD_PERMS "Skip building the chmod_perms test")

if(BUILD_TESTING)
    find_program(FUSE_LOCAL "test/fuse-local.sh" PATHS ${CMAKE_CURRENT_SOURCE_DIR} REQUIRED NO_DEFAULT_PATH)
    find_program(FUSE_REMOTE "test/fuse-remote.sh" PATHS ${CMAKE_CURRENT_SOURCE_DIR} REQUIRED NO_DEFAULT_PATH)
    find_program(EXTENDED_ATTRIBUTES "test/fuse-xattr.sh" PATHS ${CMAKE_CURRENT_SOURCE_DIR} REQUIRED NO_DEFAULT_PATH)
    find_program(OUTPUT_TIMES "test/output-times.sh" PATHS ${CMAKE_CURRENT_SOURCE_DIR} REQUIRED NO_DEFAULT_PATH)

    foreach(t
            unmodified
            create_empty_file
            create_file
            create_executable_file
            append_file
            truncate_file
            fallocate_file
            rename_file
            rename_file_crossdir
            rename_file_noreplace
            rename_file_noreplace_crossdir
            replace_file
            replace_file_crossdir
            remove_file
            create_empty_directory
            create_directory_with_file
            remove_directory
            create_symlink
            create_hardlink
            chown_ctime
            atime
            fifo
            chmod_perms)
        if(SKIP_BUILD_FUSE_CHMOD_PERMS AND t STREQUAL "chmod_perms")
            continue()
        endif()
        add_test(NAME fuse_local_${t} COMMAND ${FUSE_LOCAL} ${t})
        set_property(TEST fuse_local_${t} PROPERTY ENVIRONMENT BUILDBOX=$<TARGET_FILE:buildbox-fuse>)

        if(NOT FUSE_TESTS_SKIP_REMOTE)
            add_test(NAME fuse_remote_${t} COMMAND ${FUSE_REMOTE} ${t})
            set_property(TEST fuse_remote_${t} PROPERTY ENVIRONMENT BUILDBOX=$<TARGET_FILE:buildbox-fuse> CAS_SERVER=$<TARGET_FILE:buildbox-casd>)

            add_test(NAME fuse_prefetch_${t} COMMAND ${FUSE_REMOTE} ${t})
            set_property(TEST fuse_prefetch_${t} PROPERTY ENVIRONMENT BUILDBOX=$<TARGET_FILE:buildbox-fuse> CAS_SERVER=$<TARGET_FILE:buildbox-casd> PREFETCH=1)
        endif()
    endforeach()

    if(NOT FUSE_TESTS_SKIP_XATTR)
        add_test(NAME fuse_extended_attributes COMMAND ${EXTENDED_ATTRIBUTES})
        set_property(TEST fuse_extended_attributes PROPERTY ENVIRONMENT BUILDBOX=$<TARGET_FILE:buildbox-fuse> PROTO_PATH=${CMAKE_SOURCE_DIR}/protos)
    endif()

    add_test(NAME fuse_output_times COMMAND ${OUTPUT_TIMES})
    set_property(TEST fuse_output_times PROPERTY ENVIRONMENT BUILDBOX=$<TARGET_FILE:buildbox-fuse> PROTO_PATH=${CMAKE_SOURCE_DIR}/protos)

    if(NOT FUSE_TESTS_SKIP_REMOTE)
        add_test(NAME fuse_remote_time_output COMMAND ${OUTPUT_TIMES})
        set_property(TEST fuse_remote_time_output PROPERTY ENVIRONMENT BUILDBOX=$<TARGET_FILE:buildbox-fuse> CAS_SERVER=$<TARGET_FILE:buildbox-casd> PREFETCH=1 PROTO_PATH=${CMAKE_SOURCE_DIR}/protos)
    endif()
endif()
