--- drush_make.drush.inc.orig	2009-11-14 16:20:30.000000000 -0600
+++ drush_make.drush.inc	2009-11-17 11:38:33.000000000 -0600
@@ -19,6 +19,12 @@ function drush_make_drush_command() {
     'bootstrap' => DRUSH_BOOTSTRAP_DRUSH,
   );
 
+  $items['generate makefile'] = array(
+    'description' => 'Attempts to generate a makefile from the current drupal install.',
+    'callback' => 'drush_generate_makefile',
+    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
+  );
+
   return $items;
 }
 
@@ -29,7 +35,141 @@ function drush_make_drush_help($section)
   switch ($section) {
     case 'drush:make':
       return dt("@TODO: Print information on the format of the makefile here.");
+    case 'drush:generate_makefile':
+      return dt("This command attempts to generate a makefile from the current drupal install. It is imperfect in that it cannot always find the sources of various components.");
+  }
+}
+
+/**
+ * Drush callback; generate makefile from the current build.
+ */
+function drush_generate_makefile() {
+  print "Beginning makefile generation...\n";
+  $makefile = '';
+
+  // Drupal core section.
+  print "Generating core section...\n";
+  $makefile .= 'core = ' . DRUPAL_CORE_COMPATIBILITY . "\n";
+  include('includes/install.inc');
+  include(drupal_get_path('module', 'system') . '/system.install');
+  $system_requirements = system_requirements('runtime');
+  $core_project = strtolower($system_requirements['drupal']['title']); 
+  $makefile .= 'projects[] = "' . $core_project . '"' . "\n";
+  if ($core_project != 'drupal') {
+    $makefile .= '; Non-standard Drupal - Please enter the required information.' . "\n";
+    $makefile .= '; projects[' . $core_project . '][download][type] = "DOWNLOADTYPE"' . "\n";
+    $makefile .= '; projects[' . $core_project . '][download][url] = "DOWNLOADURL"' . "\n";
+    $makefile .= '; projects[' . $core_project . '][download][branch] = "BRANCHNAME"' . "\n";
+  }
+  $makefile .= "\n";
+
+  // Non-default profiles section.
+  print "Generating profile section...\n";
+  $install_profile = variable_get('install_profile', '');
+  if ($install_profile != 'default' && $install_profile != '') {
+    $makefile .= 'projects[' . $install_profile . '][type] = "profile"' . "\n";
+    $project_check = _drush_generate_makefile_check_project($install_profile, 'profile');
+    if ($project_check !== TRUE) $makefile .= $project_check;
+  }
+  $makefile .= "\n";
+
+  // Enabled modules section.
+  print "Generating modules section...\n";
+  $module_info = drush_get_modules();
+  foreach ($module_info as $module) {
+    // The project name is required to be the same as module name to avoid listing out submodules.
+    if ($module->info['project'] != 'drupal' 
+        && $module->info['project'] == $module->name 
+        && $module->status) {
+      $makefile .= 'projects[] = ' . $module->name . "\n";
+      // Check for non-default installation location.
+      $path_check = _drush_generate_makefile_check_location($module->name, 'module');
+      if ($path_check !== TRUE) $makefile .= $path_check;
+      $project_check = _drush_generate_makefile_check_project($module->name, 'module');
+      if ($project_check !== TRUE) $makefile .= $project_check;
+    }
+  }
+  $makefile .= "\n";
+
+  // Libraries section. (requires the libraries module)
+  print "Generating libraries section...\n";
+  if (function_exists('libraries_get_libraries')) {
+    $libraries = libraries_get_libraries();
+    foreach ($libraries as $library_name => $library_path) {
+      $path = explode('/', $library_path);
+      $makefile .= 'libraries[' . $library_name . '][download][type] = "get"' . "\n";
+      $makefile .= 'libraries[' . $library_name . '][directory_name] = "' . $path[(count($path) - 1)] . '"' . "\n";
+      $makefile .= 'libraries[' . $library_name . '][download][url] = "DOWNLOADURL"' . "\n";
+    }
+  }
+  $makefile .= "\n";
+
+  // Themes section.
+  print "Generating themes section...\n";
+  // Re-build theme data to be sure we have the most current information.
+  if (function_exists('system_theme_data')) system_theme_data();
+  if (function_exists('_system_theme_data')) {
+    $theme_info = _system_theme_data();
+    foreach ($theme_info as $theme) {
+      if ($theme->info['project'] != 'drupal') {
+        $makefile .= 'projects[] = ' . $theme->name . "\n";
+        $path_check = _drush_generate_makefile_check_location($theme->name, 'theme');
+        if ($path_check !== TRUE) $makefile .= $path_check;
+        $project_check = _drush_generate_makefile_check_project($theme->name, 'theme');
+        if ($project_check !== TRUE) $makefile .= $project_check;
+      }
+    }
+  }
+  print "Makefile output follows.\n\n";
+  print "; Makefile auto-generated by drush make on " . date(DATE_RFC822) . "\n";
+  print $makefile;
+
+}
+
+/**
+ * Helper function to check if a detected project lives on drupal.org or not.
+ */
+function _drush_generate_makefile_check_project($name, $type) {
+  // First we set up the project information array.
+  $project = array();
+  $project['name'] = $name;
+  $project['location'] = DRUSH_MAKE_UPDATE_DEFAULT_URL;
+  $project['core'] = DRUPAL_CORE_COMPATIBILITY;
+  $project['download'] = TRUE;
+  // Now we get the project information.
+  $update_check = drush_make_updatexml($project);
+  if ($update_check === FALSE || $type != $update_check['type']) {
+    // The check for releases on drupal.org failed, or the types do not match. (profile instead of module, or similar) 
+    // Therefore, we're assuming that this is not a drupal.org project.
+    $return_info = '; Release information for this ' . $type . ' was not detected on drupal.org.' . "\n";
+    $return_info .= '; Please enter the required information.' . "\n";
+    $return_info .= '; projects[' . $name . '][download][type] = "DOWNLOADTYPE"' . "\n";
+    $return_info .= '; projects[' . $name . '][download][url] = "DOWNLOADURL"' . "\n";
+    $return_info .= '; projects[' . $name . '][download][branch] = "BRANCHNAME"' . "\n";
+    return $return_info;
+  } 
+  return TRUE;
+}
+
+/**
+ * Helper function to check for a non-default installation location.
+ */
+function _drush_generate_makefile_check_location($name, $type) {
+  // Check for non-default installation location.
+  $path = drupal_get_path($type, $name);
+  $default_location = 'sites/all/' . $type . 's';
+  if ($path != $default_location . '/' . $name) {
+    if (substr($path, 0, strlen($default_location)) != $default_location) {
+      // If the beginning of the path isn't correct, return the install_path.
+      $output .= 'projects[' . $name . '][install_path] = "' . $path . '"' . "\n";
+    } else {
+      // If it's in a subdir of sites/all/modules, set the subdir.
+      $subdir = preg_replace(array('@^sites/all/' . $type . 's/@', "@/$name" . '$@'), '', $path);
+      $output .= 'projects[' . $name . '][subdir] = "' . $subdir . '"' . "\n";
+    }
+    return $output;
   }
+  return TRUE;
 }
 
 /**
