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/config/common.mk b/config/common.mk
index 35451c3..a2b2f10 100644
--- a/config/common.mk
+++ b/config/common.mk
@@ -20,6 +20,13 @@
 ADDITIONAL_DEFAULT_PROPERTIES += ro.adb.secure=1
 endif
 
+# Backup Tool
+PRODUCT_COPY_FILES += \
+    vendor/custom/prebuilt/bin/backuptool.sh:system/bin/backuptool.sh \
+    vendor/custom/prebuilt/bin/backuptool.functions:system/bin/backuptool.functions \
+    vendor/custom/prebuilt/bin/50-example.sh:system/addon.d/50-example.sh \
+    vendor/custom/prebuilt/bin/blacklist:system/addon.d/blacklist
+
 # init.d support
 PRODUCT_COPY_FILES += \
 	vendor/custom/prebuilt/etc/init.d/00banner:system/etc/init.d/00banner \
diff --git a/prebuilt/bin/50-hosts.sh b/prebuilt/bin/50-hosts.sh
new file mode 100755
index 0000000..ed4805c
--- /dev/null
+++ b/prebuilt/bin/50-hosts.sh
@@ -0,0 +1,44 @@
+#!/sbin/sh
+#
+# /system/addon.d/50-hosts.sh
+# During a firmware upgrade, this script backs up /system/etc/hosts,
+# /system is formatted and reinstalled, then the file is restored.
+#
+# This script is primarily used as an example of how to use backuptool
+#
+# Originally implemented in CyanogenMod
+
+. /tmp/backuptool.functions
+
+list_files() {
+cat <<EOF
+etc/hosts
+EOF
+}
+
+case "$1" in
+  backup)
+    list_files | while read FILE DUMMY; do
+      backup_file $S/"$FILE"
+    done
+  ;;
+  restore)
+    list_files | while read FILE REPLACEMENT; do
+      R=""
+      [ -n "$REPLACEMENT" ] && R="$S/$REPLACEMENT"
+      [ -f "$C/$S/$FILE" ] && restore_file $S/"$FILE" "$R"
+    done
+  ;;
+  pre-backup)
+    # Stub
+  ;;
+  post-backup)
+    # Stub
+  ;;
+  pre-restore)
+    # Stub
+  ;;
+  post-restore)
+    # Stub
+  ;;
+esac
diff --git a/prebuilt/bin/backuptool.functions b/prebuilt/bin/backuptool.functions
new file mode 100644
index 0000000..1d80a9b
--- /dev/null
+++ b/prebuilt/bin/backuptool.functions
@@ -0,0 +1,36 @@
+#!/sbin/sh
+#
+# Functions for backuptool.sh
+#
+
+export C=/tmp/backupdir
+export S=/system
+
+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/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
diff --git a/prebuilt/bin/blacklist b/prebuilt/bin/blacklist
new file mode 100644
index 0000000..03c36c4
--- /dev/null
+++ b/prebuilt/bin/blacklist
@@ -0,0 +1,4 @@
+80f99c594f7b82c4cbe533e3f5447729
+29f4bab6bae5959458678869350dc888
+77d73f73da664f3592e712b7e7c107c1
+a5019b358023a3a6ae8be3f3380ba5ca