#!/bin/bash

# Everything else needs to be run as root
BNAME=`basename $0`
if [[ ${EUID} -ne 0 ]]; then
	clear

	echo "Script must be run as root.

Try 'sudo bash ./$BNAME'
"
	exit 1
fi
NKVER=`uname -r`

do_reboot() {
	if [ x"$ASK_TO_REBOOT" == x"true" ]; then
		echo
		read -p "Would you like to reboot now [y/n] ? " YESNO
		if [ x"$YESNO" == x"n" -o x"$YESNO" == x"N" ]; then
			return 0
		else # yes
			sync ; sync
			reboot
		fi
	else
		echo "A reboot is needed"
		sync ; sync
		sleep 5
		reboot
	fi
	return 0
}

clear
echo "current kernel version is "$NKVER
echo
read -p "1) upgrade kernel version 4.18
2) upgrade kernel version 5.0
q) exit
select 1 or 2 : " VER
echo $VER

if [ x"$VER" == x"1" ] ; then
	echo "4.18"

	echo "deb http://ports.ubuntu.com/ubuntu-ports cosmic universe" > /etc/apt/sources.list.d/linux-raspi2-update.list

	apt-get update 2>&1 | tee /var/log/linux-raspi2-update_apt-get_update.log
	apt install linux-image-raspi2 -y 2>&1 | tee /var/log/linux-raspi2-update_apt-install.log

	RET=$?
	echo "RET="$RET

	rm -f /etc/apt/sources.list.d/linux-raspi2-update.list

	apt-get update 2>&1 > /dev/null

	ASK_TO_REBOOT=true
	do_reboot
elif [ x"$VER" == x"2" ] ; then
	echo "5.0"

	echo "deb http://ports.ubuntu.com/ubuntu-ports disco universe" > /etc/apt/sources.list.d/linux-raspi2-update.list

	apt-get update 2>&1 | tee /var/log/linux-raspi2-update_apt-get_update.log
	apt install linux-image-raspi2 -y 2>&1 | tee /var/log/linux-raspi2-update_apt-install.log

	RET=$?
	echo "RET="$RET

	rm -f /etc/apt/sources.list.d/linux-raspi2-update.list

	apt-get update 2>&1 > /dev/null

	ASK_TO_REBOOT=true
	do_reboot
else
	exit 1
fi

