Index: commands/core/core.drush.inc
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/drush/commands/core/core.drush.inc,v
retrieving revision 1.119
diff -u -p -r1.119 core.drush.inc
--- commands/core/core.drush.inc	23 Sep 2010 00:33:02 -0000	1.119
+++ commands/core/core.drush.inc	30 Sep 2010 03:27:55 -0000
@@ -76,6 +76,7 @@ function core_drush_command() {
     'options' => array(
       '--script-path' => "Additional paths to search for scripts.  Use POSIX path separator (':') for multiple paths.",
     ),
+    'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap.
     'aliases' => array('scr'),
     'deprecated-aliases' => array('script'),
   );
@@ -659,51 +660,93 @@ function _drush_core_is_named_in_array($
  * Command callback. Runs "naked" php scripts.
  */
 function drush_core_php_script() {
+  drush_bootstrap_max();
   $args = func_get_args();
+  $script = $args[0];
+  $found = FALSE;
+  
+  if (file_exists($script)) {
+    $found = $script;
+  }
+  else {
+    // Array of paths to search for scripts
+    $searchpath['DIR'] = dirname(__FILE__);
+    $searchpath['cwd'] = drush_cwd();
 
-  // Array of paths to search for scripts
-  $searchpath['DIR'] = dirname(__FILE__);
-  $searchpath['cwd'] = drush_cwd();
-
-  // Additional script paths, specified by 'script-path' option
-  if ($script_path = drush_get_option('script-path', FALSE)) {
-    foreach (explode(":", $script_path) as $path) {
-      $searchpath[] = $path;
+    // Additional script paths, specified by 'script-path' option
+    if ($script_path = drush_get_option('script-path', FALSE)) {
+      foreach (explode(":", $script_path) as $path) {
+	$searchpath[] = $path;
+      }
     }
-  }
 
-  if (empty($args)) {
-    // List all available scripts.
-    $all = array();
-    foreach($searchpath as $key => $path) {
-      $recurse = !$key == 'cwd';
-      $all = array_merge( $all , array_keys(drush_scan_directory($path, '/\.php$/', array('.', '..', 'CVS'), NULL, $recurse)) );
+    if (!isset($script)) {
+      // List all available scripts.
+      $all = array();
+      foreach($searchpath as $key => $path) {
+	$recurse = !(($key == 'cwd') || ($path == '/'));
+	$all = array_merge( $all , array_keys(drush_scan_directory($path, '/\.php$/', array('.', '..', 'CVS'), NULL, $recurse)) );
+      }
+      drush_print(implode("\n", $all));
     }
-    drush_print(implode("\n", $all));
-  }
-  else {
-    // Execute the specified script.
-    $script = $args[0];
-    if (!preg_match('/\.php$/i', $script)) {
-      $script .= '.php';
-    }
-    $found = FALSE;
-    foreach($searchpath as $path) {
-      $script_filename = $path . '/' . $script;
-      if (file_exists($script_filename)) {
-        include($script_filename);
-        $found = TRUE;
-        break;
+    else {
+      // Execute the specified script.
+      foreach($searchpath as $path) {
+	$script_filename = $path . '/' . $script;
+	if (file_exists($script_filename . ".php")) {
+          $script_filename .= ".php";
+	}
+	if (file_exists($script_filename)) {
+          $found = $script_filename;
+          break;
+	}
+	$all[] = $script_filename;
+      }
+      if (!$found) {
+	drush_print('nope, not found');
+	drush_set_error('Script not found.', dt('Unable to find any of the following: @files', array('@files' => implode(', ', $all))));
       }
-      $all[] = $script_filename;
     }
+  }
+  
+  if ($found) {
+    if (_drush_core_eval_shebang_script($found) === FALSE) {
+      include($found);
+    }
+  }
+}
 
-    if (!$found) {
-      drush_set_error('Script not found.', dt('Unable to find any of the following: @files', array('@files' => implode(', ', $all))));
+/*
+ * Evaluate a script that begins with #!drush php-script
+ */
+function _drush_core_eval_shebang_script($script_filename) {
+  $found = FALSE;
+  $fp = fopen($script_filename, "r");
+  if ($fp !== FALSE) {
+    $line = fgets($fp);
+    if (($line[0] == '#') && (strstr($line, 'drush'))) {
+      $first_script_line = '';
+      while ($line = fgets($fp)) {
+	$line = trim($line);
+	if ($line == '<?php') {
+	  $found = TRUE;
+	  break;
+	}
+	elseif (!empty($line)) {
+	  $first_script_line = $line . "\n";
+	  break;
+	}
+      }
+      $script = stream_get_contents($fp);
+      eval($first_script_line . $script);
+      $found = TRUE;
     }
+    fclose($fp);
   }
+  return $found;
 }
 
+
 function drush_core_php_eval($command) {
   eval($command . ';');
 }
@@ -901,4 +944,4 @@ EOD;
   }
 
   return $bashrc_data;
-}
\ No newline at end of file
+}
