# Installation configuration

if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
    set(CMAKE_INSTALL_PREFIX "/opt/bloom" CACHE PATH "..." FORCE)
endif()

set(BLOOM_INSTALLATION_PREFIX ${CMAKE_INSTALL_PREFIX})
set(BLOOM_SHARED_LIBRARY_PATHS "")

# If any path prefixes passed to CMAKE contain a lib directory, include it in our invocation script (LD_LIBRARY_PATH)
foreach(LIB_PATH ${CMAKE_PREFIX_PATH})
    if(EXISTS "${LIB_PATH}/lib")
        list(APPEND BLOOM_SHARED_LIBRARY_PATHS "${LIB_PATH}/lib")
    endif()
endforeach()

list(
    JOIN
    BLOOM_SHARED_LIBRARY_PATHS
    ":"
    BLOOM_SHARED_LIBRARY_PATHS
)

# Generate Bloom's invocation script
configure_file(
    "${CMAKE_CURRENT_SOURCE_DIR}/build/distributed/bloom.sh.in"
    "${CMAKE_BINARY_DIR}/bloom.sh"
    @ONLY
)

install(
    TARGETS Bloom
    DESTINATION "./bin"
    PERMISSIONS
        OWNER_EXECUTE OWNER_READ OWNER_WRITE
        GROUP_EXECUTE GROUP_READ
        WORLD_EXECUTE WORLD_READ
)

install(
    FILES ${CMAKE_BINARY_DIR}/bloom.sh
    DESTINATION "./bin"
    PERMISSIONS
        OWNER_EXECUTE OWNER_READ OWNER_WRITE
        GROUP_EXECUTE GROUP_READ
        WORLD_EXECUTE WORLD_READ
)

install(
    DIRECTORY ${CMAKE_BINARY_DIR}/resources
    DESTINATION "./"
    DIRECTORY_PERMISSIONS
        OWNER_EXECUTE OWNER_READ OWNER_WRITE
        GROUP_EXECUTE GROUP_READ
        WORLD_EXECUTE WORLD_READ
    FILE_PERMISSIONS
        OWNER_EXECUTE OWNER_READ OWNER_WRITE
        GROUP_EXECUTE GROUP_READ
        WORLD_EXECUTE WORLD_READ
)

install(
    DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build/distributed/fonts
    DESTINATION "./resources"
    DIRECTORY_PERMISSIONS
        OWNER_EXECUTE OWNER_READ OWNER_WRITE
        GROUP_EXECUTE GROUP_READ
        WORLD_EXECUTE WORLD_READ
    FILE_PERMISSIONS
        OWNER_READ OWNER_WRITE
        GROUP_EXECUTE GROUP_READ
        WORLD_EXECUTE WORLD_READ
)

install(
    FILES ${CMAKE_CURRENT_SOURCE_DIR}/build/distributed/udevrules/99-bloom.rules
    DESTINATION "/usr/lib/udev/rules.d/"
    PERMISSIONS
        OWNER_READ OWNER_WRITE
        GROUP_READ
        WORLD_READ
)
