#!/usr/bin/env python

from gi.repository import Gtk, Gdk
import subprocess
import re

f = open("/usr/sbin/grub-install", "r").read()
grub_version = re.findall("PACKAGE_VERSION=(.*)", f)[0]

f = open("/boot/grub/grub.cfg", "r").read()
grub_menu_entries = re.findall("menuentry [\"'](.*)[\"']", f)

#for line in open("/boot/grub/grubenv", "r").xreadlines():
#	if re.search('^saved_entry', line):
#		saved_entry = line.split('=')[1]
#		print "saved_entry:", saved_entry
#
#	if re.search('^prev_saved_entry', line):
#		prev_saved_entry = line.split('=')[1]
#		print "prev_saved_entry:", prev_saved_entry
#
#	#if 

f = open("/boot/grub/grubenv", "r").read()
grub_default = re.findall("saved_entry=(.*)", f)[0]

f = open("/etc/default/grub", "r").read()
grub_timeout = re.findall("GRUB_TIMEOUT=(.*)", f)[0]

class GrubBootManager:
	def __init__(self):
		builder = Gtk.Builder()
		builder.add_from_file("grub-boot-manager.ui")

		self.window = builder.get_object("dialog_main")
		self.window.set_title("GRUB Boot Manager")
		self.window.connect("destroy", self.quit)
		#self.window.connect("key-press-event", self._keyhandler)
		self.window.show()

		self.label_version = builder.get_object("label_version")
		self.label_version.set_text(self.label_version.get_text() + grub_version)

		self.treeview = builder.get_object("treeview1")
		self.treeview.connect("row-activated", self.show_dialog_reboot)
		self.liststore = Gtk.ListStore(str)
		self.treeview.set_model(self.liststore)

		self.treeview_column = Gtk.TreeViewColumn("Menu Entry")
		self.treeview.append_column(self.treeview_column)

		self.cellrenderer = Gtk.CellRendererText()
		self.treeview_column.pack_start(self.cellrenderer, True)
		self.treeview_column.add_attribute(self.cellrenderer, "text", 0)

		for entry in grub_menu_entries:
			self.liststore.append([entry])

		self.treeselection = self.treeview.get_selection()
		self.treeselection.connect("changed", self.selection_changed)
		self.treeselection.select_path(grub_menu_entries.index(grub_default))

		self.entry_timeout = builder.get_object("entry_timeout")
		self.entry_timeout.set_text(str(grub_timeout))

		self.button_default = builder.get_object("button_default")
		self.button_default.connect("clicked", self.show_dialog_default)

		self.button_reboot = builder.get_object("button_reboot")
		self.button_reboot.connect("clicked", self.show_dialog_reboot)

		self.button_cancel = builder.get_object("button_cancel")
		self.button_cancel.connect("clicked", self.quit)

		# dialog_default
		self.dialog_default = builder.get_object("dialog_default")
		self.dialog_default.set_title("Default - Grub Boot Manager")
		self.dialog_default.connect("delete-event", self.hide_dialog_default)

		self.label_default = builder.get_object("label_default")

		self.button_cancel_default = builder.get_object("button_cancel_default")
		self.button_cancel_default.connect("clicked", self.hide_dialog_default)

		self.button_apply_default = builder.get_object("button_apply_default")
		self.button_apply_default.connect("clicked", self.grub_set_default)

		# dialog_reboot
		self.dialog_reboot = builder.get_object("dialog_reboot")
		self.dialog_reboot.set_title("Reboot - Grub Boot Manager")
		self.dialog_reboot.connect("delete-event", self.hide_dialog_reboot)

		self.label_reboot = builder.get_object("label_reboot")

		self.button_no_reboot = builder.get_object("button_no_reboot")
		self.button_no_reboot.connect("clicked", self.hide_dialog_reboot)

		self.button_yes_reboot = builder.get_object("button_yes_reboot")
		self.button_yes_reboot.connect("clicked", self.grub_reboot)

	#def _keyhandler(self, widget, event):
	#	keyname = Gdk.keyval_name(event.keyval)			
	#
	#	print "Key %s (%d) was pressed" % (keyname, event.keyval)
	#
	#	if Gdk.keyval_name(event.keyval) == 'e':
	#		self.show_dialog_edit()

	def selection_changed(self, tree_selection):
		(model, pathlist) = tree_selection.get_selected_rows()
		for path in pathlist:
			tree_iter = model.get_iter(path)
			self.grub_menu_entry = model.get_value(tree_iter, 0)

	def show_dialog_default(self, *args):
		self.label_default.set_text(self.grub_menu_entry)
		self.dialog_default.show()

	def hide_dialog_default(self, *args):
		self.dialog_default.hide()

		return True

	def grub_set_default(self, *args):
		subprocess.Popen(['echo', 'grub-set-default', self.grub_menu_entry])

		self.hide_dialog_default()

	def show_dialog_reboot(self, *args):
		self.label_reboot.set_text(self.grub_menu_entry)
		self.dialog_reboot.show()

	def hide_dialog_reboot(self, *args):
		self.dialog_reboot.hide()

		return True

	def grub_reboot(self, *args):
		subprocess.Popen(['echo', 'grub-reboot', self.grub_menu_entry])
		#subprocess.Popen(['echo', 'shutdown', '-r', 'now'])

		self.hide_dialog_reboot()

	def show_dialog_edit(self, *args):
		self.dialog_edit.show()

	def hide_dialog_edit(self, *args):
		self.dialog_edit.hide()

		return True

	def quit(self, *args):
		Gtk.main_quit()

if __name__ == "__main__":
	app = GrubBootManager()
	Gtk.main()


@@ -88,24 +88,34 @@
 f = 
-open(%22/usr/sbin/
+subprocess.check_output(%5B%22
 grub
@@ -122,33 +122,35 @@
 -install%22, %22
-r%22).read(
+--version%22%5D
 )%0Agrub_versi
@@ -158,47 +158,20 @@
 n = 
-re.findall(%22PACKAGE_VERSION=(.*)%22, f)%5B0
+f.split()%5B-1
 %5D%0A%0Af
@@ -259,22 +259,15 @@
 try 
-%5B%5C%22'%5D(.*)%5B%5C%22'%5D
+'(.*?)'
 %22, f

Fix menu entries