#!/bin/bash

dir="$(dirname "$0")"

GNOMEAPPS="$dir/data/gnome-apps.list"

# Cleanup System
function gnome {
	eval `resize` 
	GNOME=$(whiptail \
		--notags \
		--title "Install Latest GNOME" \
		--menu "\nWhat would you like to do?" \
		--cancel-button "Go Back" \
		$LINES $COLUMNS $(( $LINES - 12 )) \
		gnome-upgrade   'Update to latest GNOME' \
		gnome-desktop   'Install basic GNOME desktop' \
		gnome-apps	  'Install GNOME core applications' \
		3>&1 1>&2 2>&3)
	 
	exitstatus=$?
	if [ $exitstatus = 0 ]; then
		$GNOME
	else
		main
	fi
}

# Add GNOME PPA
function gnome-upgrade {
	# Add repository
	show_info 'Adding GNOME3 PPA to software sources...'
	show_warning 'Requires root privileges'
	sudo add-apt-repository -y ppa:gnome3-team/gnome3
	# Update repository information
	show_info 'Updating repository information...'
	sudo apt update -y
	show_success 'Done.'
	# Upgrade GNOME
	show_info 'Updating repository information...'
	sudo apt dist-upgrade -y
	show_success 'Done.'
	gnome
}

# Install GNOME Desktop
function gnome-desktop {
	# Install
	echo 'Installing GNOME Desktop...'
	show_warning 'Requires root privileges'
	sudo apt install -y fonts-cantarell gnome-shell gnome-shell-extensions gnome-session gnome-terminal nautilus
	# Done
	show_success 'Done.'
	# Check
	EXITSTATUS=$1
	if [[ $EXITSTATUS != 0 ]]; then
		whiptail --title "Finished" --msgbox "GNOME desktop is installed." 8 78
		gnome
	else
		whiptail --title "Finished" --msgbox "GNOME desktop installation complete." 8 78
		gnome
	fi
	gnome
}

# Install GNOME Apps
function gnome-apps {
	if (eval `resize` && whiptail --title \
		"GNOME Apps"  --yesno \
		"Current list of GNOME apps: \n\n$(cat $GNOMEAPPS) \n\nWould you like to install the GNOME core app suite?" \
		$LINES $COLUMNS $(( $LINES - 12 )) \
		--scrolltext ) then
		show_info 'Installing '$NAME'...'
		show_warning 'Requires root privileges'
		# Feel free to change the contents of 'gnome-apps.list' in the data folder to whatever suits your preference.
		sudo apt install -y --no-install-recommends $(cat $GNOMEAPPS)
		# Done
		show_success 'Done.'
		# Check
		EXITSTATUS=$1
		if [[ $EXITSTATUS != 0 ]]; then
			whiptail --title "Finished" --msgbox "GNOME apps are installed." 8 78
			gnome
		else
			whiptail --title "Finished" --msgbox "GNOME apps installation complete." 8 78
			gnome
		fi
	else
		gnome
	fi
}