http_prebuilt: Implement archive of artifacts

* checksum will be downloaded and asserted to prevent a
    redownload every clean
* Archives will exist (invisibly) at vendor/cm/prebuilt/archive

Change-Id: I637e86cacd146d665044a119885900dc4660e22b
Signed-off-by: Brandon McAnsh <brandon.mcansh@gmail.com>
diff --git a/.gitignore b/.gitignore
index f5b82f3..931ce07 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
 proprietary/*
+prebuilt/archive
diff --git a/build/core/http_prebuilt.mk b/build/core/http_prebuilt.mk
index 805b9f0..9dc3975 100644
--- a/build/core/http_prebuilt.mk
+++ b/build/core/http_prebuilt.mk
@@ -21,12 +21,67 @@
   $(error LOCAL_HTTP_FILENAME not defined.)
 endif
 
+ifeq ($(strip $(LOCAL_HTTP_MD5SUM)),)
+  $(error LOCAL_HTTP_MD5SUM not defined.)
+endif
+
+PREBUILT_MODULE_ARCHIVE := vendor/cm/prebuilt/archive/$(LOCAL_MODULE)
+
+PREBUILT_MODULE_FILE := $(PREBUILT_MODULE_ARCHIVE)/$(LOCAL_HTTP_FILENAME)
+
+PREBUILT_MODULE_MD5SUM := $(PREBUILT_MODULE_ARCHIVE)/md5sum
+
+HTTP_FILE_URL := $(LOCAL_HTTP_PATH)/$(LOCAL_HTTP_FILENAME)
+HTTP_FILE_MD5_URL := $(LOCAL_HTTP_PATH)/$(LOCAL_HTTP_MD5SUM)
+
 LOCAL_PREBUILT_MODULE_FILE := $(call intermediates-dir-for,$(LOCAL_MODULE_CLASS),$(LOCAL_MODULE),,COMMON)/$(LOCAL_HTTP_FILENAME)
 
 $(LOCAL_PREBUILT_MODULE_FILE): filename := $(LOCAL_HTTP_FILENAME)
+$(LOCAL_PREBUILT_MODULE_FILE): checksum := $(PREBUILT_MODULE_MD5SUM)
+$(LOCAL_PREBUILT_MODULE_FILE): filepath := $(PREBUILT_MODULE_FILE)
+$(LOCAL_PREBUILT_MODULE_FILE): version  := $(LOCAL_HTTP_FILE_VERSION)
+
+define curl-checksum
+  @echo "Pulling comparison md5sum for $(filename)"
+  $(call download-prebuilt-module, $(HTTP_FILE_MD5_URL),$(checksum))
+endef
+
+define audit-checksum
+  @echo "Downloading: $(filename) (version $(version))" -> $(filepath);
+  $(hide) if [ ! -f $(filepath) ]; then \
+            $(call download-prebuilt-module, $(HTTP_FILE_URL),$(filepath)) \
+          else \
+            if [ "$(shell echo $(md5sum $(filepath)))" != "$(shell cat $(checksum) | cut -d ' ' -f1)" ]; then \
+              rm -rf $(filepath); \
+              $(call download-prebuilt-module, $(HTTP_FILE_URL),$(filepath)) \
+            fi; \
+          fi; \
+          rm -f $(checksum);
+endef
+
+# $(1) url
+# $(2) file output
+define download-prebuilt-module
+  ./vendor/cm/build/tasks/http_curl_prebuilt.sh $(1) $(2);
+endef
+
+define cleanup
+  @echo "Copying: $(filename) -> $(dir $@)"
+  $(hide) mkdir -p $(dir $@)
+  $(hide) cp $(filepath) $(dir $@)/$(filename)
+endef
 
 $(LOCAL_PREBUILT_MODULE_FILE):
-	$(hide) curl -L $(LOCAL_HTTP_PATH) --create-dirs -o $(dir $@)/$(filename) --compressed -H "Accept-Encoding: gzip,deflate,sdch"
-	@echo "Download: $@"
+	$(call curl-checksum)
+	$(call audit-checksum)
+	$(call cleanup)
 
 include $(BUILD_PREBUILT)
+
+# the "fetchprebuilts" target will go through and pre-download all of the maven dependencies in the tree
+fetchprebuilts: $(LOCAL_PREBUILT_MODULE_FILE)
+
+# the "nukeprebuilts" target will evict all archived artifacts
+nukeprebuilts:
+	  @echo "Removing artifact for $(LOCAL_HTTP_FILENAME)"
+	  $(hide) rm -rf $(PREBUILT_MODULE_ARCHIVE)
diff --git a/build/tasks/http_curl_prebuilt.sh b/build/tasks/http_curl_prebuilt.sh
new file mode 100755
index 0000000..c90c35f
--- /dev/null
+++ b/build/tasks/http_curl_prebuilt.sh
@@ -0,0 +1,6 @@
+#!/bin/bash
+
+url=$1
+output=$2
+
+curl -L "$url" --create-dirs -o $output --compressed -H "Accept-Encoding: gzip,deflate,sdch" && exit 0 || exit 1
diff --git a/gello/Android.mk b/gello/Android.mk
index 62e6aa6..a88c42a 100644
--- a/gello/Android.mk
+++ b/gello/Android.mk
@@ -42,9 +42,10 @@
 LOCAL_BUILT_MODULE_STEM := package.apk
 LOCAL_MODULE_SUFFIX := $(COMMON_ANDROID_PACKAGE_SUFFIX)
 
-LOCAL_GELLO_VERSION := 40
-LOCAL_HTTP_PATH := https://github.com/LineageOS/android_packages_apps_Gello/releases/download/$(LOCAL_GELLO_VERSION)/gello.apk
+LOCAL_HTTP_FILE_VERSION := 40
+LOCAL_HTTP_PATH := https://github.com/LineageOS/android_packages_apps_Gello/releases/download/$(LOCAL_HTTP_FILE_VERSION)
 LOCAL_HTTP_FILENAME := gello.apk
+LOCAL_HTTP_MD5SUM := $(LOCAL_HTTP_FILENAME).md5sum
 
 include $(BUILD_HTTP_PREBUILT)
 endif