#!/bin/bash
clear

C_RED_BLD='\033[1;31m' C_YLW_BLD='\033[1;93m' C_GRN_BLD='\033[1;32m' C_BLU_BLD='\033[1;94m' C_CYN_BLD='\033[1;36m' C_WHT_BLD='\033[1;37m'
C_RED='\033[0;91m'	  C_YLW='\033[0;93m'		 C_GRN='\033[0;92m'		C_BLU='\033[0;94m'	  C_CYN='\033[0;36m'		 C_WHT='\033[0;37m'
C_BLANK='\033[0m'      C_FLASH='\033[1;5m'

TOOLKIT_PATH="$(pwd)" # "${HOME}/ps4"
TOOLKIT_WWW="https://www.psx-place.com/resources/ps4-hdd-decryption-helper.1294/history"
CHECK_DNS="8.8.8.8"

# ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

echo -e "Checking internet connection..."

if ping -c 1 -W 2 $CHECK_DNS > /dev/null 2>&1; then
	echo -e "${C_GRN_BLD}OK${C_GRN}, ${CHECK_DNS} responded.${C_BLANK}"
	wget -P /tmp $TOOLKIT_WWW &> /dev/null

	VERSION_CURRENT=$(awk 'NR==2 {print $1}' "${TOOLKIT_PATH}/docs/changelog.txt")
	VERSION_NEW=$(awk 'match($0, /[0-9]{4}-[0-9]{2}-[0-9]{2}/) { print substr($0, RSTART, RLENGTH); exit }' /tmp/history)
 # VERSION_NEW=$(sed -n '/span class="muted"/p' /tmp/history | cut -c 95-104)

	echo
	echo -e " ${C_YLW_BLD}Current toolkit version:${C_BLANK}"
	echo -e " ${VERSION_CURRENT}"
	echo
	echo -e " ${C_YLW_BLD}Newest toolkit version:${C_BLANK}"
	echo -e " ${VERSION_NEW}"
	echo

	rm /tmp/history &> /dev/null

	COMPARE_CURRENT=$(echo "${VERSION_CURRENT}" | tr -d -)
	COMPARE_NEW=$(echo "${VERSION_NEW}" | tr -d -)

	if [[ ${COMPARE_CURRENT} -lt ${COMPARE_NEW} ]]; then
		echo -e " ${C_GRN_BLD}NEW version availble! ${C_GRN}I will open for you download page: ^^${C_BLANK}"
		echo -e " ${TOOLKIT_WWW}\n"
		xdg-open $TOOLKIT_WWW &> /dev/null
		else
			echo -e " ${C_RED}Sorry but ${C_RED_BLD}NO new version${C_RED} availble yet! :(${C_BLANK}\n"
			sleep 10 && exit
	fi

	read -p "Press enter key to exit... "

	else
		echo -e "\n${C_RED}Probably ${C_RED_BLD}NO${C_RED} network connection! ${CHECK_DNS} remains silent...\n"
		sleep 10 && exit
fi
