#!/bin/bash
do {
	#////////////////////////////////////
	# DietPi Launcher
	#
	#////////////////////////////////////
	# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
	#
	#////////////////////////////////////
	#
	# Info:
	# - filename /DietPi/dietpi/dietpi-launcher
	# - Whiptail menu with list of all DietPi programs etc.
	#
	#////////////////////////////////////

	#Import DietPi-Globals ---------------------------------------------------------------
	source /DietPi/dietpi/func/dietpi-globals
	G_CHECK_ROOT_USER
	G_CHECK_ROOTFS_RW
	export G_PROGRAM_NAME='DietPi-Launcher'
	#Import DietPi-Globals ---------------------------------------------------------------

	#/////////////////////////////////////////////////////////////////////////////////////
	# MENUS
	#/////////////////////////////////////////////////////////////////////////////////////
	setvar PROGRAM_NAME = ""DietPi-Launcher""

	setvar CHOICE = '0'
	setvar OPTION = '0'
	setvar TARGETMENUID = '0'
	setvar LAST_SELECTED_ITEM = ""dietpi-config""

	#TARGETMENUID=0
	proc Menu_Main {

		local option_name=()

		setvar option_name = ''("" "────Install Optimized Software──────────────");((index++))
		setvar option_name = ''("DietPi-Software" "Install optimized software thats ready to run.");((index++))
		setvar option_name = ''("" "────Configuration───────────────────────────");((index++))
		setvar option_name = ''("DietPi-Config" "Feature rich config tool for your device.");((index++))

		if (( $(dpkg --get-selections | grep -ci -m1 '^alsa-utils') )) {

			setvar option_name = ''("DietPi-JustBoom" "Launches EQ and MPD audio options menu")

		}

		setvar option_name = ''("DietPi-AutoStart" "Choose what software runs after boot.");((index++))

		#+ add dietpi-cloudshell if installed.
		if test -f /DietPi/dietpi/.installed && (( $(cat /DietPi/dietpi/.installed | grep -ci -m1 'aSOFTWARE_INSTALL_STATE\[62\]=2') )) {

			setvar option_name = ''("DietPi-CloudShell" "Adjust update rate, scenes settings and more.");((index++))

		}

		setvar option_name = ''("DietPi-Cron" "Modify the start times of cron jobs.");((index++))
		setvar option_name = ''("DietPi-Process_Tool" "Tweak Nice, Affinity, Schedulers for programs.");((index++))
		setvar option_name = ''("DietPi-Drive_Manager" "Setup and control multiple external drives.");((index++))
		setvar option_name = ''("" "────DietPi Updates──────────────────────────");((index++))
		setvar option_name = ''("DietPi-Update" "Keep your DietPi system upto date.");((index++))
		setvar option_name = ''("" "────Backups / Sync──────────────────────────");((index++))
		setvar option_name = ''("DietPi-Backup" "Backup and restore your DietPi system.");((index++))
		setvar option_name = ''("DietPi-Sync" "Duplicate (Sync) one directory to another.");((index++))
		setvar option_name = ''("" "────Maintenance─────────────────────────────");((index++))
		setvar option_name = ''("DietPi-Cleaner" "Remove unwanted junk from your system.");((index++))
		setvar option_name = ''("" "────Misc────────────────────────────────────");((index++))
		setvar option_name = ''("DietPi-BugReport" "Found a bug? Let us know!");((index++))
		setvar option_name = ''("DietPi-CpuInfo" "Displays CPU Temp, frequencies, type etc.");((index++))
		setvar option_name = ''("DietPi-LetsEncrypt" "Frontend for Lets Encrypt, free SSL certs");((index++))
		setvar option_name = ''("DietPi-MorseCode" "Converts and outputs a text file to morsecode.");((index++))


		setvar OPTION = $(whiptail --title "$PROGRAM_NAME" --menu "Please select a program to run:" --cancel-button "Exit" --backtitle "$PROGRAM_NAME" --default-item "$LAST_SELECTED_ITEM" 18 76 10 "${option_name[@]}" 3>&1 1>&2 2>&3)
		setvar CHOICE = ""$?

		unset option_name

		#Run program
		if (( $CHOICE == 0 )) {

			if test $OPTION != "" {

				setvar LAST_SELECTED_ITEM = "$OPTION"

				#convert to lowercase
				local command=$(echo "${OPTION,,}")

				#Run
				if test -f "/DietPi/dietpi/$command" {

					/DietPi/dietpi/"$command"

				# - different location
				} elif test -f "/DietPi/dietpi/misc/$command" {

					/DietPi/dietpi/misc/"$command"

				# - different location
				} elif test -f "/DietPi/dietpi/func/$command" {

					/DietPi/dietpi/func/"$command"

				}

				#+ Wait for user input.
				if test $command = "dietpi-cpuinfo" ||
					test $command = "dietpi-update" ||
					test $command = "dietpi-morsecode" {

					echo -e ""
					read -p " - Press any key to exit...."

				}

			}

		#Exit
		} else {

			whiptail --title "Exit $PROGRAM_NAME?" --yesno "Exit $PROGRAM_NAME?" --backtitle $PROGRAM_NAME --yes-button "Ok" --no-button "Back" --defaultno 9 55
			setvar CHOICE = ""$?
			if (( $CHOICE == 0 )) {

				setvar TARGETMENUID = '-1'

			}

		} {

		#Clear Screen buffer
		clear

		if (( $TARGETMENUID == 0 )) {
			Menu_Main

		}

	}

	#-----------------------------------------------------------------------------------
	exit
	#-----------------------------------------------------------------------------------
}