Automatically run get_blobs if blobs are missing

Let the build system automatically check for the presence of blobs in
the tree and run the get_blobs.py script if they are missing.

Issue: FP2N-362
Change-Id: I025481d8a166778fc3a95ef5e54720adcf01b1d1
Depends-On: I6300fa038e0ca546ca6504f28214cf3faa7adf05
diff --git a/device.mk b/device.mk
index 32517db..42d2eb4 100644
--- a/device.mk
+++ b/device.mk
@@ -15,23 +15,37 @@
 
 # For PRODUCT_COPY_FILES, the first instance takes precedence.
 
-BLOBS_PATH = device/fairphone/fp2-proprietary
+# Stop here if we already tried to download the blobs and notified how to
+# download them.
+ifeq ("$(BLOBS_MISSING_NOTIFIED)","TRUE")
+$(error)
+endif
 
-# Check the presence of proprietary blobs
-ifeq ("$(wildcard $(BLOBS_PATH)/device-vendor.mk)","")
+BLOBS_PATH = $(abspath device/fairphone/fp2-proprietary)
+GET_BLOBS_CMD = vendor/fairphone/tools/bin/get_blobs.py --device FP2 --build-id $(BUILD_ID) --blobs-dir $(BLOBS_PATH)
+CHECK_BLOBS_EXIST = $(wildcard $(BLOBS_PATH)/device-vendor.mk)
+
+# Download the blobs if they don't exist.
+ifeq ("$(CHECK_BLOBS_EXIST)","")
+$(shell python3 $(GET_BLOBS_CMD) >&2)
+endif
+
+# Prompt the user if something went wrong.
+ifeq ("$(CHECK_BLOBS_EXIST)","")
 define BLOBS_INSTRUCTION
 
 
 Cannot find FP2 binary blobs.
 
-Please visit the download page
+Please run
 
-  https://code.fairphone.com/projects/fp-osos/dev/fp2-blobs-download-page.html
+  python3 $(GET_BLOBS_CMD)
 
-accept the terms of agreement and follow the instructions.
+and accept the terms of agreement.
 
 
 endef
+BLOBS_MISSING_NOTIFIED = TRUE
 $(error $(BLOBS_INSTRUCTION))
 endif