#
# Download rules for the original/unmodified image file(s)
#


# Define DL_SOURCE/DL_SITE related variables
#  $(1) Suffix
define DEFINE_DL_SOURCE_VARS
ifneq ($(strip $(FREETZ_DL_SOURCE$(1))),)
DL_SITE$(1):=$(call qstrip,$(FREETZ_DL_SITE$(1)))
DL_SOURCE$(1):=$(call qstrip,$(FREETZ_DL_SOURCE$(1)))
DL_SOURCE$(1)_LOCAL:=$(call GET_STRING_COMPONENT,$(call qstrip,$(FREETZ_DL_SOURCE$(1))),/,1)
DL_SOURCE$(1)_REMOTE:=$(call GET_STRING_COMPONENT,$(call qstrip,$(FREETZ_DL_SOURCE$(1))),/,2)
DL_SOURCE$(1)_MD5:=$(call qstrip,$(FREETZ_DL_SOURCE$(1)_MD5))
endif

ifneq ($(strip $(FREETZ_DL_SOURCE$(1)_CONTAINER)),)
DL_SOURCE$(1)_CONTAINER:=$(call qstrip,$(FREETZ_DL_SOURCE$(1)_CONTAINER))
DL_SOURCE$(1)_CONTAINER_MD5:=$(call qstrip,$(FREETZ_DL_SOURCE$(1)_CONTAINER_MD5))
endif
endef

$(eval $(call DEFINE_DL_SOURCE_VARS))
$(eval $(call DEFINE_DL_SOURCE_VARS,2))
$(eval $(call DEFINE_DL_SOURCE_VARS,3))

export DL_SOURCE_ID=$(shell echo $(DL_SOURCE_LOCAL) | tools/imagename2id)



DL_IMAGE:=
image:

# Download Firmware Image
#  $(1) Suffix
define DOWNLOAD_FIRMWARE
ifneq ($(strip $(DL_SOURCE$(1))),)
IMAGE$(1):=$(DL_FW_DIR)/$(DL_SOURCE$(1)_LOCAL)
DL_IMAGE+=$$(IMAGE$(1))
image: $$(IMAGE$(1))
$$(IMAGE$(1)): | $(DL_FW_DIR)
ifeq ($$(strip $$(DL_SITE$(1))),)
	@echo
	@echo "Please copy the following file into the '$$(DL_FW_DIR)' sub-directory manually:"
	@echo "$$(DL_SOURCE$(1)_LOCAL)" $$(if $$(DL_SOURCE$(1)_REMOTE),"(rename original AVM '$$(DL_SOURCE$(1)_REMOTE)' for that)")
	@echo
	@exit 3
else
	@if [ -n "$$(DL_SOURCE$(1)_CONTAINER)" ]; then \
		if [ ! -r $$(DL_FW_DIR)/$$(DL_SOURCE$(1)_CONTAINER) ]; then \
			if ! $$(DL_TOOL) --no-append-servers $$(DL_FW_DIR) "$$(DL_SOURCE$(1)_CONTAINER)" "$$(DL_SITE$(1))" $$(DL_SOURCE$(1)_CONTAINER_MD5) $$(SILENT); then \
				$$(call ERROR,3,Could not download firmware image. See http://trac.freetz.org/wiki/FAQ#Couldnotdownloadfirmwareimage for details.) \
			fi; \
		fi; \
		case "$$(suffix $$(DL_SOURCE$(1)_CONTAINER))" in \
			.zip|.ZIP) \
				if ! unzip -j $$(QUIETSHORT) $$(DL_FW_DIR)/$$(DL_SOURCE$(1)_CONTAINER) *$$(if $$(DL_SOURCE$(1)_REMOTE),$$(DL_SOURCE$(1)_REMOTE),$$(DL_SOURCE$(1)_LOCAL)) -d $$(DL_FW_DIR); then \
					$$(call ERROR,3,Could not unzip firmware image.) \
				fi \
				;; \
			*) \
				$$(call ERROR,3,Could not extract firmware image.) \
				;; \
		esac \
	elif ! $$(DL_TOOL) --no-append-servers $$(DL_FW_DIR) "$$(if $$(DL_SOURCE$(1)_REMOTE),$$(DL_SOURCE$(1)_REMOTE),$$(DL_SOURCE$(1)_LOCAL))" "$$(DL_SITE$(1))" $$(DL_SOURCE$(1)_MD5) $$(SILENT); then \
		$$(call ERROR,3,Could not download firmware image. See http://trac.freetz.org/wiki/FAQ#Couldnotdownloadfirmwareimage for details.) \
	fi; \
	$$(if $$(DL_SOURCE$(1)_REMOTE),echo "Renaming '$$(DL_SOURCE$(1)_REMOTE)' to '$$(DL_SOURCE$(1)_LOCAL)' to make the filename unique." $$(SILENT);) \
	$$(if $$(DL_SOURCE$(1)_REMOTE),mv -f "$$(DL_FW_DIR)/$$(DL_SOURCE$(1)_REMOTE)" "$$(DL_FW_DIR)/$$(DL_SOURCE$(1)_LOCAL)";)
endif
endif
endef

$(eval $(call DOWNLOAD_FIRMWARE))
$(eval $(call DOWNLOAD_FIRMWARE,2))
$(eval $(call DOWNLOAD_FIRMWARE,3))
