lineage: Rework bootanimation generation

Change-Id: Ib0b363c3e0545cb0c86dc69dd1a75f82dd777e59
diff --git a/bootanimation/Android.mk b/bootanimation/Android.mk
index 7eaf96c..b32e4b9 100644
--- a/bootanimation/Android.mk
+++ b/bootanimation/Android.mk
@@ -1,6 +1,6 @@
 #
 # Copyright (C) 2016 The CyanogenMod Project
-#               2017 The LineageOS Project
+#               2017-2019 The LineageOS Project
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -24,18 +24,32 @@
     TARGET_SCREEN_HEIGHT := 1920
 endif
 
-define build-bootanimation
-    $(shell) vendor/lineage/bootanimation/generate-bootanimation.sh \
-    $(PRODUCT_OUT) \
-    $(TARGET_SCREEN_WIDTH) \
-    $(TARGET_SCREEN_HEIGHT) \
-    $(TARGET_BOOTANIMATION_HALF_RES)
-endef
-
 TARGET_GENERATED_BOOTANIMATION := $(TARGET_OUT_INTERMEDIATES)/BOOTANIMATION/bootanimation.zip
-$(TARGET_GENERATED_BOOTANIMATION):
-	@echo "Building bootanimation"
-	$(build-bootanimation)
+$(TARGET_GENERATED_BOOTANIMATION): INTERMEDIATES := $(TARGET_OUT_INTERMEDIATES)/BOOTANIMATION
+$(TARGET_GENERATED_BOOTANIMATION): $(SOONG_ZIP)
+	@echo "Building bootanimation.zip"
+	@rm -rf $(dir $@)
+	@mkdir -p $(dir $@)
+	$(hide) tar xfp vendor/lineage/bootanimation/bootanimation.tar -C $(INTERMEDIATES)
+	$(hide) if [ $(TARGET_SCREEN_HEIGHT) -lt $(TARGET_SCREEN_WIDTH) ]; then \
+	    IMAGEWIDTH=$(TARGET_SCREEN_HEIGHT); \
+	else \
+	    IMAGEWIDTH=$(TARGET_SCREEN_WIDTH); \
+	fi; \
+	IMAGESCALEWIDTH=$$IMAGEWIDTH; \
+	IMAGESCALEHEIGHT=$$(expr $$IMAGESCALEWIDTH / 3); \
+	if [ "$(TARGET_BOOTANIMATION_HALF_RES)" = "true" ]; then \
+	    IMAGEWIDTH="$$(expr "$$IMAGEWIDTH" / 2)"; \
+	fi; \
+	IMAGEHEIGHT=$$(expr $$IMAGEWIDTH / 3); \
+	RESOLUTION="$$IMAGEWIDTH"x"$$IMAGEHEIGHT"; \
+	for part_cnt in 0 1 2 3 4; do \
+	    mkdir -p $(INTERMEDIATES)/part$$part_cnt; \
+	done; \
+	mogrify -resize $$RESOLUTION -colors 250 $(INTERMEDIATES)/*/*.png; \
+	echo "$$IMAGESCALEWIDTH $$IMAGESCALEHEIGHT 60" > $(INTERMEDIATES)/desc.txt; \
+	cat vendor/lineage/bootanimation/desc.txt >> $(INTERMEDIATES)/desc.txt
+	$(hide) $(SOONG_ZIP) -L 0 -o $(TARGET_GENERATED_BOOTANIMATION) -C $(INTERMEDIATES) -D $(INTERMEDIATES)
 
 ifeq ($(TARGET_BOOTANIMATION),)
     TARGET_BOOTANIMATION := $(TARGET_GENERATED_BOOTANIMATION)
@@ -59,5 +73,4 @@
 include $(BUILD_SYSTEM)/base_rules.mk
 
 $(LOCAL_BUILT_MODULE): $(TARGET_BOOTANIMATION)
-	@mkdir -p $(dir $@)
 	@cp $(TARGET_BOOTANIMATION) $@
diff --git a/bootanimation/generate-bootanimation.sh b/bootanimation/generate-bootanimation.sh
deleted file mode 100755
index 41d8c02..0000000
--- a/bootanimation/generate-bootanimation.sh
+++ /dev/null
@@ -1,41 +0,0 @@
-#!/bin/bash
-
-PRODUCT_OUT="$1"
-WIDTH="$2"
-HEIGHT="$3"
-HALF_RES="$4"
-
-OUT="$PRODUCT_OUT/obj/BOOTANIMATION"
-
-if [ "$HEIGHT" -lt "$WIDTH" ]; then
-    IMAGEWIDTH="$HEIGHT"
-else
-    IMAGEWIDTH="$WIDTH"
-fi
-
-IMAGESCALEWIDTH="$IMAGEWIDTH"
-IMAGESCALEHEIGHT=$(expr $IMAGESCALEWIDTH / 3)
-
-if [ "$HALF_RES" = "true" ]; then
-    IMAGEWIDTH=$(expr $IMAGEWIDTH / 2)
-fi
-
-IMAGEHEIGHT=$(expr $IMAGEWIDTH / 3)
-
-RESOLUTION=""$IMAGEWIDTH"x"$IMAGEHEIGHT""
-
-for part_cnt in 0 1 2 3 4
-do
-    mkdir -p "$OUT/bootanimation/part$part_cnt"
-done
-tar xfp "vendor/lineage/bootanimation/bootanimation.tar" -C "$OUT/bootanimation/"
-mogrify -resize $RESOLUTION -colors 250 "$OUT/bootanimation/"*"/"*".png"
-
-# Create desc.txt
-echo "$IMAGESCALEWIDTH $IMAGESCALEHEIGHT" 60 > "$OUT/bootanimation/desc.txt"
-cat "vendor/lineage/bootanimation/desc.txt" >> "$OUT/bootanimation/desc.txt"
-
-# Create bootanimation.zip
-cd "$OUT/bootanimation"
-
-zip -qr0 "$OUT/bootanimation.zip" .