set(GIT_REPO "https://github.com/vadz/libtiff.git")

function(vad_system)
  vad_system_default(${ARGN})
  if(TIFF_FOUND AND NOT TARGET TIFF::TIFF)
    # Some earlier versions of cmake do not provide the TIFF::TIFF target.
    message(STATUS "Creating the TIFF::TIFF imported target.")
    add_library(TIFF::TIFF UNKNOWN IMPORTED)
    set_target_properties(TIFF::TIFF PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${TIFF_INCLUDE_DIR}")
    set_property(TARGET TIFF::TIFF APPEND PROPERTY IMPORTED_LOCATION "${TIFF_LIBRARIES}")
    make_imported_targets_global()
  endif()
endfunction()

function(vad_live)
  git_clone(TIFF)
  if(VAD_PREFER_STATIC OR VAD_TIFF_PREFER_STATIC)
    message(STATUS "Forcing libtiff static build.")
    set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
  endif()
  add_subdirectory("${VAD_EXTERNAL_ROOT}/TIFF" "${VAD_EXTERNAL_ROOT}/TIFF/build_external_dep")
  add_library(_VAD_TIFF_STUB INTERFACE)
  set_target_properties(_VAD_TIFF_STUB PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${VAD_EXTERNAL_ROOT}/TIFF/libtiff;${VAD_EXTERNAL_ROOT}/TIFF/build_external_dep/libtiff")
  target_link_libraries(_VAD_TIFF_STUB INTERFACE tiff)
  # The TIFF live CMake auto-detects libraries such as ZLIB, LibLZMA and JPEG. If they are found, we will
  # add them to the TIFF interface.
  get_property(_OPT_DEP GLOBAL PROPERTY VAD_DEP_ZLIB_SATISFIED)
  if(_OPT_DEP)
      message(STATUS "The optional dependency ZLIB for TIFF was satisfied, linking ZLIB to the TIFF interface.")
      target_link_libraries(_VAD_TIFF_STUB INTERFACE ZLIB::ZLIB)
  endif()
  get_property(_OPT_DEP GLOBAL PROPERTY VAD_DEP_JPEG_SATISFIED)
  if(_OPT_DEP)
      message(STATUS "The optional dependency JPEG for TIFF was satisfied, linking JPEG to the TIFF interface.")
      target_link_libraries(_VAD_TIFF_STUB INTERFACE JPEG::JPEG)
  endif()
  get_property(_OPT_DEP GLOBAL PROPERTY VAD_DEP_LibLZMA_SATISFIED)
  if(_OPT_DEP)
      message(STATUS "The optional dependency LibLZMA for TIFF was satisfied, linking LibLZMA to the TIFF interface.")
      target_link_libraries(_VAD_TIFF_STUB INTERFACE LibLZMA::LibLZMA)
  endif()
  # Add the final alias.
  add_library(TIFF::TIFF ALIAS _VAD_TIFF_STUB)
endfunction()
