From 91dfd1df513416bacd1a74e506ddd2b79c8d4716 Mon Sep 17 00:00:00 2001
From: Bob Mottram <bob@robotics.uk.to>
Date: Mon, 24 Oct 2016 17:55:05 +0100
Subject: [PATCH] Restore apps in a loop

---
 src/freedombone-controlpanel | 139 ++++++++++++++++++-----------------
 1 file changed, 72 insertions(+), 67 deletions(-)

diff --git a/src/freedombone-controlpanel b/src/freedombone-controlpanel
index 7fc58b4e7..bd64539a1 100755
--- a/src/freedombone-controlpanel
+++ b/src/freedombone-controlpanel
@@ -906,96 +906,101 @@ function restore_data_from_storage {
 
     detect_apps
 
-    app_list=()
-    AllStr=$"all"
-    n=1
-    applist="$n $AllStr off"
-    n=$[n+1]
-
-    util_index=0
-    for a in "${utils_installed[@]}"
+    while true
     do
-        applist="$applist $n $a off"
-        app_name=${utils_installed[util_index]}
+
+        app_list=()
+        AllStr=$"all"
+        n=1
+        applist="$n $AllStr off"
         n=$[n+1]
-        util_index=$[util_index+1]
-        app_list+=("$app_name")
-    done
 
-    app_index=0
-    for a in "${APPS_INSTALLED_NAMES[@]}"
-    do
-        applist="$applist $n $a off"
+        util_index=0
+        for a in "${utils_installed[@]}"
+        do
+            applist="$applist $n $a off"
+            app_name=${utils_installed[util_index]}
+            n=$[n+1]
+            util_index=$[util_index+1]
+            app_list+=("$app_name")
+        done
+
+        app_index=0
+        for a in "${APPS_INSTALLED_NAMES[@]}"
+        do
+            applist="$applist $n $a off"
+            n=$[n+1]
+            app_name=${APPS_INSTALLED_NAMES[app_index]}
+            app_index=$[app_index+1]
+            app_list+=("$app_name")
+        done
+        ExitStr=$"Exit"
+        applist="$applist $n $ExitStr on"
         n=$[n+1]
-        app_name=${APPS_INSTALLED_NAMES[app_index]}
-        app_index=$[app_index+1]
-        app_list+=("$app_name")
-    done
-    ExitStr=$"Exit"
-    applist="$applist $n $ExitStr on"
-    n=$[n+1]
 
-    choices=$(dialog --stdout --backtitle $"Freedombone" \
-                     --title $"Restore apps" \
-                     --radiolist $'Choose:' \
-                     30 40 20 $applist)
+        choices=$(dialog --stdout --backtitle $"Freedombone" \
+                         --title $"Restore apps" \
+                         --radiolist $'Choose:' \
+                         30 40 20 $applist)
 
-    sel=$?
-    case $sel in
-        1) return;;
-        255) return;;
-    esac
-    app_name=$(cat $data)
+        sel=$?
+        case $sel in
+            1) return;;
+            255) return;;
+        esac
+        app_name=$(cat $data)
 
-    # exit
-    if [[ $app_name == "$ExitStr" ]]; then
-        return
-    fi
+        # exit
+        if [[ $app_name == "$ExitStr" ]]; then
+            break
+        fi
 
-    clear
+        clear
+
+        # Restore all
+        if [[ $app_name == "$AllStr" ]]; then
+            $restore_command
+            retcode="$?"
+            if [[ "$retcode" != "0" ]]; then
+                if [[ "$1" == "local" ]]; then
+                    dialog --title $"Restore all apps from USB" \
+                           --msgbox $"Restore failed with code $retcode" 6 60
+                else
+                    dialog --title $"Restore all apps from $1" \
+                           --msgbox $"Restore failed with code $retcode" 6 60
+                fi
+                break
+            fi
 
-    # Restore all
-    if [[ $app_name == "$AllStr" ]]; then
-        $restore_command
-        retcode="$?"
-        if [[ "$retcode" != "0" ]]; then
             if [[ "$1" == "local" ]]; then
                 dialog --title $"Restore all apps from USB" \
-                       --msgbox $"Restore failed with code $retcode" 6 60
+                       --msgbox $"Restore complete" 6 40
             else
                 dialog --title $"Restore all apps from $1" \
-                       --msgbox $"Restore failed with code $retcode" 6 60
+                       --msgbox $"Restore complete" 6 40
             fi
+            break
+        fi
+
+        # Restore an app
+        $restore_command "${app_name}"
+        retcode="$?"
+        if [[ "$retcode" != "0" ]]; then
+            dialog --title $"Restore apps from USB" \
+                   --msgbox $"Restore of ${app_name} failed with code $retcode" 6 60
             return
         fi
 
+        # finished
         if [[ "$1" == "local" ]]; then
-            dialog --title $"Restore all apps from USB" \
+            dialog --title $"Restore apps from USB" \
                    --msgbox $"Restore complete" 6 40
         else
-            dialog --title $"Restore all apps from $1" \
+            dialog --title $"Restore apps from $1" \
                    --msgbox $"Restore complete" 6 40
         fi
-        return
-    fi
-
-    # Restore an app
-    $restore_command "${app_name}"
-    retcode="$?"
-    if [[ "$retcode" != "0" ]]; then
-        dialog --title $"Restore apps from USB" \
-               --msgbox $"Restore of ${app_name} failed with code $retcode" 6 60
-        return
-    fi
 
-    # finished
-    if [[ "$1" == "local" ]]; then
-        dialog --title $"Restore apps from USB" \
-               --msgbox $"Restore complete" 6 40
-    else
-        dialog --title $"Restore apps from $1" \
-               --msgbox $"Restore complete" 6 40
-    fi
+    done
 }
 
 function restore_data {
-- 
GitLab