#!/bin/sh

if [ "$1" != "" ] && [ "$1" != "0" ] && [ "$1" != "2" ]; then
	echo "you might select a grub-target, boot partition is also mirrored"
	echo "boot partition is not switched by default,"
	echo "only default system partition will be changed"
	echo "one argument"
	echo "  0 for standard boot partition (/dev/hda1)"
	echo "  2 for mirrored boot partition (/dev/hda3)"
	if [ "$1" != "" ]; then
		echo "wrong argument, exiting"
		exit 1
	fi
fi
if [ "$1" == "" ]; then
	BOOTDEV="0"
else
	BOOTDEV="$1"
fi

mkdir -p /tmp/boot
mount /dev/hda$((BOOTDEV+1)) /tmp/boot
if [ -n "$(grep "default 0" /tmp/boot/boot/grub/menu.lst)" ]; then
	echo "switching from default boot system to spare system"
	NEWDEFAULT=2
else	echo "switching from spare boot system to default system"
	NEWDEFAULT=0
fi

awk '{if ($0 ~ "default") print "default '"$NEWDEFAULT"'"; else print}' /tmp/boot/boot/grub/menu.lst >/tmp/menu.lst
mv /tmp/menu.lst /tmp/boot/boot/grub/menu.lst

sync

umount /dev/hda$((BOOTDEV+1))
rmdir /tmp/boot

echo "menu.lst changed"
echo "starting grub..."

echo "grub-install for wrap"

grub --batch --no-curses --no-floppy <<EOF
device (hd0) /dev/hda
root (hd0,$BOOTDEV)
setup (hd0)
quit
EOF