otasigcheck and backuptools from CM

Change-Id: I1b582d57ed58176db83ec046e68884cd9e4a1b6a

Add otasigcheck.sh from cm

Change-Id: I930b06ccd1d8fc2b10bdb5e98418efb05ca28e71
diff --git a/prebuilt/common/bin/backuptool.sh b/prebuilt/common/bin/backuptool.sh
index 9e67f48..7b3e39d 100755
--- a/prebuilt/common/bin/backuptool.sh
+++ b/prebuilt/common/bin/backuptool.sh
@@ -5,7 +5,7 @@
 
 export C=/tmp/backupdir
 export S=/system
-export V=Slim-4.4
+export V=Slim-5.0
 
 # Preserve /system/addon.d in /tmp/addon.d
 preserve_addon_d() {
@@ -20,14 +20,44 @@
   rm -rf /tmp/addon.d/
 }
 
-# Check for proper Slim version
-check_version() {
-  if ( ! grep -q "ro.slim.version=$V.*" /system/build.prop); then
-    echo "Not running backup from incompatible version"
-    exit
+# Proceed only if /system is the expected major and minor version
+check_prereq() {
+if ( ! grep -q "^ro.slim.version=$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 | awk {'print $1'})
+          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 | awk {'print $1'})
+          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() {
 for script in $(find /tmp/addon.d/ -name '*.sh' |sort -n); do
@@ -37,15 +67,25 @@
 
 case "$1" in
   backup)
-    check_version
     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)
-    check_version
+    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