backuptool: Fix backup/restore functionality

Backup/restore functionality was broken in the
Ia1f4ae95c9e4dae4df844853e81c264bc838f177 change
because of incorrect check of the function's result.

check_prereq() function refactored to return 0 if
backuping/restoration is possible. Any work should be
performed only if check_prereq() succeeds.

Change-Id: Ic977dba675df58a228ef4b882b25beb66cc9d2c6
diff --git a/prebuilt/common/bin/backuptool.sh b/prebuilt/common/bin/backuptool.sh
index 2f14522..3178842 100755
--- a/prebuilt/common/bin/backuptool.sh
+++ b/prebuilt/common/bin/backuptool.sh
@@ -26,16 +26,16 @@
 
 # Restore only if backup has the expected major and minor version
 check_prereq() {
-  if [ ! -f /tmp/build.prop ]; then
-    # this will block any backups made before 8 cause file was not copied before
-    echo "Not restoring files from incompatible version: $V"
-    return 0
-  fi
-  if ( ! grep -q "^ro.build.version.release=$V.*" /tmp/build.prop ); then
-    echo "Not restoring files from incompatible version: $V"
-    return 0
-  fi
+  # If there is no build.prop file the partition is probably empty.
+if [ ! -r $S/build.prop ]; then
+  echo "Backup/restore is not possible. Partition is probably empty"
   return 1
+fi
+  if ( ! grep -q "^ro.build.version.release=$V.*" /tmp/build.prop ); then
+    echo "Backup/restore is not possible. Incompatible ROM version: $V"
+  return 2
+  fi
+  return 0
 }
 
 # Execute /system/addon.d/*.sh scripts with $1 parameter
@@ -81,30 +81,26 @@
     cp $S/bin/backuptool.functions /tmp
     cp $S/build.prop /tmp
     mount_system
-    mkdir -p $C
-    if ! check_prereq; then
-      unmount_system
-      exit 127
-    end
-    preserve_addon_d
-    run_stage pre-backup
-    run_stage backup
-    run_stage post-backup
+    if check_prereq; then
+      mkdir -p $C
+      preserve_addon_d
+      run_stage pre-backup
+      run_stage backup
+      run_stage post-backup
+    fi
     unmount_system
   ;;
   restore)
     cp $S/bin/backuptool.functions /tmp
     mount_system
-    if ! check_prereq; then
-      unmount_system
-      exit 127
-    end
-    run_stage pre-restore
-    run_stage restore
-    run_stage post-restore
-    restore_addon_d
-    rm -rf $C
-    sync
+    if check_prereq; then
+      run_stage pre-restore
+      run_stage restore
+      run_stage post-restore
+      restore_addon_d
+      rm -rf $C
+      sync
+    fi
     unmount_system
   ;;
   *)
diff --git a/prebuilt/common/bin/backuptool_ab.sh b/prebuilt/common/bin/backuptool_ab.sh
index d542700..0dbbb9a 100755
--- a/prebuilt/common/bin/backuptool_ab.sh
+++ b/prebuilt/common/bin/backuptool_ab.sh
@@ -47,14 +47,15 @@
 check_prereq() {
 # If there is no build.prop file the partition is probably empty.
 if [ ! -r /system/build.prop ]; then
-    return 0
+  echo "Backup/restore is not possible. Partition is probably empty"
+  return 1
 fi
 
 if ! grep -q "^ro.omni.version=$V.*" /product/build.prop; then
-    echo "Not backing up files from incompatible version: $V"
-    return 0
+  echo "Backup/restore is not possible. Incompatible ROM version: $V"
+  return 2
 fi
-return 1
+return 0
 }
 
 # Execute /system/addon.d/*.sh scripts with $1 parameter
@@ -78,30 +79,25 @@
 
 case "$1" in
   backup)
-    mkdir -p $C
-    if ! check_prereq; then
-      exit 127
+    if check_prereq; then
+      mkdir -p $C
+      preserve_addon_d
+      run_stage pre-backup
+      run_stage backup
+      run_stage post-backup
     fi
     log -t "update_engine" "backuptool_ab.sh backup"
-
-    preserve_addon_d
-    run_stage pre-backup
-    run_stage backup
-    run_stage post-backup
   ;;
   restore)
-    if ! check_prereq; then
-      exit 127
-    fi
+    if check_prereq; then
+      run_stage pre-restore
+      run_stage restore
+      run_stage post-restore
+      restore_addon_d
+      rm -rf $C
+      rm -rf /postinstall/tmp
+      sync
     log -t "update_engine" "backuptool_ab.sh restore"
-
-    run_stage pre-restore
-    run_stage restore
-    run_stage post-restore
-    restore_addon_d
-    rm -rf $C
-    rm -rf /postinstall/tmp
-    sync
   ;;
   *)
     echo "Usage: $0 {backup|restore}"