Index: includes/bootstrap.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/bootstrap.inc,v
retrieving revision 1.107
diff -u -F^f -r1.107 bootstrap.inc
--- includes/bootstrap.inc	22 Jul 2006 07:00:30 -0000	1.107
+++ includes/bootstrap.inc	31 Jul 2006 03:29:14 -0000
@@ -192,11 +192,14 @@ function conf_init() {
  * @param $filename
  *   The filename of the item if it is to be set explicitly rather
  *   than by consulting the database.
+ * @param $check_db
+ *   Allows the database search to be skipped (useful for pre-bootstrap
+ *   checks where configuration paths must still be respected). 
  *
  * @return
  *   The filename of the requested item.
  */
-function drupal_get_filename($type, $name, $filename = NULL) {
+function drupal_get_filename($type, $name, $filename = NULL, $check_db = TRUE) {
   static $files = array();
 
   if (!isset($files[$type])) {
@@ -209,7 +212,7 @@ function drupal_get_filename($type, $nam
   elseif (isset($files[$type][$name])) {
     // nothing
   }
-  elseif (($file = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file)) {
+  elseif ($check_db && (($file = db_result(db_query("SELECT filename FROM {system} WHERE name = '%s' AND type = '%s'", $name, $type))) && file_exists($file))) {
     $files[$type][$name] = $file;
   }
   else {
Index: includes/install.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/install.inc,v
retrieving revision 1.9
diff -u -F^f -r1.9 install.inc
--- includes/install.inc	29 Jul 2006 17:56:41 -0000	1.9
+++ includes/install.inc	31 Jul 2006 03:29:14 -0000
@@ -270,75 +270,79 @@ function drupal_install_profile($profile
 
   require_once($profile_file);
 
-  // Get a list of modules absolutely required by Drupal.
-  $bootstrap_list = array('system');
-
   // Get a list of modules required by this profile.
   $function = $profile .'_profile_modules';
   $module_list = $function();
-
-  // If anyone has added modules that we automatically install, filter them out.
-  $module_list = array_diff($module_list, $bootstrap_list);
+  
+  $install_list = array_diff($module_list, array('system'));
+  $verify_list = array_merge($module_list, array('system'));
 
   // Verify that all required modules exist.
-  $bootstrap_modules = drupal_find_modules($bootstrap_list);
-  $profile_modules = drupal_find_modules($module_list);
-
-  // Install the essential system modules and bootstrap Drupal.
-  drupal_install_modules($bootstrap_list);
-  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
-
-  // Install schemas for profile and all its modules.
-  $function = $profile .'_install';
-  if (function_exists($function)) {
-    $function();
-  }
-
-  drupal_install_modules($module_list);
-
-  // Enable the modules required by the profile.
-  db_query("DELETE FROM {system} WHERE type = 'module'");
-  $modules = array_merge($bootstrap_modules, $profile_modules);
-  foreach ($modules as $module) {
-    db_query("INSERT INTO {system} (filename, name, type, description, status, throttle, bootstrap, schema_version) VALUES('%s', '%s', 'module', '', 1, 0, 0, 0)", $module->filename, $module->name);
+  $modules_present = TRUE;
+  foreach ($verify_list as $module) {
+    $module_path = dirname(drupal_get_filename('module', $module, NULL, FALSE));
+    if (!$module_path) {
+      drupal_set_message(st('The %module module is required but was not found. Please move it into the <em>modules</em> subdirectory.', array('%module' => $module)), 'error');
+      $modules_present = FALSE;
+    }
+  }
+  
+  if ($modules_present) {
+    // We've found everything we need, we can start the installation.
+ 
+    // The system module is a special case; we can't bootstrap until it's
+    // installed, so we can't use the normal installation function.
+    $system_path = dirname(drupal_get_filename('module', 'system', NULL, FALSE));
+    require_once './' . $system_path . '/system.install';
+    module_invoke('system', 'install');
+    $system_versions = drupal_get_schema_versions('system');
+    $system_version = $system_versions ? max($system_versions) : SCHEMA_INSTALLED;
+    db_query("INSERT INTO {system} (filename, name, type, description, status, throttle, bootstrap, schema_version) VALUES('%s', '%s', 'module', '', 1, 0, 0, %d)", $system_path . '/system.module', 'system', $system_version);
+  
+    // Now that we've installed things properly, bootstrap the full Drupal environment
+    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
+
+    // Install schemas for profile and all its modules.
+    module_rebuild_cache();
+    drupal_install_modules($install_list);
+    
+    // And now, run the profile's install function.
+    $function = $profile .'_install';
+    if (function_exists($function)) {
+      $function();
+    }
   }
 }
 
 
 /**
- * Finds the file paths for a set of modules.
+ * Execute the install scripts for a set of modules.
  *
  * @param module_list
- *   The modules to locate.
- * @return
- *   An array containing file information for the modules.
+ *   The modules to install.
  */
-function drupal_find_modules($module_list = array()) {
-  $modules = array();
-  foreach ($module_list as $current) {
-    $module = file_scan_directory('./modules', "^$current.module$", array('.', '..', 'CVS'), 0, TRUE, 'name', 0);
-    if (empty($module)) {
-      drupal_set_message(st('The %module module is required but was not found. Please move the file %file into the <em>modules</em> subdirectory.', array('%module' => $current, '%file' => $current .'.module')), 'error');
-    }
-    else {
-      $modules = array_merge($modules, $module);
-    }
+function drupal_install_modules($module_list = array()) {
+  foreach ($module_list as $module) {
+    drupal_install_module($module);
   }
-  return $modules;
 }
 
 /**
- * Execute the install scripts for a set of modules.
+ * Calls the install function and updates the system table for a given module.
  *
- * @param module_list
- *   The modules to install.
+ * @param module
+ *   The module to install.
  */
-function drupal_install_modules($module_list = array()) {
-  // Get a list of all .install files.
-  $installs = drupal_get_install_files($module_list);
-  foreach ($installs as $install) {
-    require_once $install->filename;
-    module_invoke($install->name, 'install');
+function drupal_install_module($module) {
+  if (drupal_get_installed_schema_version($module, TRUE) == SCHEMA_UNINSTALLED) {
+    $install_file = './'. drupal_get_path('module', $module) .'/'. $module .'.install';
+    if (is_file($install_file)) {
+      include_once $install_file;
+    }
+    module_invoke($module, 'install');
+    $versions = drupal_get_schema_versions($module);
+    drupal_set_installed_schema_version($module, $versions ? max($versions) : SCHEMA_INSTALLED);
+    module_enable($module);
   }
 }
 
@@ -349,9 +353,6 @@ function drupal_install_modules($module_
  *   The file to check for.
  * @param $mask
  *   An optional bitmask created from various FILE_* constants.
- * @param $message_type
- *   The type of message to create, can be error or status. Passed on to drupal_set_message as second parameter.
- *   Set to NULL to not output any messages at all.
  * @param $type
  *   The type of file. Can be file (default), dir, or link.
  * @return
Index: includes/module.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/module.inc,v
retrieving revision 1.79
diff -u -F^f -r1.79 module.inc
--- includes/module.inc	13 Jul 2006 13:11:36 -0000	1.79
+++ includes/module.inc	31 Jul 2006 03:29:14 -0000
@@ -88,6 +88,47 @@ function module_list($refresh = FALSE, $
 }
 
 /**
+ * Rebuild the database cache of module files.
+ * 
+ * @return
+ *   The array of filesystem objects used to rebuild the cache.
+ */
+function module_rebuild_cache() {
+  // Get current list of modules
+  $files = system_listing('\.module$', 'modules', 'name', 0);
+
+  // Extract current files from database.
+  system_get_files_database($files, 'module');
+
+  ksort($files);
+
+  foreach ($files as $filename => $file) {
+    drupal_get_filename('module', $file->name, $file->filename);
+    drupal_load('module', $file->name);
+
+    // log the critical hooks implemented by this module
+    $bootstrap = 0;
+    foreach (bootstrap_hooks() as $hook) {
+      if (module_hook($file->name, $hook)) {
+        $bootstrap = 1;
+        break;
+      }
+    }
+
+    // Update the contents of the system table:
+    if (isset($file->status) || (isset($file->old_filename) && $file->old_filename != $file->filename)) {
+      db_query("UPDATE {system} SET description = '%s', name = '%s', filename = '%s', bootstrap = %d WHERE filename = '%s'", $file->description, $file->name, $file->filename, $bootstrap, $file->old_filename);
+    }
+    else {
+      // This is a new module.
+      db_query("INSERT INTO {system} (name, description, type, filename, status, throttle, bootstrap) VALUES ('%s', '%s', '%s', '%s', %d, %d, %d)", $file->name, $file->description, 'module', $file->filename, $file->status, $file->throttle, $bootstrap);
+    }
+  }
+
+  return $files;
+}
+
+/**
  * Determine whether a given module exists.
  *
  * @param $module
@@ -100,6 +141,43 @@ function module_exist($module) {
   return array_key_exists($module, $list);
 }
 
+
+/**
+ * Enable a given module.
+ *
+ * @param $module
+ * Enable a given module and call its enable hook.
+ */
+function module_enable($module) {
+  $existing = db_fetch_object(db_query("SELECT name, status FROM {system} WHERE type = 'module' AND name = '%s'", $module));
+  if ($existing->status === '0') {
+    db_query("UPDATE {system} SET status = 1, throttle = 0 WHERE type = 'module' AND name = '%s'", $module);
+    drupal_load('module', $module);
+    module_invoke($module, 'enable');
+    return TRUE;
+  }
+  else {
+    return FALSE;
+  }
+}
+
+/**
+ * Disable a given module and call its disable hook.
+ *
+ * @param $module
+ *   The name of the module (without the .module extension).
+ */
+function module_disable($module) {
+  if (module_exist($module)) {
+    module_invoke($module, 'disable');
+    db_query("UPDATE {system} SET status = 0, throttle = 0 WHERE type = 'module' AND name = '%s'", $module);
+    return TRUE;
+  }
+  else {
+    return FALSE;
+  }
+}
+
 /**
  * @defgroup hooks Hooks
  * @{
Index: modules/system/system.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.module,v
retrieving revision 1.332
diff -u -F^f -r1.332 system.module
--- modules/system/system.module	10 Jul 2006 21:12:09 -0000	1.332
+++ modules/system/system.module	31 Jul 2006 03:29:14 -0000
@@ -988,12 +988,7 @@ function system_themes_submit($form_id, 
  */
 function system_modules() {
   // Get current list of modules
-  $files = system_listing('\.module$', 'modules', 'name', 0);
-
-  // Extract current files from database.
-  system_get_files_database($files, 'module');
-
-  ksort($files);
+  $files = module_rebuild_cache();
 
   foreach ($files as $filename => $file) {
     drupal_get_filename('module', $file->name, $file->filename);
@@ -1010,27 +1005,8 @@ function system_modules() {
     if ($file->throttle) {
       $throttle[] = $file->name;
     }
-
-    // log the critical hooks implemented by this module
-    $bootstrap = 0;
-    foreach (bootstrap_hooks() as $hook) {
-      if (module_hook($file->name, $hook)) {
-        $bootstrap = 1;
-        break;
-      }
-    }
-
-    // Update the contents of the system table:
-    if (isset($file->status) || (isset($file->old_filename) && $file->old_filename != $file->filename)) {
-      db_query("UPDATE {system} SET description = '%s', name = '%s', bootstrap = %d, filename = '%s' WHERE filename = '%s'", $file->description, $file->name, $bootstrap, $file->filename, $file->old_filename);
-    }
-    else {
-      // This is a new module.
-      db_query("INSERT INTO {system} (name, description, type, filename, status, throttle, bootstrap) VALUES ('%s', '%s', '%s', '%s', %d, %d, %d)", $file->name, $file->description, 'module', $file->filename, $file->status, $file->throttle, $bootstrap);
-    }
   }
 
-
   // Handle status checkboxes, including overriding the generated
   // checkboxes for required modules.
   $form['status'] = array('#type' => 'checkboxes', '#default_value' => $status, '#options' => $options);
@@ -1080,35 +1056,33 @@ function theme_system_modules($form) {
 
 
 function system_modules_submit($form_id, $edit) {
-  db_query("UPDATE {system} SET status = 0, throttle = 0 WHERE type = 'module'");
-
   $new_modules = array();
   foreach ($edit['status'] as $key => $choice) {
     if ($choice) {
-      db_query("UPDATE {system} SET status = 1 WHERE type = 'module' AND name = '%s'", $key);
       if (!module_exist($key)) {
         $new_modules[] = $key;
       }
-    }
-  }
-
-  if (is_array($edit['throttle'])) {
-    foreach ($edit['throttle'] as $key => $choice) {
-      if ($choice) {
-        db_query("UPDATE {system} SET throttle = 1 WHERE type = 'module' and name = '%s'", $key);
+      else {
+        module_enable($key);
       }
     }
+    else {
+      module_disable($key);
+    }
   }
 
   module_list(TRUE, FALSE);
 
   include './includes/install.inc';
   foreach ($new_modules as $module) {
-    // Set the installed schema version for newly-enabled modules
-    $versions = drupal_get_schema_versions($module);
-    if (drupal_get_installed_schema_version($module) == SCHEMA_UNINSTALLED) {
-      drupal_set_installed_schema_version($module, $versions ? max($versions) : SCHEMA_INSTALLED);
-      module_invoke($module, 'install');
+    drupal_install_module($module);
+  }
+
+  if (is_array($edit['throttle'])) {
+    foreach ($edit['throttle'] as $key => $choice) {
+      if ($choice) {
+        db_query("UPDATE {system} SET throttle = 1 WHERE type = 'module' and name = '%s'", $key);
+      }
     }
   }
 
Index: modules/user/user.module
===================================================================
RCS file: /cvs/drupal/drupal/modules/user/user.module,v
retrieving revision 1.642
diff -u -F^f -r1.642 user.module
--- modules/user/user.module	29 Jul 2006 17:56:41 -0000	1.642
+++ modules/user/user.module	31 Jul 2006 03:29:14 -0000
@@ -1198,10 +1198,6 @@ function user_register_submit($form_id, 
     drupal_set_message(t('<p>Welcome to Drupal. You are user #1, which gives you full and immediate access. All future registrants will receive their passwords via e-mail, so please make sure your website e-mail address is set properly under the general settings on the <a href="%settings">site information settings page</a>.</p><p> Your password is <strong>%pass</strong>. You may change your password below.</p>', array('%pass' => $pass, '%settings' => url('admin/settings/site-information'))));
     user_authenticate($account->name, trim($pass));
 
-    // Set the installed schema version of the system module to the most recent version.
-    include_once './includes/install.inc';
-    drupal_set_installed_schema_version('system', max(drupal_get_schema_versions('system')));
-
     return 'user/1/edit';
   }
   else {
