--- pilgrim.py.prepatch-kernelversion	2007-08-05 19:51:53.000000000 +0200
+++ pilgrim.py	2007-08-05 20:20:38.000000000 +0200
@@ -301,7 +301,7 @@
         return self.runTransaction(cb)
 
 class InstallationTarget:
-    def __init__(self, repos, packages, epackages, groups, fs_label, skip_compression, skip_prelink, tmpdir):
+    def __init__(self, repos, packages, epackages, groups, fs_label, skip_compression, skip_prelink, tmpdir, bootloader_timeout, bootloader_options, preferred_kernel):
         self.ayum = None
         self.repos = repos
         self.packages = packages
@@ -312,6 +312,9 @@
         self.skip_prelink = skip_prelink
         self.build_dir = None
         self.tmpdir = tmpdir
+        self.bootloader_timeout = bootloader_timeout
+        self.bootloader_options = bootloader_options
+        self.preferred_kernel = preferred_kernel
 
         self.build_dir = None
         self.instloop = None
@@ -794,16 +797,65 @@
         # FIXME: this doesn't handle multiple kernels - we should list
         #        them all in the isolinux menu
         #
+        self.kernel_types = [
+                                         {
+                                           "regexp": ".*PAE-debug$",
+                                           "initrd": "initpaed.img",
+                                           "vmlinuz": "vmlpaed",
+                                           "type": "PAE-debug"
+                                         },
+
+                                         {
+                                           "regexp": ".*PAE$",
+                                           "initrd": "initpae.img",
+                                           "vmlinuz": "vmlpae",
+                                           "type": "PAE"
+                                         },
+
+                                         {
+                                            "regexp": ".*xen$",
+                                            "initrd": "initxen.img",
+                                            "vmlinuz": "vmlxen",
+                                            "type": "xen"
+                                         },
+
+                                         {
+                                            "regexp": ".*debug$",
+                                            "initrd": "initdbg.img",
+                                            "vmlinuz": "vmldebug",
+                                            "type": "debug"
+                                         },
+
+                                         {
+                                            "regexp": "^.*$",
+                                            "initrd": "initrd.img",
+                                            "vmlinuz": "vmlinuz",
+                                            "type": "normal"
+                                         }
+                            ]
+
+        kernel_dirs = []
         kernels = []
         modules_dir = "%s/install_root/lib/modules" % self.build_dir
 
         if os.path.isdir(modules_dir):
-            kernels = os.listdir(modules_dir)
+            kernel_dirs = os.listdir(modules_dir)
+
+        kernel_dirs.sort()
 
-        if not kernels:
+        if not kernel_dirs:
             raise InstallationError(_("No kernels installed: /lib/modules is empty"))
 
-        return kernels[0]
+        for kernel_dir in kernel_dirs:
+            found = False
+            for kernel_type in self.kernel_types:
+                if not found:
+                    if re.match(kernel_type["regexp"], kernel_dir):
+                        kernels.append({"name": kernel_dir, "initrd": kernel_type["initrd"], "vmlinuz": kernel_type["vmlinuz"], "type": kernel_type["type"] })
+                        found = True
+
+        self.log.debug("Kernels: %s" % str(kernels))
+        return kernels
 
     def createInitramfs(self):
         # Create initramfs
@@ -822,9 +874,11 @@
         mayflowerconf.write('MODULES+="sym53c8xx aic7xxx "\n')
         mayflowerconf.close()
 
-        subprocess.call(["/sbin/mayflower", "-f", "/boot/livecd-initramfs.img",
-                        self.get_kernel_version()],
-                        preexec_fn=self.run_in_root)
+        for kernel in self.get_kernel_version():
+            subprocess.call(["/sbin/mayflower", "-f", "/boot/livecd-initramfs-%s.img" % kernel["name"],
+                            kernel["name"]],
+                            preexec_fn=self.run_in_root)
+
         for f in ("/sbin/mayflower", "/etc/mayflower.conf"):
             os.unlink("%s/install_root/%s" %(self.build_dir, f))
 
@@ -857,6 +911,120 @@
         #  - fix for non-i386
         #  - error handling
         #
+
+        # Handle multiple kernels
+        for kernel in self.get_kernel_version():
+            # Copy appr. vmlinuz in place
+            shutil.copyfile("%s/install_root/boot/vmlinuz-%s"
+                            %(self.build_dir, kernel["name"]),
+                            "%s/out/isolinux/%s" %(self.build_dir, kernel["vmlinuz"]))
+
+            # Copy appr. initrd in place
+            shutil.copyfile("%s/install_root/boot/livecd-initramfs-%s.img"
+                            %(self.build_dir,kernel["name"]),
+                            "%s/out/isolinux/%s" %(self.build_dir,kernel["initrd"]))
+
+            # Lose whatever was there
+            os.unlink("%s/install_root/boot/livecd-initramfs-%s.img"
+                    %(self.build_dir,kernel["name"]))
+
+        syslinuxfiles = ["isolinux.bin"]
+        menus = ["vesamenu.c32", "menu.c32"]
+        syslinuxMenu = None
+
+        for m in menus:
+            path = "%s/install_root/usr/lib/syslinux/%s" % (self.build_dir, m)
+            if os.path.isfile(path):
+                syslinuxfiles.append(m)
+                syslinuxMenu=m
+                break
+        if syslinuxMenu is None:
+            raise InstallationError(_("syslinux not installed : no suitable *menu.c32 found"))
+
+        for p in syslinuxfiles:
+            path = "%s/install_root/usr/lib/syslinux/%s" % (self.build_dir, p)
+            if not os.path.isfile(path):
+                raise InstallationError(_("syslinux not installed : %s not found") % path)
+
+            shutil.copy(path, "%s/out/isolinux/%s" % (self.build_dir, p))
+
+        if os.path.exists("%s/install_root/usr/lib/anaconda-runtime/syslinux-vesa-splash.jpg" %(self.build_dir,)):
+            shutil.copy("%s/install_root/usr/lib/anaconda-runtime/syslinux-vesa-splash.jpg" %(self.build_dir,),
+                        "%s/out/isolinux/splash.jpg" %(self.build_dir,))
+            have_background = "menu background splash.jpg"
+        else:
+            have_background = ""
+
+        cfg_template = """
+default %(menu)s
+timeout %(bootloader_timeout)s
+%(bootloader_isolinux_extras)s
+
+%(background)s
+menu title """ + _("Welcome to %(label)s!") + """
+menu color border 0 #ffffffff #00000000
+menu color sel 7 #ffffffff #ff000000
+menu color title 0 #ffffffff #00000000
+menu color tabmsg 0 #ffffffff #00000000
+menu color unsel 0 #ffffffff #00000000
+menu color hotsel 0 #ff000000 #ffffffff
+menu color hotkey 7 #ffffffff #ff000000
+"""
+
+        cfg = cfg_template % {"menu" : syslinuxMenu, "background" : have_background, "label" : self.fs_label,
+                              "bootloader_timeout" : self.bootloader_timeout,
+                              "bootloader_isolinux_extras" : self.bootloader_isolinux_extras }
+
+# FIXME
+# Change initrd.img to the "default" kernel, which at this moment we don't have
+
+        for kernel in self.get_kernel_version():
+            # Match against self.preferred_kernel_type
+            if kernel["type"] == self.preferred_kernel_type:
+                preferred_kernel = kernel
+
+        # short, long, extra, kernel_initrd, kernel_vmlinuz
+        stanzas = [("linux", _("Run from image"), self.bootloader_options, "%s" % preferred_kernel["initrd"], "%s" % preferred_kernel["vmlinuz"]),
+                   ("runfromram", _("Run from RAM - requires 1 GB+"), self.bootloader_options+" live_ram", "%s" % preferred_kernel["initrd"], "%s" % preferred_kernel["vmlinuz"])]
+
+        # List all kernels
+        for kernel in self.get_kernel_version():
+            if not kernel["type"] == self.cfg.lm_preferred_kernel_type:
+                stanzas.append( ("linux-%s" % kernel["name"], _("Run kernel %s") % kernel["name"], self.bootloader_options, "%s" % kernel["initrd"], "%s" % kernel["vmlinuz"]) )
+
+        if os.path.exists("%s/install_root/usr/lib/anaconda-runtime/checkisomd5" %(self.build_dir,)):
+            stanzas.append( ("check", _("Verify and run from image"), "check", "", "") )
+
+        bootcfg = ""
+
+        for (short, long, extra, kernel_initrd, kernel_vmlinuz) in stanzas:
+            bootcfg += """label %(short)s
+  menu label %(long)s
+  kernel %(kernel_vmlinuz)s
+  append initrd=%(kernel_initrd)s ro quiet root=CDLABEL=%(label)s rootfstype=iso9660 liveimg %(extra)s
+"""
+
+        bootcfg = bootcfg % {"label": self.fs_label, "background" : have_background, "short": short, "long": long, "extra": extra, "kernel_vmlinuz": kernel_vmlinuz, "kernel_initrd": kernel_initrd }
+
+        memtest = glob.glob("%s/install_root/boot/memtest86*" %(self.build_dir,))
+        if len(memtest) > 0:
+            shutil.copy(memtest[0], "%s/out/isolinux/memtest" %(self.build_dir,))
+            bootcfg += """label memtest
+  menu label """ + _("Memory Test") + """
+  kernel memtest"""
+
+        finalcfg = cfg + bootcfg
+
+        cfgf = open("%s/out/isolinux/isolinux.cfg" %(self.build_dir,), "w")
+        cfgf.write(finalcfg)
+        cfgf.close()
+
+
+        ##### ORIGINAL CODE
+        #####
+        #####
+        #####
+        #####
         shutil.copyfile("%s/install_root/boot/vmlinuz-%s"
                         %(self.build_dir, self.get_kernel_version()),
                         "%s/out/isolinux/vmlinuz" %(self.build_dir,))
@@ -1067,6 +1235,9 @@
                       [--builddir]
                       [--installroot]
                       [--yumcache]
+                      [--bootloader_timeout]
+                      [--bootloader_options]
+                      [--preferred_kernel]
                       [--tmpdir=<tmpdir>]
 
  --help              : """ + _("Print usage and exit") + """
@@ -1083,6 +1254,9 @@
  --builddir          : """ + _("Use this build directory") + """
  --installroot       : """ + _("Use this directory as installroot") + """
  --yumcache          : """ + _("Use this yum cache directory") + """
+ --bootloader_timeout: """ + _("Set the bootloader timeout (default: 3)") + """
+ --bootloader_options: """ + _("Set additional bootloader options") + """
+ --preferred_kernel  : """ + _("Set the preffered kernel (default: normal)") + """
  --shell             : """ + _("Start a shell in the chroot for post-configuration") + """
  --tmpdir            : """ + _("Temporary directory to use (default: /var/tmp)") + """
 
@@ -1118,6 +1292,10 @@
         self.build_dir = None
         self.install_root = None
         self.yum_cache = None
+        self.bootloader_timeout = "3"
+        self.bootloader_options = ""
+        self.preferred_kernel = "normal"
+        self.bootloader_isolinux_extras = ""
         self.tmpdir = "/var/tmp"
 
 def parse_options(args):
@@ -1128,6 +1306,8 @@
                                     "skip-compression", "uncompressed-size=",
                                     "ignore-deleted", "shell", "no-prelink",
                                     "build_dir=", "install_root=", "yum_cache",
+                                    "bootloader_timeout", "bootloader_options",
+                                    "preferred_kernel",
                                     "prelink", "tmpdir="])
 
     except getopt.GetoptError, msg:
@@ -1199,6 +1379,15 @@
         if o in ("-y", "--yumcache"):
             options.yum_cache = a
             continue
+        if o in ("--bootloader_timeout"):
+            options.bootloader_timeout = str(int(a))
+            continue
+        if o in ("--bootloader_options"):
+            options.bootloader_options = a
+            continue
+        if o in ("--preferred_kernel"):
+            options.preferred_kernel = a
+            continue
         raise Usage(_("Unknown option %s") % o)
 
     if not options.kscfg and not (options.packages or options.groups):
@@ -1235,7 +1424,11 @@
                                 options.fs_label,
                                 options.skip_compression,
                                 options.skip_prelink,
-                                options.tmpdir)
+                                options.tmpdir,
+                                options.bootloader_timeout,
+                                options.bootloader_options,
+                                options.preferred_kernel,
+                                options.bootloader_isolinux_extras)
 
     try:
         target.parse(options.kscfg)
