# PROCESS_DEPENDENCY_ENTRIES() Process all entries of the dependency list from a tart.yaml file
# 
FUNCTION(PROCESS_DEPENDENCY_ENTRIES PARENT_BUILD_TARGET ENTRIES ENTRY_INDENTS)
  GET_ENTRY_VALUES("dependency" "${ENTRIES}" "${ENTRY_INDENTS}" DEPENDENCIES HAS_SUBTREE)
  TART_DEBUG("Processing dependencies: ${DEPENDENCIES}")
  
  FOREACH(DEPENDENCY ${DEPENDENCIES})
    TART_DEBUG("Processing dependency: ${DEPENDENCY}")
    

    GET_ENTRY_VALUES("${DEPENDENCY}" "${ENTRIES}" "${ENTRY_INDENTS}" DEPENDENCY_DETAIL_KEYS HAS_SUBTREE)
    GET_ENTRY_SUBTREE("${DEPENDENCY}" "${ENTRIES}" "${ENTRY_INDENTS}" DEPENDENCY_ENTRIES DEPENDENCY_INDENTS)

    FIND_EXTERNAL_DEPENDENCY(${PARENT_BUILD_TARGET} ${DEPENDENCY} FOUND_EXTERNAL_DEPENDENCY)
    IF(FOUND_EXTERNAL_DEPENDENCY)
      CONTINUE()
    ENDIF()
    
    TART_DEBUG("   with keys: ${DEPENDENCY_DETAIL_KEYS}")
    FOREACH(DEPENDENCY_DETAIL_KEY ${DEPENDENCY_DETAIL_KEYS})
      TART_DEBUG("Processing dependency key: ${DEPENDENCY_DETAIL_KEY}")
      # Configure repo URL from host key
      IF(${DEPENDENCY_DETAIL_KEY} STREQUAL "host")
        GET_ENTRY_VALUES("${DEPENDENCY_DETAIL_KEY}" "${DEPENDENCY_ENTRIES}" "${DEPENDENCY_INDENTS}" DEPENDENCY_DETAIL_VALUE HAS_SUBTREE)
        TART_DEBUG("${DEPENDENCY_DETAIL_VALUE}")
        IF(${DEPENDENCY_DETAIL_VALUE} STREQUAL "github")
          SET(GIT_HOST_URL "https://github.com/")
        ELSEIF(${DEPENDENCY_DETAIL_VALUE} STREQUAL "gitlab")
          SET(GIT_HOST_URL "https://gitlab.com/")
        ELSE()
          TART_ERROR("Unsupported repo host value ${DEPENDENCY_DETAIL_VALUE}")
        ENDIF()
      ELSEIF(${DEPENDENCY_DETAIL_KEY} STREQUAL "publisher")
        GET_ENTRY_VALUES("${DEPENDENCY_DETAIL_KEY}" "${DEPENDENCY_ENTRIES}" "${DEPENDENCY_INDENTS}" DEPENDENCY_DETAIL_VALUE HAS_SUBTREE)
        TART_DEBUG("${DEPENDENCY_DETAIL_VALUE}")
        SET(GIT_PUBLISHER "${DEPENDENCY_DETAIL_VALUE}")
      ELSEIF(${DEPENDENCY_DETAIL_KEY} STREQUAL "checkout")
        GET_ENTRY_VALUES("${DEPENDENCY_DETAIL_KEY}" "${DEPENDENCY_ENTRIES}" "${DEPENDENCY_INDENTS}" DEPENDENCY_DETAIL_VALUE HAS_SUBTREE)
        TART_DEBUG("${DEPENDENCY_DETAIL_VALUE}")
        SET(GIT_CHECKOUT "${DEPENDENCY_DETAIL_VALUE}")
      ELSE()
        TART_WARNING("Ignoring unsupported dependency detail: ${DEPENDENCY_DETAIL_KEY}")
      ENDIF()
    ENDFOREACH()
    IF(GIT_HOST_URL)
      IF(GIT_PUBLISHER)
        SET(GIT_URL "${GIT_HOST_URL}/${GIT_PUBLISHER}/${DEPENDENCY}")
      ELSE()
        TART_ERROR("Missing publisher detail for ${DEPENDENCY} when repo host is ${GIT_HOST_URL}")
      ENDIF()
    ENDIF()
    
    LIST(FIND TART_USED_PACKAGES "${DEPENDENCY}" INDEX)
    IF(INDEX EQUAL -1)
      LIST(APPEND TART_USED_PACKAGES ${DEPENDENCY})
    ELSE()
      TART_WARNING("Package ${DEPENDENCY} was already added to current Project. \n Ignoring version info given in Package ${PARENT_BUILD_TARGET}")
      ADD_TART_DEPENDENCY(${PARENT_BUILD_TARGET} ${DEPENDENCY})
      RETURN()
    ENDIF()
  
    IF(GIT_CHECKOUT AND GIT_URL)
      ASSERT_PACKAGE_LOCALLY_EXISTING("${GIT_URL}" "${GIT_CHECKOUT}" "${TART_CURRENT_WORKSPACE_DIR}")
      ADD_TART_DEPENDENCY(${PARENT_BUILD_TARGET} ${DEPENDENCY})
    ELSEIF(GIT_CHECKOUT AND NOT GIT_URL)
      # TODO ASSERT_PACKAGE_LOCALLY_EXISTING without url
    ELSE()
      # TODO check if package available, even without git repo
      TART_DEBUG("No repo details for package ${DEPENDENCY}, adding local version..")
      ADD_TART_DEPENDENCY(${PARENT_BUILD_TARGET} ${DEPENDENCY})
      CONTINUE()
    ENDIF()
  ENDFOREACH()
ENDFUNCTION(PROCESS_DEPENDENCY_ENTRIES)
