# HG changeset patch
# User John Donoghue
# Date 1426185460 14400
#      Thu Mar 12 14:37:40 2015 -0400
# Node ID 14e9d95d5d2c4295fc50530ff15c6fafd1e7f900
# Parent  065ccb9a13244b02248ab09539f43329f34d9600
Check if found packages to uninstall before attempting uninstall (Bug #44524)

* scripts/pkg/private/uninstall.m
  (uninstall): check if delete_idx is empty before attempting to delete packages

diff -r 065ccb9a1324 -r 14e9d95d5d2c scripts/pkg/private/uninstall.m
--- a/scripts/pkg/private/uninstall.m	Thu Mar 12 08:15:48 2015 -0700
+++ b/scripts/pkg/private/uninstall.m	Thu Mar 12 14:37:40 2015 -0400
@@ -66,80 +66,85 @@
     endif
   endif
 
-  ## Compute the packages that will remain installed.
-  idx = setdiff (1:num_packages, delete_idx);
-  remaining_packages = {installed_pkgs_lst{idx}};
+  if (length (delete_idx) == 0)
+    warning ("no packages will be uninstalled")
+  else
 
-  ## Check dependencies.
-  if (handle_deps)
-    error_text = "";
-    for i = 1:length (remaining_packages)
-      desc = remaining_packages{i};
-      bad_deps = get_unsatisfied_deps (desc, remaining_packages);
+    ## Compute the packages that will remain installed.
+    idx = setdiff (1:num_packages, delete_idx);
+    remaining_packages = {installed_pkgs_lst{idx}};
 
-      ## Will the uninstallation break any dependencies?
-      if (! isempty (bad_deps))
-        for i = 1:length (bad_deps)
-          dep = bad_deps{i};
-          error_text = [error_text " " desc.name " needs " ...
-                        dep.package " " dep.operator " " dep.version "\n"];
-        endfor
+    ## Check dependencies.
+    if (handle_deps)
+      error_text = "";
+      for i = 1:length (remaining_packages)
+        desc = remaining_packages{i};
+        bad_deps = get_unsatisfied_deps (desc, remaining_packages);
+
+        ## Will the uninstallation break any dependencies?
+        if (! isempty (bad_deps))
+          for i = 1:length (bad_deps)
+            dep = bad_deps{i};
+            error_text = [error_text " " desc.name " needs " ...
+                          dep.package " " dep.operator " " dep.version "\n"];
+          endfor
+        endif
+      endfor
+
+      if (! isempty (error_text))
+        error ("the following dependencies where unsatisfied:\n  %s", error_text);
+      endif
+    endif
+
+    ## Delete the directories containing the packages.
+    for i = delete_idx
+      desc = installed_pkgs_lst{i};
+      ## If an 'on_uninstall.m' exist, call it!
+      if (exist (fullfile (desc.dir, "packinfo", "on_uninstall.m"), "file"))
+        wd = pwd ();
+        cd (fullfile (desc.dir, "packinfo"));
+        on_uninstall (desc);
+        cd (wd);
+      endif
+      ## Do the actual deletion.
+      if (desc.loaded)
+        rmpath (desc.dir);
+        if (exist (getarchdir (desc)))
+          rmpath (getarchdir (desc));
+        endif
+      endif
+      if (exist (desc.dir, "dir"))
+        [status, msg] = rmdir (desc.dir, "s");
+        if (status != 1 && exist (desc.dir, "dir"))
+          error ("couldn't delete directory %s: %s", desc.dir, msg);
+        endif
+        [status, msg] = rmdir (getarchdir (desc), "s");
+        if (status != 1 && exist (getarchdir (desc), "dir"))
+          error ("couldn't delete directory %s: %s", getarchdir (desc), msg);
+        endif
+        if (dirempty (desc.archprefix))
+          rmdir (desc.archprefix, "s");
+        endif
+      else
+        warning ("directory %s previously lost", desc.dir);
       endif
     endfor
 
-    if (! isempty (error_text))
-      error ("the following dependencies where unsatisfied:\n  %s", error_text);
-    endif
-  endif
-
-  ## Delete the directories containing the packages.
-  for i = delete_idx
-    desc = installed_pkgs_lst{i};
-    ## If an 'on_uninstall.m' exist, call it!
-    if (exist (fullfile (desc.dir, "packinfo", "on_uninstall.m"), "file"))
-      wd = pwd ();
-      cd (fullfile (desc.dir, "packinfo"));
-      on_uninstall (desc);
-      cd (wd);
-    endif
-    ## Do the actual deletion.
-    if (desc.loaded)
-      rmpath (desc.dir);
-      if (exist (getarchdir (desc)))
-        rmpath (getarchdir (desc));
-      endif
-    endif
-    if (exist (desc.dir, "dir"))
-      [status, msg] = rmdir (desc.dir, "s");
-      if (status != 1 && exist (desc.dir, "dir"))
-        error ("couldn't delete directory %s: %s", desc.dir, msg);
-      endif
-      [status, msg] = rmdir (getarchdir (desc), "s");
-      if (status != 1 && exist (getarchdir (desc), "dir"))
-        error ("couldn't delete directory %s: %s", getarchdir (desc), msg);
-      endif
-      if (dirempty (desc.archprefix))
-        rmdir (desc.archprefix, "s");
+    ## Write a new ~/.octave_packages.
+    if (global_install)
+      if (length (remaining_packages) == 0)
+        unlink (global_list);
+      else
+        global_packages = save_order (remaining_packages);
+        save (global_list, "global_packages");
       endif
     else
-      warning ("directory %s previously lost", desc.dir);
-    endif
-  endfor
-
-  ## Write a new ~/.octave_packages.
-  if (global_install)
-    if (length (remaining_packages) == 0)
-      unlink (global_list);
-    else
-      global_packages = save_order (remaining_packages);
-      save (global_list, "global_packages");
-    endif
-  else
-    if (length (remaining_packages) == 0)
-      unlink (local_list);
-    else
-      local_packages = save_order (remaining_packages);
-      save (local_list, "local_packages");
+      if (length (remaining_packages) == 0)
+        unlink (local_list);
+      else
+        local_packages = save_order (remaining_packages);
+        save (local_list, "local_packages");
+      endif
     endif
   endif
 
