#!/bin/bash
#////////////////////////////////////
# DietPi DXX-Rebirth Launcher
#
#////////////////////////////////////
# Created by Daniel Knight / daniel.knight@dietpi.com / dietpi.com
#
#////////////////////////////////////

MAX_FPS=60

FP_INSTALL_DIRECTORY='/mnt/dietpi_userdata/dxx-rebirth'

PROGRAM_NAME="DietPi - DXX-Rebirth"

TARGETMENUID=0

Menu_Main(){

	local whiptail_menu_array=()

	#OpenGL enabled
	if (( $(cat /DietPi/config.txt | grep -ci -m1 '^dtoverlay=vc4-kms-v3d') )); then

		whiptail_menu_array+=("3" ": Descent 1 (OpenGL)")
		whiptail_menu_array+=("4" ": Descent 2 (OpenGL)")

	#FBdev
	else

		whiptail_menu_array+=("1" ": Descent 1 (FB)")
		whiptail_menu_array+=("2" ": Descent 2 (FB)")

	fi

	#whiptail_menu_array+=("5" ": Max FPS ($MAX_FPS)")

	WHIP_QUESTION='Select a program:'
	OPTION=$(whiptail --title "$PROGRAM_NAME" --backtitle "$PROGRAM_NAME" --menu "$WHIP_QUESTION" --cancel-button "Exit" --default-item "$LAST_SELECTED_NAME" 12 60 4 "${whiptail_menu_array[@]}" 3>&1 1>&2 2>&3)
	CHOICE=$?

	unset whiptail_menu_array

	if (( $CHOICE == 0 )); then

		#Descent 1 GLES/FB
		if (( $OPTION == 1 )); then

			"$FP_INSTALL_DIRECTORY"/d1x-rebirth_rpifb -maxfps $MAX_FPS -hogdir "$FP_INSTALL_DIRECTORY"/descent_1_game

		#Descent 2 GLES/FB
		elif (( $OPTION == 2 )); then

			"$FP_INSTALL_DIRECTORY"/d2x-rebirth_rpifb -maxfps $MAX_FPS -hogdir "$FP_INSTALL_DIRECTORY"/descent_2_game

		#Descent 1 OpenGL
		elif (( $OPTION == 3 )); then

			"$FP_INSTALL_DIRECTORY"/d1x-rebirth_rpigl -maxfps $MAX_FPS -hogdir "$FP_INSTALL_DIRECTORY"/descent_1_game

		#Descent 2 OpenGL
		elif (( $OPTION == 4 )); then

			"$FP_INSTALL_DIRECTORY"/d2x-rebirth_rpigl -maxfps $MAX_FPS -hogdir "$FP_INSTALL_DIRECTORY"/descent_2_game

		fi

	else

		Menu_Exit

	fi

}

Menu_Exit(){

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

		#exit
		TARGETMENUID=-1

	else

		#Return to Main Menu
		TARGETMENUID=0

	fi

}


#-------------------------------------------------------------------------------
#Main Loop
#-------------------------------------------------------------------------------

while (( $TARGETMENUID > -1 )); do

	clear

	if (( $TARGETMENUID == 0 )); then

		Menu_Main

	fi

done

#-------------------------------------------------------------------------------

exit 0

#-------------------------------------------------------------------------------
