# teach libraries how to find plugins and libraries, even if there is another
# copy of Graphviz installed in the system default paths
if(APPLE)
  list(APPEND CMAKE_INSTALL_RPATH
    "@executable_path/../../../${PLUGIN_INSTALL_DIR}")
  list(APPEND CMAKE_INSTALL_RPATH "@executable_path/../..")
else()
  list(APPEND CMAKE_INSTALL_RPATH "\$ORIGIN/../../../${PLUGIN_INSTALL_DIR}")
  list(APPEND CMAKE_INSTALL_RPATH "\$ORIGIN/../..")
endif()

if(enable_tcl)
  add_subdirectory(gdtclft)
  add_subdirectory(tcldot)
  add_subdirectory(tclhandle)
  add_subdirectory(tclpathplan)
  add_subdirectory(tclstubs)
endif()

add_subdirectory(gv)

if(enable_tcl)
  if(GD_FOUND)
    set(GD_INDEX "gdtclft/pkgIndex.tcl")
  else()
    set(GD_INDEX "")
  endif()
  if(enable_swig)
    set(SWIG_INDEX "gv/pkgIndex.tcl")
  else()
    set(SWIG_INDEX "")
  endif()
  if(CMAKE_VERSION VERSION_LESS "3.18.0")
    message(WARNING "CMake < 3.18, so TCL pkgIndex.tcl will not be built")
  else()
    add_custom_target(tcl_pkgindex
      ALL
      COMMAND ${CMAKE_COMMAND} -E cat tcldot/pkgIndex.tcl
        tclpathplan/pkgIndex.tcl ${GD_INDEX} ${SWIG_INDEX} >pkgIndex.tcl
      COMMAND ${CMAKE_COMMAND} -E echo "\"#" "end\"" >>pkgIndex.tcl
      BYPRODUCTS pkgIndex.tcl
      COMMENT "create TCL package index"
    )
    add_dependencies(tcl_pkgindex tcldot_pkgindex tclplan_pkgindex)
    if(GD_FOUND)
      add_dependencies(tcl_pkgindex gdtclft_pkgindex)
    endif()
    if(enable_swig)
      add_dependencies(tcl_pkgindex gv_tcl_pkgindex)
    endif()

    install(
      FILES ${CMAKE_CURRENT_BINARY_DIR}/pkgIndex.tcl
      DESTINATION ${LIBRARY_INSTALL_DIR}/graphviz/tcl
    )
  endif()
endif()
