# Try pkg-config first, fall back to find_library
# Use FUSE_PKG prefix to avoid collision with FUSE_PREFIX
if(PkgConfig_FOUND)
  pkg_check_modules(FUSE_PKG QUIET IMPORTED_TARGET fuse>=2.6)
  if(FUSE_PKG_FOUND AND NOT TARGET Fuse::Fuse)
    add_library(Fuse::Fuse ALIAS PkgConfig::FUSE_PKG)
  endif()
endif()

if(NOT FUSE_PKG_FOUND)
  find_library(FUSE_PKG_LIBRARIES fuse QUIET)
  if(FUSE_PKG_LIBRARIES)
    set(FUSE_PKG_FOUND TRUE)
    if(NOT TARGET Fuse::Fuse)
      add_library(Fuse::Fuse UNKNOWN IMPORTED)
      set_target_properties(Fuse::Fuse PROPERTIES
        IMPORTED_LOCATION "${FUSE_PKG_LIBRARIES}")
    endif()
  endif()
endif()

if(FUSE_PKG_FOUND)
  set(DISABLE_FUSE FALSE CACHE BOOL "Disable FUSE support")
  if(NOT DISABLE_FUSE)
    if(FUSE_PKG_VERSION)
      message(STATUS "FUSE support enabled (version ${FUSE_PKG_VERSION})")
    else()
      message(STATUS "FUSE support enabled")
    endif()
    set(HAVE_FUSE TRUE CACHE INTERNAL "FUSE filesystem support available")
    set(FUSE_PREFIX "/tmp/ouroboros" CACHE STRING
      "Mountpoint for RIB filesystem")
  else()
    message(STATUS "FUSE support disabled by user")
    unset(HAVE_FUSE CACHE)
  endif()
else()
  message(STATUS "Install FUSE version >= 2.6 to enable RIB access")
  unset(HAVE_FUSE CACHE)
endif()

if(NOT HAVE_FUSE)
  set(FUSE_PKG_LIBRARIES "")
endif()

mark_as_advanced(FUSE_PKG_LIBRARIES)
