=== modified file 'modules/system/system.install'
--- modules/system/system.install	2007-05-27 20:31:13 +0000
+++ modules/system/system.install	2007-05-29 13:27:17 +0000
@@ -3287,23 +3287,89 @@ function system_update_6019() {
 
 function system_update_6020() {
   $ret = array();
-
+  // We should die a horrible death here if this query fails.
+  $menu = unserialize(db_result(db_query("SELECT data FROM {cache_menu} WHERE cid LIKE '1:%%'")));
   $schema['menu_router'] = drupal_get_schema_unprocessed('system', 'menu_router');
   $schema['menu_links'] = drupal_get_schema_unprocessed('system', 'menu_links');
   _drupal_initialize_schema('system', $schema);
   $ret = array();
   foreach ($schema as $table) {
     db_create_table($ret, $table);
+
+  }
+  $new_menu = menu_router_build(TRUE);
+  if ($menu && module_exists('menu')) {
+    $items = $menu['items'];
+    drupal_install_schema('menu');
+    $menu_names = array();
+    $system_menus = array(
+      1 => 'navigation',
+      variable_get('menu_primary_menu', 2) => 'primary-links',
+      variable_get('menu_secondary_menu', 0) => 'secondary-links',
+    );
+    // These are the root menus.
+    foreach ($items[0]['children'] as $mid) {
+      $item = $items[$mid];
+      if (isset($system_menus[$mid])) {
+        $menu_name = $system_menus[$mid];
+      }
+      else {
+        $title = $item['title'];
+        $menu_name = preg_replace('[^a-z0-9]', '-', $title);
+        if (isset($menu_names[$menu_name])) {
+          $counter = 0;
+          while (isset($menu_names[$menu_name . ++$counter]));
+          $menu_name = $menu_name . $counter;
+        }
+        $description = db_result(db_query('SELECT description FROM {menu} WHERE mid = %d', $mid));
+        db_query("INSERT INTO {menu_custom} (menu_name, title, description) VALUES ('%s', '%s', '%s')", $menu_name, $title, $description);
+      }
+      $menu_names[$menu_name] = $mid;
+      if (isset($item['children'])) {
+        foreach ($item['children'] as $child_mid) {
+          _system_update_6020($items, $menu_name, $child_mid);
+        }
+      }
+      menu_cache_clear($menu_name);
+    }
+    array_flip($menu_names);
+    $menu_parent_items = variable_get('menu_parent_items', NULL);
+    if (isset($menu_parent_items)) {
+      if ($menu_parent_items) {
+        variable_set('menu_parent_items', $menu_names[$menu_parent_items]);
+      }
+      else {
+        drupal_set_message(t('The menu name for on-the-fly creation of menu links has been set the navigation'), 'warning');
+        variable_set('menu_parent_items', 'navigation');
+      }
+    }
   }
+  _menu_navigation_links_rebuild($new_menu);
+
   return $ret;
 }
 
-function system_update_6021() {
-  $ret = array();
-  // TODO - menu module updates. These need to happen before we do the menu_rebuild
-
-  menu_rebuild();
-  return $ret;
+function _system_update_6020($items, $menu_name, $mid) {
+  static $map;
+  $item = $items[$mid];
+  if ($item['type'] & MENU_VISIBLE_IN_BREADCRUMB) {
+    $item = _menu_link_build($item);
+    $item['menu_name'] = $menu_name;
+    $item['plid'] = isset($map[$item['pid']]) ? $map[$item['pid']] : 0;
+    if ($mid > 0) {
+      $item['description'] = db_result(db_query('SELECT description FROM {menu} WHERE mid = %d', $mid));
+      if ($item['type'] & MENU_CREATED_BY_ADMIN) {
+        $item['module'] = 'menu';
+      }
+    }
+    $item['expanded'] = (bool)($item['type'] & 0x0100);
+    $map[$mid] = menu_link_save($item);
+  }
+  if (isset($item['children'])) {
+    foreach ($item['children'] as $child_mid) {
+      _system_update_6020($items, $menu_name, $child_mid);
+    }
+  }
 }
 
 /**

