vendor/custom: Backuptool files

This goes along with the releasetools enhancements in build/

Based on CyanogenMod 10.1, but debranded to generic naming for
easier sharing between community projects

Change-Id: I7031af3372a0bda31397e68761f772eccd4cb138
diff --git a/prebuilt/bin/backuptool.sh b/prebuilt/bin/backuptool.sh
new file mode 100755
index 0000000..17e1032
--- /dev/null
+++ b/prebuilt/bin/backuptool.sh
@@ -0,0 +1,74 @@
+#!/sbin/sh
+#
+# Backup and restore addon /system files
+#
+
+export C=/tmp/backupdir
+export S=/system
+export V=4.3
+
+# Preserve /system/addon.d in /tmp/addon.d
+preserve_addon_d() {
+  mkdir -p /tmp/addon.d/
+  cp -a /system/addon.d/* /tmp/addon.d/
+  chmod 755 /tmp/addon.d/*.sh
+}
+
+# Restore /system/addon.d in /tmp/addon.d
+restore_addon_d() {
+  cp -a /tmp/addon.d/* /system/addon.d/
+  rm -rf /tmp/addon.d/
+}
+
+# Proceed only if /system is the expected major and minor version
+check_prereq() {
+if ( ! grep -q "^ro.build.version.release=$V.*" /system/build.prop ); then
+  echo "Not backing up files from incompatible version: $V"
+  exit 127
+fi
+}
+
+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 | awk {'print $1'})
+          grep -q $s /system/addon.d/blacklist && rm -f $f
+      done
+  fi
+}
+
+# Execute /system/addon.d/*.sh scripts with $1 parameter
+run_stage() {
+for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
+  $script $1
+done
+}
+
+case "$1" in
+  backup)
+    mkdir -p $C
+    check_prereq
+    check_blacklist system
+    preserve_addon_d
+    run_stage pre-backup
+    run_stage backup
+    run_stage post-backup
+  ;;
+  restore)
+    check_prereq
+    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