#! /bin/bash
#
# GRUB OS Selector 1.2
#
# History:
#	1.2	Use Perl rather than a set of shell hacks and a temporary file
#		to parse the GRUB option
#	1.1	Proceed to logout, reboot or halt
#	1.0	First version
#
# Copyright (c) 2008 Xavier 'Xr' Dalem
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The name of the author may not be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


# Set the logout handler for your window manager.
# If you don't have one, you can set it to the empty string and pass a parameter
# of 'halt', 'reboot', or 'prompt' (not recommended but better than nothing)
logout_handler='xfce4-session-logout'

# Set the sudo handler, used for privileged actions. Set to the empty string
# for automatic detection
sudo=''

# It's not necessary to edit anything below this point

# Set sudo helper
if [ -z "$sudo" ]; then
	if [ -x /usr/bin/gksudo ]; then
		sudo='gksudo'
	elif [ -x /usr/bin/sudo ]; then
		sudo='sudo'
	else
		zenity --error --text 'Cannot find a sudo program'
		exit
	fi
fi

# Find list of available systems
if ! grep -E '^default[[:space:]]+saved$' /boot/grub/menu.lst > /dev/null; then
	zenity --error --text 'Default GRUB action is not to load saved option'
	exit
fi

# Detect and show a list of available systems, selecting the current default
export grub_default_option=`head -n 1 /boot/grub/default`
zenity_columns=$(perl -lwne 'BEGIN {$d = $ENV{"grub_default_option"}; $i = 0; $options = ""; } {
	$options = $options.($i == $d?" TRUE ":" FALSE ")."\"$_\" ".$i++ if (s/^title\s+//);
	} END { print $options } ' < /boot/grub/menu.lst)

selected=`eval "zenity --list --radiolist --width 500 --height 200 --title 'Reboot selection' --text 'Select OS to boot' --column '' --column 'OS' --column index --print-column 3 --hide-column 3 $zenity_columns"`

# Change the default OS
if [ -n "$selected" ]; then
	$sudo grub-set-default $selected
else
	exit
fi

# Proceed to logout
if [ -n "$logout_handler" ]; then
	$logout_handler
else
	if [ "$1" = 'reboot' ]; then
		action=reboot
	elif [ "$1" = 'halt' ]; then
		action=halt
	elif [ "$1" = 'prompt' ]; then
		action=`zenity --list --radiolist --title 'Action' --text 'What to do now:' --column '' --column 'Action' TRUE halt FALSE reboot FALSE 'Do nothing'`
	fi

	if [ "$action" = "reboot" ] || [ "$action" = "halt" ]; then
		$sudo /sbin/$action
	fi
fi


