#!/bin/sh

# cat /etc/lilo.conf | grep label | awk -F= '{print $2}' | perl -p -e 's/\"//g' | perl pickline.pl

TYPE=$1
ST=' 	' # a space and a tab

case $TYPE in
	name)
		case $0 in 
			*lilo*) echo lilo ;;
			*yaboot*) echo yaboot ;;
		esac
		;;
	detect)
		CONFIG_LOCATIONS="/etc/"$($0 name)".conf"
		for i in $CONFIG_LOCATIONS; do
			if [ -e $i ]; then echo $i; exit 0; fi;
			exit 1;
		done;
		;;
	print_titles)
		CONF_FILE=`$0 detect`
		cat "$CONF_FILE" | grep -E 'label[[:space:]]*=' 	\
			| awk -F= '{print $2}' 	\
			| perl -p -e 's/[" ]//g'
		;;
	print_fallback)
		MENU_LST=`$0 detect`
		cat "$MENU_LST" \
			| grep -v '^#'	\
			| awk -v tt=$2 '/(image|other).*=/ {tn++} {if (tn==tt) {print $0;}}' \
			| perl -p -e 's/label\s*=.*/label=fallback/'
			
		;;
	print_autobench)
		MENU_LST=`$0 detect`
		if [ -z "$KERN_INSTALL_PATH" ]; then
			KERN_INSTALL_PATH=/boot
		fi

		# this will only pick up the append= if it is the  global one
		# and not one for an image
		#
		# the yaboot "append" image option doesn't append to the global
		# one, it replaces it.  
		APPEND="`egrep '(image|append).*=' $MENU_LST | head -1 \
				| grep append | cut -d= -f2- \
				| perl -p -e 's/\"//g'`"
		# welome to hell
		#
		# the greps strip off comments, then whitespace, then any 
		# "optional" commands.  We always add the optional part later
		# anyway, so take it out here
		#
		# the awk makes looks for the n'th LILO entry and prints it
		# if that entry didn't have an "append=" section, it will add it
		# it always adds an "optional"  
		#
		# the first perl is easy, the second one adds the "autobench_args:"
		# to the append= line.  The quotes are optional, so it's a little
		# harder.  We always add quotes, even if it didn't already have them
		# $1 == "append <whitespace> ="
		# then, absorb some whitespace, and the opening " if it's there
		# $2 == all of the actual arguments
		# $3 == ignored
		#
		# the last perl uses the configuration variable to point to the 
		# autobench kernels
		cat "$MENU_LST" \
			| grep -v '^#'	\
			| grep -v '^\s*$'	\
			| grep -v optional	\
			| awk -v tt=$2 '/(image|other).*=/ {tn++} 			\
					/append/ {if (tn==tt) {sawappend=1}}		\
					{if (tn==tt) {print $0;}} 			\
					END {if (sawappend!=1) {			\
						print "\tappend=\"'"$APPEND"'\""};	\
					     	print "\toptional"}'			\
			| perl -p -e 's/label.*=.*/label=autobench/'			\
			| perl -p -e 's/(append\s*=)\s*\"?([^"]*)(")?/$1"$2 autobench_args:"/'	\
			| perl -p -e 's#(image.*=).*#image='$KERN_INSTALL_PATH/vmlinuz-autobench'#'
		echo
		;;
esac;
