#!/bin/sh

CONFIG_LOCATIONS="/boot/grub/menu.lst /etc/grub.conf"
TYPE=$1

case $TYPE in
	name)
		echo grub;
		;;
	detect)
		for i in $CONFIG_LOCATIONS; do
			if [ -e $i ]; then echo $i; exit 0; fi;
			exit 1;
		done;
		;;
	print_titles)
		MENU_LST=`$0 detect`
		cat "$MENU_LST" | grep title
		;;
	print_autobench)
		MENU_LST=`$0 detect`
		ENTRY_NUM=$1;
		if [ -z "$ENTRY_NUM" ];  then
			echo entry number required;
			exit 1;
		fi
		# first grep is to strip off comments, the second if for whitespace lines,
		# and the third is beacuse we don't support initrd's yet
		# 
		# the awk make sure that we only print out the requested menu entry
		#
		# the first perl changes the title
		#
		# the second one looks for the kernel line, This line is made up of 
		# an optional device: "(\(.*\))?" (like (hd0,0)).  This is stored in $2.
		# This is followed by a path name which is a series of things ending in /, 
		# and represents the path to the /boot directory, usually.  It is stored in $3
		# finally come the command-line arguments in $4 
		cat "$MENU_LST" \
			| grep -v '^#'		\
			| grep -v '^\s*$'	\
			| grep -v 'initrd'	\
			| awk -v tt=$2 '/title/ {tn++} {if (tn==tt) {print $0;}}'	\
			| perl -p -e 's/title.*/title autobench/'			\
			| perl -p -e 's/kernel (--.* )*(\(.*\))?(\S*\/)+\S* (.*)/kernel $1$2$3vmlinuz-autobench $4 autobench_args:/'
		echo
		;;
	print_fallback)
		MENU_LST=`$0 detect`
		cat "$MENU_LST" \
			| grep -v '^#'		\
			| grep -v '^\s*$'	\
			| grep -v 'initrd'	\
			| awk -v tt=$2 '/title/ {tn++} {if (tn==tt) {print $0;}}'	\
			| perl -p -e 's/title.*/title fallback/'
		echo
		;;
esac;
