--- /usr/bin/pygrub	2009-08-17 22:22:29.000000000 +0300
+++ pygrub	2009-08-19 18:25:01.000000000 +0300
@@ -21,6 +21,8 @@
 import curses, _curses, curses.wrapper, curses.textpad, curses.ascii
 import getopt
 
+sys.path = [ '/usr/lib/python' ] + sys.path
+
 import fsimage
 import grub.GrubConf
 import grub.LiloConf
@@ -122,7 +124,7 @@
 class GrubLineEditor(curses.textpad.Textbox):
     def __init__(self, screen, startx, starty, line = ""):
         screen.addstr(startx, starty, "> ")
-        screen.noutrefresh()
+        screen.refresh()
         win = curses.newwin(1, 74, startx, starty + 2)
         curses.textpad.Textbox.__init__(self, win)
         
@@ -135,7 +137,7 @@
         """Show the text.  One of our advantages over standard textboxes
         is that we can handle lines longer than the window."""
 
-        self.win.erase()
+        self.win.clear()
         p = self.pos
         off = 0
         while p > 70:
@@ -186,7 +188,6 @@
         return 1
 
     def edit(self):
-        curses.doupdate()
         r = curses.textpad.Textbox.edit(self)
         if self.cancelled:
             return None
@@ -216,15 +217,16 @@
             curses.def_prog_mode()
         
         curses.reset_prog_mode()
-        self.screen.erase()
+        self.screen.clear()
+        self.screen.refresh()
 
         # create basic grub screen with a box of entries and a textbox
         self.screen.addstr(1, 4, "pyGRUB  version %s" %(PYGRUB_VER,))
         self.entry_win.box()
-        self.screen.noutrefresh()
+        self.screen.refresh()
 
     def fill_entry_list(self):
-        self.entry_win.erase()
+        self.entry_win.clear()
         self.entry_win.box()
 
         maxy = self.entry_win.getmaxyx()[0]-3 # maxy - 2 for the frame + index
@@ -238,11 +240,11 @@
             if y > self.start_image + maxy:
                 break
             if y == self.selected_image:
-                self.entry_win.attron(curses.A_REVERSE)
-            self.entry_win.addstr(y + 1 - self.start_image, 2, i.title.ljust(70))
-            if y == self.selected_image:
-                self.entry_win.attroff(curses.A_REVERSE)
-        self.entry_win.noutrefresh()
+                attr = curses.A_REVERSE
+            else:
+                attr = 0
+            self.entry_win.addstr(y + 1 - self.start_image, 2, i.title.ljust(70), attr)
+        self.entry_win.refresh()
 
     def edit_entry(self, origimg):
         def draw():
@@ -257,29 +259,27 @@
             self.text_win.addch(0, 14, curses.ACS_DARROW)
             (y, x) = self.text_win.getmaxyx()
             self.text_win.move(y - 1, x - 1)
-            self.text_win.noutrefresh()
+            self.text_win.refresh()
 
         curline = 1
         img = copy.deepcopy(origimg)
         while 1:
             draw()
-            self.entry_win.erase()
+            self.entry_win.clear()
             self.entry_win.box()
             for idx in range(1, len(img.lines)):
                 # current line should be highlighted
+                attr = 0
                 if idx == curline:
-                    self.entry_win.attron(curses.A_REVERSE)
+                    attr = curses.A_REVERSE
 
                 # trim the line
                 l = img.lines[idx].ljust(70)
                 if len(l) > 70:
                     l = l[:69] + ">"
                     
-                self.entry_win.addstr(idx, 2, l)
-                if idx == curline:
-                    self.entry_win.attroff(curses.A_REVERSE)
-            self.entry_win.noutrefresh()
-            curses.doupdate()
+                self.entry_win.addstr(idx, 2, l, attr)
+            self.entry_win.refresh()
 
             c = self.screen.getch()
             if c in (ord('q'), 27): # 27 == esc
@@ -314,17 +314,13 @@
                 curline = len(img.lines) - 1
 
         if self.isdone:
-           # Fix to allow pygrub command-line editing in Lilo bootloader (used by IA64)
-           if platform.machine() == 'ia64':
-              origimg.reset(img.lines, img.path)
-           else:
-              origimg.reset(img.lines)
+            origimg.reset(img.lines)
 
     def edit_line(self, line):
-        self.screen.erase()
+        self.screen.clear()
         self.screen.addstr(1, 2, "[ Minimal BASH-like line editing is supported.  ")
         self.screen.addstr(2, 2, "  ESC at any time cancels.  ENTER at any time accepts your changes. ]")
-        self.screen.noutrefresh()
+        self.screen.refresh()
 
         t = GrubLineEditor(self.screen, 5, 2, line)
         enable_cursor(True)
@@ -334,10 +330,10 @@
         return None
 
     def command_line_mode(self):
-        self.screen.erase()
+        self.screen.clear()
         self.screen.addstr(1, 2, "[ Minimal BASH-like line editing is supported.  ESC at any time ")
         self.screen.addstr(2, 2, "  exits.  Typing 'boot' will boot with your entered commands. ] ")
-        self.screen.noutrefresh()
+        self.screen.refresh()
 
         y = 5
         lines = []
@@ -380,8 +376,8 @@
             file_list += ("/efi/boot/elilo.conf", "/elilo.conf",)
         else:
             self.cf = grub.GrubConf.GrubConfigFile()
-            file_list = ("/boot/grub/menu.lst", "/boot/grub/grub.conf",
-                         "/grub/menu.lst", "/grub/grub.conf")
+            file_list = ("/boot/grub/grub.conf", "/boot/grub/menu.lst",
+                         "/grub/grub.conf", "/grub/menu.lst")
 
         if not fs:
             # set the config file and parse it
@@ -423,7 +419,7 @@
             self.text_win.addch(0, 14, curses.ACS_DARROW)
             (y, x) = self.text_win.getmaxyx()
             self.text_win.move(y - 1, x - 1)
-            self.text_win.noutrefresh()
+            self.text_win.refresh()
 
         # now loop until we hit the timeout or get a go from the user
         mytime = 0
@@ -436,18 +432,13 @@
             else:
                 self.screen.addstr(20, 5, " " * 80)
             self.fill_entry_list()
-            curses.doupdate()
 
             c = self.screen.getch()
             if c == -1:
                 # Timed out waiting for a keypress
                 if mytime != -1:
                     mytime += 1
-                    # curses.timeout() does not work properly on Solaris
-                    # So we may come here even after a key has been pressed.
-                    # Check both timeout and mytime to avoid exiting
-                    # when we shouldn't.
-                    if timeout != -1 and mytime >= int(timeout):
+                    if mytime >= int(timeout):
                         self.isdone = True
                         break
             else:
@@ -491,6 +482,9 @@
                 self.selected_image = 0
             elif self.selected_image >= len(self.cf.images):
                 self.selected_image = len(self.cf.images) - 1
+
+        if (mytime >= int(timeout)):
+            self.isdone = True
         
 def get_entry_idx(cf, entry):
     # first, see if the given entry is numeric
@@ -507,7 +501,7 @@
 
     return None
 
-def run_grub(file, entry, fs, arg):
+def run_grub(file, entry, fs):
     global g
     global sel
 
@@ -532,11 +526,7 @@
         print "No kernel image selected!"
         sys.exit(1)
 
-    try:
-        img = g.cf.images[sel]
-    except IndexError:
-        log.debug("PyGrub: Default selection is not valid, using first boot configuration...")
-        img = g.cf.images[0]
+    img = g.cf.images[sel]
 
     grubcfg = { "kernel": None, "ramdisk": None, "args": None }
 
@@ -544,7 +534,7 @@
     if img.initrd:
         grubcfg["ramdisk"] = img.initrd[1]
     if img.args:
-        grubcfg["args"] = img.args + " " + arg
+        grubcfg["args"] = img.args
 
     return grubcfg
 
@@ -589,15 +579,6 @@
 
     return cfg
  
-def sniff_netware(fs, cfg):
-    if not fs.file_exists("/nwserver/xnloader.sys"):
-        return cfg
-
-    if not cfg["kernel"]:
-        cfg["kernel"] = "/nwserver/xnloader.sys"
-
-    return cfg
-
 if __name__ == "__main__":
     sel = None
     
@@ -624,7 +605,7 @@
     isconfig = False
 
     # what was passed in
-    incfg = { "kernel": None, "ramdisk": None, "args": "" }
+    incfg = { "kernel": None, "ramdisk": None, "args": None }
     # what grub or sniffing chose
     chosencfg = { "kernel": None, "ramdisk": None, "args": None }
     # what to boot
@@ -660,39 +641,30 @@
 
     # debug
     if isconfig:
-        chosencfg = run_grub(file, entry, fs, incfg["args"])
+        chosencfg = run_grub(file, entry)
         print "  kernel: %s" % chosencfg["kernel"]
         if img.initrd:
             print "  initrd: %s" % chosencfg["ramdisk"]
         print "  args: %s" % chosencfg["args"]
         sys.exit(0)
 
-    # if boot filesystem is set then pass to fsimage.open
-    bootfsargs = '"%s"' % incfg["args"]
-    bootfsgroup = re.findall('zfs-bootfs=(.*?)[\s\,\"]', bootfsargs)
-    if bootfsgroup:
-        fs = fsimage.open(file, get_fs_offset(file), bootfsgroup[0])
-    else:
-        fs = fsimage.open(file, get_fs_offset(file))
+    fs = fsimage.open(file, get_fs_offset(file))
 
     chosencfg = sniff_solaris(fs, incfg)
 
     if not chosencfg["kernel"]:
-        chosencfg = sniff_netware(fs, incfg)
-
-    if not chosencfg["kernel"]:
-        chosencfg = run_grub(file, entry, fs, incfg["args"])
+        chosencfg = run_grub(file, entry, fs)
 
     data = fs.open_file(chosencfg["kernel"]).read()
     (tfd, bootcfg["kernel"]) = tempfile.mkstemp(prefix="boot_kernel.",
-        dir="/var/run/xend/boot")
+        dir="/var/lib/xen")
     os.write(tfd, data)
     os.close(tfd)
 
     if chosencfg["ramdisk"]:
         data = fs.open_file(chosencfg["ramdisk"],).read()
         (tfd, bootcfg["ramdisk"]) = tempfile.mkstemp(prefix="boot_ramdisk.",
-            dir="/var/run/xend/boot")
+            dir="/var/lib/xen")
         os.write(tfd, data)
         os.close(tfd)
     else:
@@ -702,15 +674,7 @@
     if bootcfg["ramdisk"]:
         sxp += "(ramdisk %s)" % bootcfg["ramdisk"]
     if chosencfg["args"]:
-        zfsinfo = fsimage.getbootstring(fs)
-        if zfsinfo is None:
-            sxp += "(args \"%s\")" % chosencfg["args"]
-        else:
-            e = re.compile("zfs-bootfs=[\w\-\.\:@/]+" )
-            (chosencfg["args"],count) = e.subn(zfsinfo, chosencfg["args"])
-            if count == 0:
-               chosencfg["args"] += " -B %s" % zfsinfo
-            sxp += "(args \"%s\")" % (chosencfg["args"])
+        sxp += "(args \"%s\")" % chosencfg["args"]
 
     sys.stdout.flush()
     os.write(fd, sxp)
