#!/usr/bin/env bash

. /usr/share/dosis2/bin/shell-support-functions

OS="`getos`"

if [ "${OS}" = "Ubuntu" ]; then
	# check if GRUB_GFXMODE exists, if it does replace it with the correct value
	if grep -q '^GRUB_GFXMODE=.*$' /etc/default/grub; then
		sed -i -re 's/^GRUB_GFXMODE=.*$/GRUB_GFXMODE=800x600/g' /etc/default/grub
	# check if GRUB_GFXMODE exists but is commented out by default replace it with the uncommented correct value
	elif grep -q '^#GRUB_GFXMODE=.*$' /etc/default/grub; then
		sed -i -re 's/^#GRUB_GFXMODE=.*$/GRUB_GFXMODE=800x600/g' /etc/default/grub
	# otherwise append to the file
	else
		echo "GRUB_GFXMODE=800x600" >> /etc/default/grub
	fi
	# check if GRUB_GFXPAYLOAD_LINUX exists, if it does replace it with the correct value
	if grep -q '^GRUB_GFXPAYLOAD_LINUX=.*$' /etc/default/grub; then
		sed -i -re 's/^GRUB_GFXPAYLOAD_LINUX=.*$/GRUB_GFXPAYLOAD_LINUX=keep/g' /etc/default/grub
	# check if GRUB_GFXPAYLOAD_LINUX exists but is commented out by default replace it with the uncommented correct value
	elif grep -q '^#GRUB_GFXPAYLOAD_LINUX=.*$' /etc/default/grub; then
		sed -i -re 's/^#GRUB_GFXPAYLOAD_LINUX=.*$/GRUB_GFXPAYLOAD_LINUX=keep/g' /etc/default/grub
	# otherwise append to the file
	else
		echo "GRUB_GFXPAYLOAD_LINUX=keep" >> /etc/default/grub
	fi

	update-grub
elif [ "${OS}" = "CentOS" ]; then
	# dont touch the /boot/grub/grub.conf file for now unless necessary
	exit 0
else
	abort 2 "Unsupported OS: ${OS}"
fi

