vendor/aosp: Add back backuptool scripts

Partially reverts commit 1042d267cc4ecece281c82ca3bce98692f2f9ebd.
diff --git a/common.mk b/common.mk
index 3305da9..3d47b3f 100644
--- a/common.mk
+++ b/common.mk
@@ -16,6 +16,13 @@
 # Thank you, please drive thru!
 PRODUCT_PROPERTY_OVERRIDES += persist.sys.dun.override=0
 
+# Backup Tool
+PRODUCT_COPY_FILES += \
+    vendor/aosp/prebuilt/common/bin/backuptool.sh:install/bin/backuptool.sh \
+    vendor/aosp/prebuilt/common/bin/backuptool.functions:install/bin/backuptool.functions \
+    vendor/aosp/prebuilt/common/bin/blacklist:system/addon.d/blacklist \
+    vendor/aosp/prebuilt/common/bin/whitelist:system/addon.d/whitelist \
+
 # Bootanimation
 PRODUCT_COPY_FILES += \
     vendor/aosp/prebuilt/common/media/bootanimation.zip:system/media/bootanimation.zip
diff --git a/prebuilt/common/bin/backuptool.functions b/prebuilt/common/bin/backuptool.functions
new file mode 100644
index 0000000..8c76562
--- /dev/null
+++ b/prebuilt/common/bin/backuptool.functions
@@ -0,0 +1,37 @@
+#!/sbin/sh
+#
+# Functions for backuptool.sh
+#
+
+export C=/tmp/backupdir
+export S=/system
+export V=6.0
+
+backup_file() {
+  if [ -e "$1" ]; then
+    local F=`basename "$1"`
+    local D=`dirname "$1"`
+    # dont backup any apps that have odex files, they are useless
+    if ( echo "$F" | grep -q "\.apk$" ) && [ -e `echo "$1" | sed -e 's/\.apk$/\.odex/'` ]; then
+      echo "Skipping odexed apk $1";
+    else
+      mkdir -p "$C/$D"
+      cp -p $1 "$C/$D/$F"
+    fi
+  fi
+}
+
+restore_file() {
+  local FILE=`basename "$1"`
+  local DIR=`dirname "$1"`
+  if [ -e "$C/$DIR/$FILE" ]; then
+    if [ ! -d "$DIR" ]; then
+      mkdir -p "$DIR";
+    fi
+    cp -p "$C/$DIR/$FILE" "$1";
+    if [ -n "$2" ]; then
+      echo "Deleting obsolete file $2"
+      rm "$2";
+    fi
+  fi
+}
diff --git a/prebuilt/common/bin/backuptool.sh b/prebuilt/common/bin/backuptool.sh
new file mode 100644
index 0000000..8a6e7a5
--- /dev/null
+++ b/prebuilt/common/bin/backuptool.sh
@@ -0,0 +1,114 @@
+#!/sbin/sh
+#
+# Backup and restore addon /system files
+#
+
+export C=/tmp/backupdir
+export S=/system
+export V=6.0
+
+# Scripts in /system/addon.d expect to find backuptool.functions in /tmp
+cp -f /tmp/install/bin/backuptool.functions /tmp
+
+# Preserve /system/addon.d in /tmp/addon.d
+preserve_addon_d() {
+  if [ -d /system/addon.d/ ]; then
+    mkdir -p /tmp/addon.d/
+    cp -a /system/addon.d/* /tmp/addon.d/
+    chmod 755 /tmp/addon.d/*.sh
+  fi
+}
+
+# Restore /system/addon.d from /tmp/addon.d
+restore_addon_d() {
+  if [ -d /tmp/addon.d/ ]; then
+    cp -a /tmp/addon.d/* /system/addon.d/
+    rm -rf /tmp/addon.d/
+  fi
+}
+
+# Proceed only if /system is the expected major and minor version
+check_prereq() {
+# If there is no build.prop file the partition is probably empty.
+if [ ! -r /system/build.prop ]; then
+    return 0
+fi
+if ( ! grep -q "^ro.build.version.release=$V.*" /system/build.prop ); then
+  echo "Not backing up files from incompatible version: $V"
+  return 0
+fi
+return 1
+}
+
+check_blacklist() {
+  if [ -f /system/addon.d/blacklist ];then
+      ## Discard any known bad backup scripts
+      cd /$1/addon.d/
+      for f in *sh; do
+          s=$(md5sum $f | cut -c-32)
+          grep -q $s /system/addon.d/blacklist && rm -f $f
+      done
+  fi
+}
+
+check_whitelist() {
+  found=0
+  if [ -f /system/addon.d/whitelist ];then
+      ## forcefully keep any version-independent stuff
+      cd /$1/addon.d/
+      for f in *sh; do
+          s=$(md5sum $f | cut -c-32)
+          grep -q $s /system/addon.d/whitelist
+          if [ $? -eq 0 ]; then
+              found=1
+          else
+              rm -f $f
+          fi
+      done
+  fi
+  return $found
+}
+
+# Execute /system/addon.d/*.sh scripts with $1 parameter
+run_stage() {
+if [ -d /tmp/addon.d/ ]; then
+  for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
+    $script $1
+  done
+fi
+}
+
+case "$1" in
+  backup)
+    mkdir -p $C
+    if check_prereq; then
+        if check_whitelist system; then
+            exit 127
+        fi
+    fi
+    check_blacklist system
+    preserve_addon_d
+    run_stage pre-backup
+    run_stage backup
+    run_stage post-backup
+  ;;
+  restore)
+    if check_prereq; then
+        if check_whitelist tmp; then
+            exit 127
+        fi
+    fi
+    check_blacklist tmp
+    run_stage pre-restore
+    run_stage restore
+    run_stage post-restore
+    restore_addon_d
+    rm -rf $C
+    sync
+  ;;
+  *)
+    echo "Usage: $0 {backup|restore}"
+    exit 1
+esac
+
+exit 0
diff --git a/prebuilt/common/bin/blacklist b/prebuilt/common/bin/blacklist
new file mode 100644
index 0000000..03c36c4
--- /dev/null
+++ b/prebuilt/common/bin/blacklist
@@ -0,0 +1,4 @@
+80f99c594f7b82c4cbe533e3f5447729
+29f4bab6bae5959458678869350dc888
+77d73f73da664f3592e712b7e7c107c1
+a5019b358023a3a6ae8be3f3380ba5ca
diff --git a/prebuilt/common/bin/whitelist b/prebuilt/common/bin/whitelist
new file mode 100644
index 0000000..ca3f017
--- /dev/null
+++ b/prebuilt/common/bin/whitelist
@@ -0,0 +1 @@
+b0a27bcb5c7504a81e1450a8313e37cb