#!/bin/bash

# Usage: install_if_not_installed package_name
function install_if_not_installed
{
	PACKAGE="$1"
	INSTALLED=$(dpkg -L "$PACKAGE" > /dev/null 2>&1 && echo OK || echo KO)
	if [ "$INSTALLED" == "KO" ]
	then
		sudo apt-get install "$PACKAGE" -y
	fi
}

# Usage: contains regexp file
function contains
{
	REGEXP="$1"
	FILE="$2"

	grep "$REGEXP" "$FILE" > /dev/null && echo OK || echo KO
}

install_if_not_installed v86d
install_if_not_installed hwinfo

sudo hwinfo --framebuffer
echo "---------------------------------------------------------------"
echo "Please enter the best resolution from the list above"
echo "It usualy looks like this >>Mode 0x0323: 1024x768 (+4096), 24 bits<<"
echo "And you have to enter it like this >>1024x768-24<<"
echo "---------------------------------------------------------------"
read resolution

sed 's/GRUB\_CMDLINE\_LINUX\_DEFAULT\=\"quiet\ splash\"/GRUB\_CMDLINE\_LINUX\_DEFAULT\=\"quiet\ splash\ nomodeset\ video\=uvesafb\:mode\_option\='$resolution'\,mtrr\=3\,scroll\=ywrap\"/g' /etc/default/grub > ./newgrub
sudo mv -f ./newgrub /etc/default/grub

sed 's/GRUB\_CMDLINE\_LINUX\_DEFAULT\=\"quiet\ splash\"/GRUB\_CMDLINE\_LINUX\_DEFAULT\=\"quiet\ splash\ nomodeset\ video\=uvesafb\:mode\_option\='$resolution'\,mtrr\=3\,scroll\=ywrap\"/g' /etc/default/burg > ./newburg
sudo mv -f ./newburg /etc/default/burg

sed 's/\#GRUB\_GFXMODE\=640x480/GRUB\_GFXMODE\='$resolution'/g' /etc/default/grub > ./newgrub
sudo mv -f ./newgrub /etc/default/grub

if [ "$(contains uvesafb /etc/initramfs-tools/modules)" == 'KO' ]
then
	sudo echo "uvesafb mode_option=$resolution mtrr=3 scroll=ywrap" | sudo tee -a /etc/initramfs-tools/modules
fi

if [ "$(contains FRAMEBUFFER=y /etc/initramfs-tools/conf.d/splash)" == 'KO' ]
then
	echo FRAMEBUFFER=y | sudo tee /etc/initramfs-tools/conf.d/splash
fi

sed 's/GRUB\_CMDLINE\_LINUX\_DEFAULT\=\"\$GRUB\_CMDLINE\_LINUX\_DEFAULT vt\.handoff\=7\"/GRUB\_CMDLINE\_LINUX\_DEFAULT\=\"\$GRUB\_CMDLINE\_LINUX\_DEFAULT\"/g' /etc/grub.d/10_linux > ./new10linux
sudo mv -f ./new10linux /etc/grub.d/10_linux
sudo chmod +x /etc/grub.d/10_linux

sudo update-grub2
which update-burg > /dev/null 2>&1 && sudo update-burg
sudo update-initramfs -u
echo "The resolution should be fixed after a reboot"
