backuptool: introduce addon.d script versioning

* Since A/B addon.d scripts are going to need to do things in a
  specific way or things could go horribly wrong for a user, let's
  introduce versioning so that scripts can claim to be compatible.

* A script can denote it is compatible with addon.d version 2 by
  adding: "# ADDOND_VERSION=2" somewhere in its script.

* Only A/B will require version 2 scripts for now, and version 2
  scripts will still run on non-A/B. Additionally if a script does
  not explicitly denote its version, assume its version 1.

* Version 1: The same old scripts we've always used. We cannot assume
             these will all work with A/B backuptools.

* Version 2: Scripts that denote they are compatible with version 2
             must be aware of the fact that A/B devices will run this
             script for a rom, during a seamless update, mounted at
             /postinstall. The best way to ensure compatibility would
             be to use the pre-designated functions found in the
             backuptool[,_ab].functions scripts.

Change-Id: I5573018dabd21bb64c7c964e2081806072a75243
diff --git a/prebuilt/common/bin/50-bliss.sh b/prebuilt/common/bin/50-bliss.sh
new file mode 100755
index 0000000..a4bf2f0
--- /dev/null
+++ b/prebuilt/common/bin/50-bliss.sh
@@ -0,0 +1,43 @@
+#!/sbin/sh
+# 
+# ADDOND_VERSION=2
+#
+# /system/addon.d/50-bliss.sh
+# During a Slim upgrade, this script backs up /system/etc/hosts,
+# /system is formatted and reinstalled, then the file is restored.
+#
+
+. /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/common/bin/backuptool.functions b/prebuilt/common/bin/backuptool.functions
index 015e315..c34654e 100644
--- a/prebuilt/common/bin/backuptool.functions
+++ b/prebuilt/common/bin/backuptool.functions
@@ -5,7 +5,7 @@
 
 export C=/tmp/backupdir
 export S=/system
-export V=11.4
+export V=11.4-Stable
 
 copy_file() {
   cp -dp "$1" "$2"
diff --git a/prebuilt/common/bin/backuptool.sh b/prebuilt/common/bin/backuptool.sh
index 3e637e1..bfc8f83 100644
--- a/prebuilt/common/bin/backuptool.sh
+++ b/prebuilt/common/bin/backuptool.sh
@@ -5,7 +5,9 @@
 
 export C=/tmp/backupdir
 export S=/system
-export V=11.4
+export V=11.4-Stable
+
+export ADDOND_VERSION=1
 
 # Scripts in /system/addon.d expect to find backuptool.functions in /tmp
 cp -f /tmp/install/bin/backuptool.functions /tmp
@@ -15,6 +17,18 @@
   if [ -d /system/addon.d/ ]; then
     mkdir -p /tmp/addon.d/
     cp -a /system/addon.d/* /tmp/addon.d/
+
+    # Discard any scripts that aren't at least our version level
+    for f in /postinstall/tmp/addon.d/*sh; do
+      SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2)
+      if [ -z "$SCRIPT_VERSION" ]; then
+        SCRIPT_VERSION=1
+      fi
+      if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then
+        rm $f
+      fi
+    done
+
     chmod 755 /tmp/addon.d/*.sh
   fi
 }
@@ -25,7 +39,7 @@
 if [ ! -r /system/build.prop ]; then
     return 0
 fi
-if ( ! grep -q "^ro.build.version.release=$V.*" /system/build.prop ); then
+if ( ! grep -q "^ro.bliss.version=$V.*" /system/build.prop ); then
   echo "Not backing up files from incompatible version: $V"
   return 0
 fi
diff --git a/prebuilt/common/bin/backuptool_ab.functions b/prebuilt/common/bin/backuptool_ab.functions
index 20d5f71..62d9284 100644
--- a/prebuilt/common/bin/backuptool_ab.functions
+++ b/prebuilt/common/bin/backuptool_ab.functions
@@ -5,7 +5,7 @@
 
 export S=/system
 export C=/postinstall/tmp/backupdir
-export V=11.4
+export V=11.4-Stable
 export backuptool_ab=true
 
 copy_file() {
diff --git a/prebuilt/common/bin/backuptool_ab.sh b/prebuilt/common/bin/backuptool_ab.sh
index 3953530..7c8111c 100644
--- a/prebuilt/common/bin/backuptool_ab.sh
+++ b/prebuilt/common/bin/backuptool_ab.sh
@@ -5,7 +5,9 @@
 
 export S=/system
 export C=/postinstall/tmp/backupdir
-export V=15.1
+export V=11.4-Stable
+
+export ADDOND_VERSION=2
 
 # Scripts in /system/addon.d expect to find backuptool.functions in /tmp
 mkdir -p /postinstall/tmp/
@@ -16,6 +18,18 @@
   if [ -d /system/addon.d/ ]; then
     mkdir -p /postinstall/tmp/addon.d/
     cp -a /system/addon.d/* /postinstall/tmp/addon.d/
+
+    # Discard any scripts that aren't at least our version level
+    for f in /postinstall/tmp/addon.d/*sh; do
+      SCRIPT_VERSION=$(grep "^# ADDOND_VERSION=" $f | cut -d= -f2)
+      if [ -z "$SCRIPT_VERSION" ]; then
+        SCRIPT_VERSION=1
+      fi
+      if [ $SCRIPT_VERSION -lt $ADDOND_VERSION ]; then
+        rm $f
+      fi
+    done
+
     chmod 755 /postinstall/tmp/addon.d/*.sh
   fi
 }
@@ -36,7 +50,7 @@
     return 0
 fi
 
-grep -q "^ro.build.version=$V.*" /system/etc/prop.default /system/build.prop && return 1
+grep -q "^ro.bliss.version=$V.*" /system/etc/prop.default /system/build.prop && return 1
 
 echo "Not backing up files from incompatible version: $V"
 return 0