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/bin/50-hosts.sh b/prebuilt/bin/50-hosts.sh
index ed4805c..fc2a56f 100755
--- a/prebuilt/bin/50-hosts.sh
+++ b/prebuilt/bin/50-hosts.sh
@@ -1,7 +1,9 @@
 #!/sbin/sh
 #
-# /system/addon.d/50-hosts.sh
-# During a firmware upgrade, this script backs up /system/etc/hosts,
+# ADDOND_VERSION=2
+#
+# /system/addon.d/50-lineage.sh
+# During a LineageOS 15.1 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
diff --git a/prebuilt/bin/backuptool.sh b/prebuilt/bin/backuptool.sh
index 45a14e1..a20454f 100755
--- a/prebuilt/bin/backuptool.sh
+++ b/prebuilt/bin/backuptool.sh
@@ -7,6 +7,8 @@
 export S=/system
 export V=8.1
 
+export ADDOND_VERSION=1
+
 DEBUG=0
 
 # Preserve /system/addon.d in /tmp/addon.d
@@ -14,6 +16,16 @@
   rm -rf /tmp/addon.d/
   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
 }