#!/usr/bin/make -f

#
# \brief  Tool for determining the dependencies of depot content
# \author Norman Feske
# \date   2017-03-17
#

define HELP_MESSAGE

  Show the dependencies of depot content

  usage:

    $(firstword $(MAKEFILE_LIST)) <archive-path>...

  This tool operates solely on the depot content. It prints the
  result only if all dependencies are present within the depot.
  Otherwise, the missing archives are given as an error message.

endef

export GENODE_DIR := $(realpath $(dir $(MAKEFILE_LIST))/../..)

DEPOT_TOOL_DIR ?= $(GENODE_DIR)/tool/depot

include $(DEPOT_TOOL_DIR)/mk/front_end.inc
include $(DEPOT_TOOL_DIR)/mk/categorize_args.inc


#
# Collect dependencies for all specified arguments
#

api_file = $(addsuffix /api,$(addprefix $(DEPOT_DIR)/,$1))

used_apis_file = $(addsuffix /used_apis,$(addprefix $(DEPOT_DIR)/,$1))

_pkg_archives_of_type = $(call grep_archive_type,$1,\
                           $(call file_content,\
                              $(addsuffix /archives,$(addprefix $(DEPOT_DIR)/,$2))))

pkg_src_archives  = $(call _pkg_archives_of_type,src,$1)
pkg_raw_archives  = $(call _pkg_archives_of_type,raw,$1)
pkg_pkg_archives  = $(call _pkg_archives_of_type,pkg,$1)

include $(DEPOT_TOOL_DIR)/mk/dependencies.inc


#
# Validate and print gathered information
#

# select function for determining the archive version depending on archive type
_archive_version(bin) = bin_archive_version
_archive_version(src) = archive_version
_archive_version(api) = archive_version
_archive_version(raw) = archive_version
_archive_version(pkg) = archive_version

_version_defined = ${call ${_archive_version(${call archive_type,$1})},$1}

NEEDED_ARCHIVES := $(foreach TYPE,pkg src raw api bin,${ARCHIVES(${TYPE})})

# check for completeness of archive-path arguments
NO_VERSION_ARCHIVES := $(sort $(foreach A,$(NEEDED_ARCHIVES),\
                                 $(if $(call _version_defined,$A),,$A)))

# check for existence of archive paths in depot
MISSING_ARCHIVES := $(sort $(foreach A,$(NEEDED_ARCHIVES),\
                              $(if $(wildcard $(addprefix $(DEPOT_DIR)/,$A)),,$A)))
checked_completeness:
ifneq ($(NO_VERSION_ARCHIVES),)
	@echo "Error: version not specified:"; \
	 for i in $(NO_VERSION_ARCHIVES); do echo "       $$i"; done; false
endif
ifneq ($(MISSING_ARCHIVES),)
	@echo "Error: incomplete or missing archives:"; \
	 for i in $(MISSING_ARCHIVES); do echo "       $$i"; done; false
endif

$(MAKECMDGOALS): checked_completeness
	@for i in $(NEEDED_ARCHIVES); do echo $$i; done

