#!/bin/bash

HEIGHT=38
WIDTH=108
CHOICE_HEIGHT=38

pi_model="No model detected"

function get_raspberry_pi_model() {
  model=$(cat /sys/firmware/devicetree/base/model)
  pi_model="$model"
}

get_raspberry_pi_model

BACKTITLE="*** DOSBIAN V2.5 for Raspberry Pi 3/4/400/5 - Your brand new 486DX machine :) - Developed by Carmelo Maiolino (C) 2020-2024 *** "
TITLE="DOSBIAN V2.5 is running on $pi_model "
MENU=""
OPTIONS=(
        "" ""
        1 "  START MY RETRO-PERSONAL COMPUTER [ Boots Dosbian with the emulator you selected ]  "
	2 "  DOSBIAN CONFIGURATION HELPER  [ Helps you creating/mounting HDD, Floppy, CD-ROM ]  "
	3 "  INFO [ Information about Dosbian 2.5 ]"
	"" ""
        "R" "  REBOOT [ Reboots your Raspberry Pi ] "
        "S" "  SHUTDOWN [ Shutdown Raspberry Pi ] "
	"Q" "  QUIT [ Exit and return to command line ] "
       	"" ""
        "" "------------------------------------------------------------------------------------"
        "" ">>> SET WHAT DOSBIAN HAS TO RUN AT BOOT <<<" 
        "" ""
        "V" "  DOSBOX SVN [ Original Dosbox, good for DOS apps and games, no Windows support ]    "
        "T" "  DOSBOX-STAGING   [ Modern Dosbox, best experience ever for DOS games ]  "
	"E" "  DOSBOX ECE [ Good for DOS games but a bit aged ] "
	"L" "  LAUNCHBOX  [ A game launcher to make your life easier ] "
	"U" "  SCUMMVM 2.8  [ Run this machine for the best adventure games experience ] "
	"" ""
        "" "------------------------------------------------------------------------------------"
        "" ">>> RASPBERRY PI CONFIGURATION AND UTILITIES <<<"
        "" ""
	"C" "  RASPI-CONFIG  [ Run Raspberry Pi Configuration tool ]"
	"M" "  MIDNIGHT COMMANDER [ A friendly File Manager to copy your programs/games ] "
	"A" "  ALSA MIXER [ Set the volume of HDMI or headphone audio output ] "
        "F" "  FIX DOSBIAN FOLDER PERMISSIONS: Run it everytime you add files to Dosbian folder "
        "W" "  WHAT IS MY IP?: Print your IP to use with SSH or SAMBA "
        "B" "  ENABLE AND START SAMBA SERVER: Enable Dosbian folder sharing with your PC/MAC "
        "D" "  STOP AND DISABLE SAMBA SERVER: Stop Dosbian folder sharing "
	"" ""
	"" ""
	"" ""
        "" ""

)


fbi -t 2 -1 /usr/local/bin/dosbianmenu.jpg -noverbose 

#Create custom_configurations folder if it does not exist
mkdir -p /home/dosbian/custom_configurations

function are_there_any_custom_configurations
{
	count_options=0;
	for file in /home/dosbian/custom_configurations/*.conf
    	do
           test -f "$file" || continue
       	   count_options=$((count_options + 1));
   	done
        if [ $count_options -eq 0 ]; then
            return 0
        else
	    return 1
        fi;
}

return_configuration=""
function check_configurations_folder ()
{
        #Read external configurations and prepare options to be displayed
        count_options=0;
        file ="";
        selected_index=0;
        options_config=();
        n=0;

        for file in /home/dosbian/custom_configurations/*.conf
    	do
       	   count_options=$((count_options + 1));
	   options_config[$count_options]=$count_options" ${file##*/}"
   	done
	options_config=(${options_config[@]})
        cmd=(dialog --clear  --title "Dosbian custom configurations selector" --menu "Detected custom configurations, please select one to use with Dosbbox or esc/cancel to use default" 28 110 28) 
        config_choice=$("${cmd[@]}" "${options_config[@]}" 2>&1 >/dev/tty)
        exit_status=$?

        if  [ $exit_status -eq 0 ]; then
            for i in ${options_config[@]}; do
               if [ $i == $config_choice ]; then
                selected_index=$n
               fi
	       ((n++))
            done
            return_configuration=${options_config[$(($((selected_index + 1))))]}
        else
            return_configuration="-1"
        fi
}


function is_media_mount_installed() {
  # Get the file path
  file_path="/usr/bin/media-automount"

  # Check if the file exists
  if [[ -f "$file_path" ]]; then
    # The file exists - do nothing
     return 0
   else
    # The file doesn't exist
    cd /usr/src/udev-media-automount
    sudo make install
    sudo udevadm control --reload-rules
    sudo udevadm trigger
    clear
    cd /home/dosbian
  fi
}

is_media_mount_installed


function print_my_ip()
{
   user_ip=$(hostname -I)
   ip="${user_ip%% *}"
   if [ -z "$ip" ]; then
      ip="Unable to retrieve your IP address, please be sure to set a proper network connection"
   fi
   dialog --title "Retrieves your machine's IP address" --backtitle "$BACKTITLE" --msgbox "\nIP: $ip " 8 44
}


while :
do

CHOICE=$(dialog --clear \
                --backtitle "$BACKTITLE" \
                --title "$TITLE" \
                --menu "$MENU" \
                $HEIGHT $WIDTH $CHOICE_HEIGHT \
                "${OPTIONS[@]}" \
                2>&1 >/dev/tty)

clear
case $CHOICE in

	1)  /usr/local/bin/bootmachine && menu
            exit 0
            ;;

	2)  /usr/local/bin/menuconfigurator
	    clear
	    ;;

	Q)
            tput cvvis
            green=$(tput setaf 2)
            normal=$(tput sgr0)
            printf "\nDOSBIAN developed by Carmelo Maiolino, (C) 2020-2024\n\n"
            printf "If you liked this distro and want to leave a comment or get updated about new releases, please, visit my blog at https://cmaiolino.wordpress.com/dosbian\n\n"
	    printf "Need help/support about how to configure Dosbian and you don't know where to start from? Visit the Facebook community at www.facebook.com/groups/Dosbian\n\n"
	    printf "DOSBIAN is a donationware project, this means you can modify, improve, customise it as you like for your own use."
	    printf "\n\nIT IS STRICTLY PROHIBITED TO SELL DOSBIAN DISTROS CONTAINING GAMES AND APPLICATIONS.\n\n"
            exit 0
            ;;


        V)
            are_there_any_custom_configurations 
            result=$?
            #No custom configurations, default config will be loaded
            if [ $result -eq 0 ]; then
	       sudo sed '3c\/usr\/bin/\dosbox -conf "\/home\/dosbian\/.dosbox\/dosbox-SVN.conf" >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
               sudo chmod +x /usr/local/bin/bootmachine
               dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-SVN with default configuration has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox-SVN Shell. ' 15 50
            else
               error="-1"
               check_configurations_folder
               #Configurations are present but the user clicked Cancel button
               if [ $return_configuration == $error ]; then
		   sudo sed '3c\/usr\/bin/\dosbox -conf "\/home\/dosbian\/.dosbox\/dosbox-SVN.conf"  >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
                   sudo chmod +x /usr/local/bin/bootmachine
                   dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-SVN with default configuration has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox-SVN Shell. ' 15 50
               else
               #User selected a custom configuration, it will be saved for the next dosbox launch
                   sudo sed '3c\/usr\/bin/\dosbox -conf "\/home\/dosbian\/custom_configurations\/'$return_configuration'"  >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
                   sudo chmod +x /usr/local/bin/bootmachine
                   dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-SVN with the custom configuration '$return_configuration' has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox-SVN Shell. ' 15 50
               fi
            fi
            ;;


	E)
	    are_there_any_custom_configurations 
            result=$?
            #No custom configurations, default config will be loaded
            if [ $result -eq 0 ]; then
		  sudo sed '3c\/usr\/bin/\dosboxece -conf "\/home\/dosbian\/.dosbox\/dosbox-ECE.conf" >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
            	  sudo chmod +x /usr/local/bin/bootmachine
            	  dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-ECE with default configuration has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox Shell. ' 15 50
	    else
		error="-1"
                check_configurations_folder
                #Configurations are present but the user clicked Cancel button
                if [ $return_configuration == $error ]; then
		  sudo sed '3c\/usr\/bin/\dosboxece -conf "\/home\/dosbian\/.dosbox\/dosbox-ECE.conf" >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
            	  sudo chmod +x /usr/local/bin/bootmachine
            	  dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-ECE with default configuration has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox Shell. ' 15 50
               else
               #User selected a custom configuration, it will be saved for the next dosbox launch
                  sudo sed '3c\/usr\/bin/\dosboxece -conf "\/home\/dosbian\/custom_configurations\/'$return_configuration'"  >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
                  sudo chmod +x /usr/local/bin/bootmachine
                  dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-ECE with the custom configuration '$return_configuration' has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox-SVN Shell. ' 15 50
               fi
            fi
            ;;



	T)
	    are_there_any_custom_configurations 
            result=$?
            #No custom configurations, default config will be loaded
            if [ $result -eq 0 ]; then
		  sudo sed '3c\/usr\/bin/\dosbox-staging -conf "\/home\/dosbian\/.config\/dosbox\/dosbox-staging.conf" >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
            	  sudo chmod +x /usr/local/bin/bootmachine
            	  dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-STAGING with default configuration has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox Shell. ' 15 50
	    else
		error="-1"
                check_configurations_folder
                #Configurations are present but the user clicked Cancel button
                if [ $return_configuration == $error ]; then
		sudo sed '3c\/usr\/bin/\dosbox-staging -conf "\/home\/dosbian\/.config\/dosbox\/dosbox-staging.conf" >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
            	sudo chmod +x /usr/local/bin/bootmachine
            	dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-STAGING with default configuration has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox Shell. ' 15 50
               else
               #User selected a custom configuration, it will be saved for the next dosbox launch
                  sudo sed '3c\/usr\/bin/\dosbox-staging -conf "\/home\/dosbian\/custom_configurations\/'$return_configuration'"  >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
                  sudo chmod +x /usr/local/bin/bootmachine
                  dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nDosbox-STAGING with the custom configuration '$return_configuration' has been set successfully!\n\nNext time you reboot, you will be brought straightly to the Dosbox-SVN Shell. ' 15 50
               fi
            fi
            ;;


	L)
            sudo sed '3c\/usr\/bin/\dosbox -conf "\/home\/dosbian\/programs\/launchbx\/launchbox.conf"  >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
            sudo chmod +x /usr/local/bin/bootmachine
            dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nLAUNCHBOX set successfully!\n\nNext time you reboot, you will be brought straightly to Launchbox frontend. ' 18 40
            ;;

	U)
            sudo sed '3c\/usr\/bin/\scummvm >\/dev\/null 2>\&1' /usr/local/bin/bootmachine > /home/dosbian/temp.txt && sudo mv /home/dosbian/temp.txt /usr/local/bin/bootmachine
            sudo chmod +x /usr/local/bin/bootmachine
            dialog --backtitle "$BACKTITLE" --title "Information" --msgbox '\n\nScummVM 2.8 set successfully!\n\nNext time you reboot, you will be brought straightly to this machine. ' 18 40
          ;;

	C) sudo raspi-config
            ;;

	M)  sudo mc
            ;;

 	R) tput civis
            sudo shutdown -r now
            ;;

        S) tput civis
            sudo shutdown -h now
            ;;

	3) info
            ;;

	A) sudo alsa
            ;;

        B) sudo systemctl enable smbd.service
           sudo systemctl start smbd.service
            ;;

        D) sudo systemctl disable smbd.service
           sudo systemctl stop smbd.service
           ;;

        F) sudo chmod -R 777 /home/dosbian
           ;;

        W) print_my_ip
           ;;

esac

done


