#!/bin/bash

# Google Chrome
function google-chrome {
eval `resize` 
exitstatus=$?
if [ $exitstatus = 0 ]; then
	status="0"
	while [ "$status" -eq 0 ]  
	do
	   
		VERSION=$(whiptail \
		--title "Google Chrome" --radiolist \
		"Which version of Chrome would you like to install?" \
		--ok-button "Install" \
		--cancel-button "Go Back" \
		$LINES $COLUMNS $(( $LINES - 12 )) \
		"stable"	"Stable build" ON \
		"beta"	  "Beta build" OFF \
		"unstable"  "Unstable build" OFF \
		3>&1 1>&2 2>&3) 
		 
		# Change to lower case and remove spaces.
		case "${VERSION}" in
			stable) 
				# Variables
				PACKAGE=google-chrome-stable
				NAME="Google Chrome (Stable)"
				# Check if already installed
				echo 'Checking if '$NAME' is already installed...'
				PKGCHECK=$(dpkg-query -W --showformat='${Status}\n' $PACKAGE|grep "install ok installed")
				if [ "" == "$PKGCHECK" ]; then
					echo $NAME 'is not installed. Proceeding'
					show_info 'Downloading '$NAME'...'
					# Download Debian file that matches system architecture
					case `uname -i` in
						i386|i486|i586|i686)
							curl -O https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb
							;;
						x86_64)
							curl -O https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
							;;
						*)
							whiptail --title "No remote packages available to download." --msgbox "Error." 8 78
							;;
					esac
					# Install package(s)
					show_info 'Installing '$NAME'...'
					show_warning 'Requires root privileges'
					sudo dpkg -i google-chrome*.deb
					sudo apt install -fy
					# Cleanup and finish
					rm google-chrome*.deb
					# Done
					show_success 'Done.'
					whiptail --title "Finished" --msgbox "Installation of $NAME complete." 8 78
					status=1
					thirdparty
				else
					# Already installed
					show_success $NAME 'already installed.'
					whiptail --title "Finished" --msgbox "$NAME is already installed." 8 78
					thirdparty
				fi
			;;
			beta)
				# Variables
				PACKAGE=google-chrome-beta
				NAME="Google Chrome (Beta)"
				# Check if already installed
				echo 'Checking if '$NAME' is already installed...'
				PKGCHECK=$(dpkg-query -W --showformat='${Status}\n' $PACKAGE|grep "install ok installed")
				if [ "" == "$PKGCHECK" ]; then
					echo $NAME 'is not installed. Proceeding'
					show_info 'Downloading '$NAME'...'
					# Download Debian file that matches system architecture
					case `uname -i` in
						i386|i486|i586|i686)
							curl -O https://dl.google.com/linux/direct/google-chrome-beta_current_i386.deb
							;;
						x86_64)
							curl -O https://dl.google.com/linux/direct/google-chrome-beta_current_amd64.deb
							;;
						*)
							whiptail --title "No remote packages available to download." --msgbox "Error." 8 78
							;;
					esac
					# Install package(s)
					show_info 'Installing '$NAME'...'
					show_warning 'Requires root privileges'
					sudo dpkg -i google-chrome*.deb
					sudo apt install -fy
					# Cleanup and finish
					rm google-chrome*.deb
					# Done
					show_success 'Done.'
					whiptail --title "Finished" --msgbox "Installation of $NAME complete." 8 78
					status=1
					thirdparty
				else
					# Already installed
					show_success $NAME 'already installed.'
					whiptail --title "Finished" --msgbox "$NAME is already installed." 8 78
					thirdparty
				fi
			;;
			unstable) 
				# Variables
				PACKAGE=google-chrome-unstable
				NAME="Google Chrome (Unstable)"
				# Check if already installed
				echo 'Checking if '$NAME' is already installed...'
				PKGCHECK=$(dpkg-query -W --showformat='${Status}\n' $PACKAGE|grep "install ok installed")
				if [ "" == "$PKGCHECK" ]; then
					echo $NAME 'is not installed. Proceeding'
					show_info 'Downloading '$NAME'...'
					# Download Debian file that matches system architecture
					case `uname -i` in
						i386|i486|i586|i686)
							curl -O https://dl.google.com/linux/direct/google-chrome-unstable_current_i386.deb
							;;
						x86_64)
							curl -O https://dl.google.com/linux/direct/google-chrome-unstable_current_amd64.deb
							;;
						*)
							whiptail --title "No remote packages available to download." --msgbox "Error." 8 78
							;;
					esac
					# Install package(s)
					show_info 'Installing '$NAME'...'
					show_warning 'Requires root privileges'
					sudo dpkg -i google-chrome*.deb
					sudo apt install -fy
					# Cleanup and finish
					rm google-chrome*.deb
					# Done
					show_success 'Done.'
					whiptail --title "Finished" --msgbox "Installation of $NAME complete." 8 78
					status=1
					thirdparty
				else
					# Already installed
					show_success $NAME 'already installed.'
					whiptail --title "Finished" --msgbox "$NAME is already installed." 8 78
					thirdparty
				fi
			;;
			*) status=1 
				thirdparty
			;;
		esac
		exitstatus1=$status1
	done
else
	thirdparty
fi
}