#!/bin/bash
#upgrading script apple linux T2 Kernel

if [[ ($USER != root) && ($1 != -c) && ($1 != --check-version) ]]
then
sudo chmod 755 $0
sudo $0 $1
exit 0
fi

wget -q --spider http://github.com

if [ $? -eq 0 ]; then
    true
else
    echo "Please connect to the internet"
    exit 1
fi

set -e

cd /tmp

use_lts=true
lts_version=6.6
CODENAME=$(lsb_release -c | cut -d ":" -f 2 | xargs)

# Linux Mint
if [[ (${CODENAME} = ulyana) || (${CODENAME} = ulyssa) || (${CODENAME} = uma) || (${CODENAME} = una) ]]
then
EFFECTIVE_CODENAME=focal
elif [[ (${CODENAME} = vanessa) || (${CODENAME} = vera) || (${CODENAME} = victoria) || (${CODENAME} = virginia) ]]
then
EFFECTIVE_CODENAME=jammy

# Elementary OS
elif [[ (${CODENAME} = horus) ]]
then
EFFECTIVE_CODENAME=jammy

# Default
else
EFFECTIVE_CODENAME=${CODENAME}
fi

if [[ $use_lts != true ]]
then
latest=$(curl -sL https://github.com/t2linux/T2-Ubuntu-Kernel/releases/latest/ | grep "<title>Release" | awk -F " " '{print $2}' )
else
getkernelver=$(curl -sL https://github.com/t2linux/T2-Ubuntu-Kernel/releases | grep "{{ urlEncodedRefName }}" | grep -v "generic" | grep v${lts_version} | cut -d "{" -f 3 | cut -c 25- | head -1)
latest=${getkernelver::-76}
fi

if [[ (${#latest} = 6) || (${#latest} = 7) ]]
then
pkgrel=$(echo $latest | cut -d "-" -f 2)
latestkver=$(echo $latest | cut -d "v" -f 2 | cut -d "-" -f 1 | awk '{print $1".0"}')-${pkgrel}
latestk=$(echo $latest | cut -c 2- | cut -d "-" -f 1 | awk '{print $1".0-t2"}')-${EFFECTIVE_CODENAME}
else
latestkver=$(echo $latest | cut -d "v" -f 2)
latestk=$(echo $latest | cut -c 2- | cut -d "-" -f 1 | awk '{print $1"-t2"}')-${EFFECTIVE_CODENAME}
fi

if [[ (${EFFECTIVE_CODENAME} = focal) ]]
then
cat <<EOF
You seem to be using Ubuntu 20.04 Focal Fossa or its derived distro.
These distros are no longer supported
The latest LTS version 6.1.71 shall be installed and no further kernel upgrades shall be available.

EOF
latest=v6.1.71-1
latestk=6.1.71-t2-focal
latestkver=6.1.71-1
fi

currentk=$(uname -r)
currentkver=$(apt show linux-image-${currentk} 2>/dev/null | grep Version | cut -d ":" -f 2 | xargs)
existingK=($(dpkg --list | grep linux-image- | grep 't2\|mbp' | cut -d ' ' -f 2-3 ))
if [[ ( $latestkver != $currentkver ) && (( $(echo ${latestk} | cut -d "." -f 1,2) != $lts_version ) || ( ${use_lts} = true )) ]]; then

	if [[ ($1 = -c) || ($1 = --check-version) ]]; then
		echo "New kernel $latest is available."
		[ -n "$DISPLAY" ] && [ -x "$(command -v notify-send)" ] && notify-send --icon=info -t 12000 \
		"New kernel $latest is available." \
		"Run \'update_t2_kernel\' to upgrade your kernel or visit https://github.com/t2linux/T2-Ubuntu-Kernel/releases/tag/${latest}"
		exit 0
	fi
	echo "Downloading new kernel $latest"
	echo -e "\nDownloading linux-headers-${latestk}"
	echo
	curl -#L https://github.com/t2linux/T2-Ubuntu-Kernel/releases/download/${latest}/linux-headers-${latestk}_${latestkver}_amd64.deb > headers.deb
	echo -e "\nDownloading linux-image-${latestk}"
	echo
	curl -#L https://github.com/t2linux/T2-Ubuntu-Kernel/releases/download/${latest}/linux-image-${latestk}_${latestkver}_amd64.deb > image.deb

	echo -e "\nVerifying checksums"
	curl -sL https://github.com/t2linux/T2-Ubuntu-Kernel/releases/download/${latest}/sha256-${EFFECTIVE_CODENAME} > sha

	actual_headers_chksum=$(cat ./sha | grep linux-headers-${latestk}_${latestkver}_amd64.deb | cut -d " " -f 1)
	actual_image_chksum=$(cat ./sha | grep linux-image-${latestk}_${latestkver}_amd64.deb | cut -d " " -f 1)
	downloaded_headers_chksum=$(sha256sum ./headers.deb | cut -d " " -f 1)
	downloaded_image_chksum=$(sha256sum ./image.deb | cut -d " " -f 1)

	if [[ ${actual_headers_chksum} != ${downloaded_headers_chksum} ]]
	then
	echo -e "\nError: Failed to verify checksum of linux-headers-${latestk}"
	fi

	if [[ ${actual_image_chksum} != ${downloaded_image_chksum} ]]
	then
	echo -e "\nError: Failed to verify checksum of linux-image-${latestk}"
	fi

	if [[ (${actual_headers_chksum} = ${downloaded_headers_chksum}) && (${actual_image_chksum} = ${downloaded_image_chksum}) ]]; then
		#install
		echo -e "\nChecksums successfully verified"
		echo -e "\nInstalling new kernel $latest"
		echo
		apt install ./headers.deb ./image.deb
		#shutdown -r 02:00 "reboot scheduled for the next 2am to update runnning kernel"
		rm -f headers.deb image.deb

		#uninstall old t2 kernels but preserve current version as a backup
		if [[ ( $latestk != $currentk ) ]]; then
			for kernel in "${existingK[@]}"
			do
				if [ \( $kernel != linux-image-$currentk \) ]; then
					echo -e "\n$kernel needs to be removed"
					version=${kernel/#linux-image-}
					#purge old t2 kernel
					echo
					dpkg -P linux-headers-$version linux-image-$version
				fi
			done
		fi

		if [[ ($1 = --remove-current) ]]; then
			echo -e "\nCurrent kernel linux-image-$currentk shall also be removed"
			echo
			dpkg -P linux-headers-$currentk linux-image-$currentk
		fi
	else
	exit 1
	fi
else
echo "Kernel is up to date"
fi
