#!/bin/sh

if ! [ -e /etc/default/grub ]; then
	echo "Error: No grub2 configuration present" 1>&2
	exit 1
fi

. /etc/default/grub

if [ "a$GRUB_DEFAULT" != "asaved" ]; then
	sed -i s/^GRUB_DEFAULT=.*$/GRUB_DEFAULT=saved/ /etc/default/grub
	update-grub
fi

WINDOWS="$(grep -i menuentry.*windows /boot/grub/grub.cfg | cut -f 2 -d \" | head -1)"
if [ "a$WINDOWS" = a ]; then
	echo "Error: No Windows entry found" 1>&2
	exit 2
fi
grub-set-default "$WINDOWS"

sed -i s/'${chosen}'/0/g /boot/grub/grub.cfg

reboot
