#!/bin/sh

. /etc/autobench.conf || . functions

function print_with_number () {
				local COUNT=1
				# There's two ways to call this, 1) with each entry as a differnt arg
				# or, all in the first arg, \n seperated
				if [ $# -eq 1 ]; then
								eval print_with_number ALREADY_QUOTED_THE_ARGS `echo "$1" | \
												sed -e "s/'/'\\\\\\\\''/" -e "s/^/'/" -e "s/$/'/"`
								return
				elif [ $# -gt 1 -a "$1" = "ALREADY_QUOTED_THE_ARGS" ]; then
								shift
				fi
				while [ $# -gt 0 ]; do
								echo "($COUNT) $1"
								COUNT=$(($COUNT+1))
								shift
				done
}

function my_clear () {
	sleep 1
	clear
}

PATH=$PATH:.
BL_TYPES="grub-manage lilo-manage yaboot-manage"

for i in $BL_TYPES; do
	echo looking for bootloader `$i name`...
	CFG_FILE=`$i detect`;
		if [ -n "$CFG_FILE" ]; then
				NAME=`$i name`;
		echo "detected $NAME @ '$CFG_FILE'"
				LOADERS[${#LOADERS[*]}]="$NAME"
				CONFIG_FILES[${#CONFIG_FILES[*]}]="$CFG_FILE"
		fi
done

if [ ${#LOADERS[*]} -gt 1 ]; then
	echo 
	echo 
	echo "Multiple bootloaders detected..."
	echo "Which would you like to use:"
		echo "(0) Quit: None of the below"
		print_with_number "${LOADERS[@]}"
	read CHOICE
	if [ -n "$CHOICE" -a "$CHOICE" -gt 0 ]; then
		CHOICE=$(($CHOICE-1))
		CONFIG_FILE=${CONFIG_FILES[$CHOICE]}
		LOADER="${LOADERS[$CHOICE]}"
		SCRIPT="${LOADER}-manage"
	fi
elif [ ${#LOADERS[*]} -eq 1 ]; then
	CONFIG_FILE=${CONFIG_FILES[0]}
	LOADER="${LOADERS[0]}"
	SCRIPT="${LOADER}-manage"
fi

if [ -z "$CONFIG_FILE" ]; then
	echo
	echo ERROR: no bootloader detected.
	echo ERROR: you will need to manually configure the autobench bootloader
	exit 1;
fi;

echo "Would you like to include your bootloader setting in your autobench.conf"
echo -n "file? (yes)"

read CONF
if [ "${CONF::1}" != "n" ]; then
	cat /etc/autobench.conf | \
		sed -e "s/^.*\(DATS_CONFIG_BOOTLOADER=\).*$/\1$LOADER/" \
		> /etc/autobench.conf.new
	rm /etc/autobench.conf
	mv /etc/autobench.conf.new /etc/autobench.conf
fi

ADD=$1
shift
if grep -q autobench_args: $CONFIG_FILE; then
	echo 
	echo It appears that you already have autobench entries
	echo in your bootloader configuration file.
	echo
	echo -n "add more entries? (no/yes)"
	
	read ADD
	if [ "${ADD::1}" != "y"	]; then 
		exit 0;
	fi;
fi;

my_clear
echo
echo `$SCRIPT name` bootloader detected
echo
echo "Here is a list of your bootloader's current choices."
echo "which would you like to use as a template for the new"
echo "autobench entries?"
echo
echo "(0) Quit: don't touch anything"
print_with_number "`$SCRIPT print_titles`"
echo -n "default (1):" 
read TITLENUM
if [ "$TITLENUM" == "0" ]; then exit 0; fi;
if [ -z "$TITLENUM" ]; then TITLENUM=1; fi;

TMPFILE=/tmp/bootloader.tmp.$$
echo > $TMPFILE
echo '# auto generated by autobench client installer' >> $TMPFILE
$SCRIPT print_fallback $TITLENUM >> $TMPFILE
$SCRIPT print_autobench $TITLENUM >> $TMPFILE

while true; do
	my_clear
	echo here are the new entries:
	echo ---------------------------------------------
	cat $TMPFILE
	echo ---------------------------------------------
	echo
	echo would you like to add these to your bootloader
	echo -n "configuration file? (yes/no/edit)"
	read EDIT
	if [ "${EDIT::1}" == "y"	] || [ -z "$EDIT" ]; then
		cat $TMPFILE >> $CONFIG_FILE
		exit 0;
	elif [ "${EDIT::1}" == "n"	]; then
		exit 0;
	elif [ "${EDIT::1}" == "e"	] || [ "${EDIT::1}" == "n"	]; then
		vi $TMPFILE;
	fi
done


