Index: commands/pm/pm.drush.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/commands/pm/pm.drush.inc,v
retrieving revision 1.39
diff -u -r1.39 pm.drush.inc
--- commands/pm/pm.drush.inc	2 Jun 2009 19:37:41 -0000	1.39
+++ commands/pm/pm.drush.inc	9 Jun 2009 02:23:35 -0000
@@ -522,8 +522,46 @@
   return false;
 }
 
+/**
+ * Determine a candidate destination directory for a particular site path and
+ * return it if it exists, optionally attempting to create the directory.
+ */
+function pm_dl_destination_lookup($type, $drupal_root, $sitepath, $create = FALSE) {
+  switch ($type) {
+    case 'module':
+      $destination = $sitepath . 'modules/';
+      break;
+    case 'theme':
+      $destination = $sitepath . 'themes/';
+      break;
+    case 'theme engine':
+      $destination = $sitepath . 'themes/engines/';
+      break;
+    case 'translation':
+      $destination = $drupal_root . '/';
+      break;
+    case 'profile':
+      $destination = $drupal_root . 'profiles/';
+      break;
+  }
+  if ($create) {
+    drush_log(dt('Attempting to create destination directory at !dir', array('!dir' => $destination)));
+    @drush_op('mkdir', $destination, 0777, TRUE);
+  }
+  if (is_dir($destination)) {
+    drush_log(dt('Using destination directory !dir', array('!dir' => $destination)));
+    return $destination;
+  }
+  drush_log(dt('Could not find destination directory at !dir', array('!dir' => $destination)));
+  return FALSE;
+}
 
+/**
+ * Return the best destination for a particular download type we can find,
+ * given the drupal and site contexts.
+ */
 function pm_dl_destination($type) {
+  // Attempt 0: Use the user specified destination directory, if it exists. 
   $destination = drush_get_option('destination');
   if (!empty($destination)) {
     $destination = rtrim($destination, '/') . '/';
@@ -536,42 +574,41 @@
   }
   
   $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
-  $site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT', false); 
+  $site_root = drush_get_context('DRUSH_DRUPAL_SITE_ROOT', FALSE);
+  $full_site_root = $drupal_root .'/'. $site_root . '/';
+  $sites_all = $drupal_root . '/sites/all/';
+
+  $in_site_directory = FALSE;
+  // Check if we are running within the site directory.
+  if ($full_site_root == substr(drush_cwd() . '/', 0, strlen($full_site_root))) {
+    $in_site_directory = TRUE;
+  }
 
-  // If a URI is provided and we bootstrapped successfully then we install to
-  // that specific site, otherwise we install to sites/all/modules
-  if ($site_root && $site_root !== 'sites/default') {
-    $sitepath = $drupal_root .'/'. $site_root .'/';
+  // Attempt 1: If we are in a specific site directory, and the destination directory already exists, then we use that.
+  if (empty($destination) && $site_root && $in_site_directory) {
+    $destination = pm_dl_destination_lookup($type, $drupal_root, $full_site_root);
   }
-  else if ($drupal_root) {
-    $sitepath = $drupal_root . '/sites/all/';
+  // Attempt 2: If the destination directory already exists for sites/all, then we use that.
+  if (empty($destination) && $drupal_root) {
+    $destination = pm_dl_destination_lookup($type, $drupal_root, $sites_all);
   }
-  else {
-    $sitepath = '';
+  // Attempt 3: If a specific (non default) site directory exists and sites/all does not exist, then we create destination in the site specific directory.
+  if (empty($destination) && $site_root && $site_root !== 'sites/default' && is_dir($full_site_root) && !is_dir($sites_all)) {
+    $destination = pm_dl_destination_lookup($type, $drupal_root, $full_site_root, TRUE);
   }
-
-  switch ($type) {
-    case 'module':
-      $destination = $sitepath . 'modules/';
-      break;
-    case 'theme':
-      $destination = $sitepath . 'themes/';
-      break;
-    case 'theme engine':
-      $destination = $sitepath . 'themes/engines/';
-      break;
-    case 'translation':
-      $destination = $drupal_root . '/';
-      break;
-    case 'profile':
-      $destination = $drupal_root . 'profiles/';
-      break;
+  // Attempt 4: If sites/all exists, then we create destination in the sites/all directory.
+  if (empty($destination) && is_dir($sites_all)) {
+    $destination = pm_dl_destination_lookup($type, $drupal_root, $sites_all, TRUE);
   }
-
-  // By default (including for core) we always fall back to the current directory.
+  // Attempt 5: If site directory exists (even default), then we create destination in the this directory.
+  if (empty($destination) && $site_root && is_dir($full_site_root)) {
+    $destination = pm_dl_destination_lookup($type, $drupal_root, $full_site_root, TRUE);
+  }
+  // Attempt 6: If we didn't find a valid directory yet (or we somehow found one that doesn't exist) we always fall back to the current directory.
   if (empty($destination) || !is_dir($destination)) {
     $destination = drush_cwd() . '/';
   }
+
   return $destination;
 }
 
