#!/bin/bash
# depends: curl

source lib/source
echo "Example: 6.6.y"
echo
echo -en "BRANCH:  "
read BRANCH
ITL="\033[3m"
FIN="\033[0m"
GIT_URL="https://github.com/raspberrypi/linux"
GIT_HASH=`git ls-remote --heads ${GIT_URL}.git | grep -w "refs/heads/rpi-${BRANCH}" | sed -n '1p' | sed "s/refs\/heads\/rpi-${BRANCH}//g" | sed s/'\s'//g`
if [[ `wget -S --spider https://raw.githubusercontent.com/raspberrypi/linux/rpi-$BRANCH/Makefile 2>&1 | grep 'HTTP/1.1 200 OK'` ]]; then
	curl --silent -L https://raw.githubusercontent.com/raspberrypi/linux/rpi-$BRANCH/Makefile | grep -m1 "VERSION" | \
	sed 's/VERSION = //g' > revision.txt
	curl --silent -L https://raw.githubusercontent.com/raspberrypi/linux/rpi-$BRANCH/Makefile | grep -m1 "PATCHLEVEL" | \
	sed 's/PATCHLEVEL = /./g' >> revision.txt
	curl --silent -L https://raw.githubusercontent.com/raspberrypi/linux/rpi-$BRANCH/Makefile | grep -m1 "SUBLEVEL" | \
	sed 's/SUBLEVEL = /./g' >> revision.txt
	curl --silent -L https://raw.githubusercontent.com/raspberrypi/linux/rpi-$BRANCH/Makefile | grep -m1 "EXTRAVERSION" | \
	sed 's/EXTRAVERSION =//g' | sed 's/ //g' >> revision.txt
else
	echo ""
	echo -e "The BRANCH you selected is not available."
	rm -f revision.txt
	exit 0
fi

if [[ -f "revision.txt" ]]; then
	echo -en "CURRENT: "
	tr -d '\n' < revision.txt
	rm -f revision.txt
	if [[ -f ".cache/git_fast.${KERNEL}-${BRANCH}" ]]; then
		source .cache/git_fast.${KERNEL}-${BRANCH}
		if [[ "$GIT_COMMIT" == "$GIT_HASH" ]]; then echo -e ""; else echo -e " ${ITL}NEW!${FIN}"; fi; else echo -e "";
	fi
else
	echo -e "Something went wrong?"
	rm -f revision.txt
	exit 1
fi

exit 0
