include_guard(GLOBAL)

function(add_conventional_targets_under directory)
  if(EXISTS "${directory}/CMakeLists.txt")
    add_subdirectory("${directory}")
  else()
    file(GLOB files "${directory}/*")
    foreach(file ${files})
      if(IS_DIRECTORY "${file}")
        add_conventional_targets_under("${file}")
      endif()
    endforeach()
  endif()
endfunction()

function(add_conventional_targets_provided_under directory)
  if(EXISTS "${directory}/.cppsm")
    add_conventional_targets_under("${directory}/provides")
  elseif(EXISTS "${directory}/CMakeLists.txt")
    add_subdirectory("${directory}")
  else()
    file(GLOB files "${directory}/*")
    foreach(file ${files})
      if(IS_DIRECTORY "${file}")
        add_conventional_targets_provided_under("${file}")
      endif()
    endforeach()
  endif()
endfunction()

function(add_conventional_targets)
  add_conventional_targets_provided_under("${CMAKE_CURRENT_SOURCE_DIR}/requires")
  add_conventional_targets_under("${CMAKE_CURRENT_SOURCE_DIR}/provides")
  if("${CMAKE_SOURCE_DIR}" STREQUAL "${PROJECT_SOURCE_DIR}")
    add_conventional_targets_provided_under("${CMAKE_CURRENT_SOURCE_DIR}/equipment")
    add_conventional_targets_under("${CMAKE_CURRENT_SOURCE_DIR}/internals")
  endif()
endfunction()
