Index: upload.pl
===================================================================
--- upload.pl	(revision 3494)
+++ upload.pl	(working copy)
@@ -121,34 +121,11 @@
         {
             if($File =~ /\.zip$/i)
             {
-                if (upload("$ZipDir/$File")) 
-                {
-                    $failures=0;
-                }
-                else
-                {
-                    $failures++;
-                }
+                upload("$ZipDir/$File")
             }
             $progress++;
             $progressPercent = $progress * 100 / $zipCount;
             statusMessage(scalar(@sorted)." zip files left to upload", $Config{Verbose}, $currentSubTask, $progressJobs, $progressPercent,0);
-        
-## sleep for exponentially increasing amount of time for each consecutive failure to a max of 20 minutes (1200 seconds)
-## FIXME: discern between "soft" and "hard" failures ("soft" failure being a not started upload because of full queue and "hard" being an actual failed upload
-## CHANGEME: since the checking of the queue is much less costly than trying to upload, need to further adapt (reduce) the max delay.
-            if ($failures)
-            {
-                $sleepdelay=1.5 ** $failures;
-                $sleepdelay += rand($sleepdelay/4);
-                $sleepdelay = int($sleepdelay);
-                if ($sleepdelay > 1200)
-                {
-                    $sleepdelay = 1200;
-                }
-                statusMessage($failures . " consecutive upload failures, sleeping for " . $sleepdelay . " seconds", $Config{Verbose}, $currentSubTask, $progressJobs, $progressPercent,0);
-                sleep ($sleepdelay);
-            }
         }
     }
     
@@ -362,42 +339,54 @@
     
     my $Password = join("|", ($Config{UploadUsername}, $Config{UploadPassword}));
     my $URL = $Config{"UploadURL2"};
-    
+
+    my $failures = 0;
     my $UploadToken = UploadOkOrNot();
-    
-    if ($UploadToken) 
+    while (!$UploadToken)
     {
-        statusMessage("Uploading $File", $Config{Verbose}, $currentSubTask, $progressJobs, $progressPercent,0);
-        my $res = $ua->post($URL,
-          Content_Type => 'form-data',
-          Content => [ file => [$File], 
-          mp => $Password,
-          version => $Config{ClientVersion},
-          single_tileset => $SingleTileset,
-          token => $UploadToken ]);
-      
-        if(!$res->is_success())
+        $failures++;
+        
+## sleep for exponentially increasing amount of time for each consecutive failure to a max of 20 minutes (1200 seconds)
+## FIXME: discern between "soft" and "hard" failures ("soft" failure being a not started upload because of full queue and "hard" being an actual failed upload
+## CHANGEME: since the checking of the queue is much less costly than trying to upload, need to further adapt (reduce) the max delay.
+        $sleepdelay=1.5 ** $failures;
+        $sleepdelay += rand($sleepdelay/4);
+        $sleepdelay = int($sleepdelay);
+        if ($sleepdelay > 1200)
         {
-            print STDERR "ERROR\n";
-            print STDERR "  Error uploading $File to $URL:\n";
-            print STDERR "  ".$res->status_line."\n";
-            return 0;
-        } 
-    
-        if($Config{DeleteZipFilesAfterUpload})
-        {
-            unlink($File);
+            $sleepdelay = 1200;
         }
-        else
-        {
-            rename($File, $File."_uploaded");
-        }
+        statusMessage($failures . " consecutive upload failures, sleeping for " . $sleepdelay . " seconds", $Config{Verbose}, $currentSubTask, $progressJobs, $progressPercent,0);
+        sleep ($sleepdelay);
+        $UploadToken = UploadOkOrNot();
     }
-    else
+    
+    statusMessage("Uploading $File", $Config{Verbose}, $currentSubTask, $progressJobs, $progressPercent,0);
+    my $res = $ua->post($URL,
+                        Content_Type => 'form-data',
+                        Content => [ file => [$File], 
+                                     mp => $Password,
+                                     version => $Config{ClientVersion},
+                                     single_tileset => $SingleTileset,
+                                     token => $UploadToken ]);
+    
+    if(!$res->is_success())
     {
+        print STDERR "ERROR\n";
+        print STDERR "  Error uploading $File to $URL:\n";
+        print STDERR "  ".$res->status_line."\n";
         return 0;
-    }
+    } 
     
+    if($Config{DeleteZipFilesAfterUpload})
+    {
+        unlink($File);
+    }
+    else
+    {
+        rename($File, $File."_uploaded");
+    }
+
     return 1;
 }
 
