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

if [ $USER != root ]
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

latest=$(curl -sL https://github.com/andersfugmann/T2-Debian-Kernel/releases/latest/ | grep "<title>Release" | awk -F " " '{print $2}' )
latestkver=$(echo $latest | cut -d "v" -f 2)

if [[ (${#latest} = 6) || (${#latest} = 7) ]]
then
latestk=$(echo $latest | cut -c 2- | cut -d "-" -f 1 | awk '{print $1".0-t2"}')
else
latestk=$(echo $latest | cut -c 2- | cut -d "-" -f 1 | awk '{print $1"-t2"}')
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 \) ]; then
	
	echo "Downloading new kernel $latest"
	echo -e "\nDownloading linux-headers-${latestk}"
	echo
	curl -#L https://github.com/andersfugmann/T2-Debian-Kernel/releases/download/${latest}/linux-headers-${latestk}_${latestkver}_amd64.deb > headers.deb
	echo -e "\nDownloading linux-image-${latestk}"
	echo
	curl -#L https://github.com/andersfugmann/T2-Debian-Kernel/releases/download/${latest}/linux-image-${latestk}_${latestkver}_amd64.deb > image.deb

	echo -e "\nVerifying checksums"
	curl -sL https://github.com/andersfugmann/T2-Debian-Kernel/releases/download/${latest}/sha256 > 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
