#!/bin/bash
destination=$1
destination_file=""
drive=""
drive_lower=""

BACKTITLE="*** DOSBIAN MOUNT DRIVE HELPER - Developed by Carmelo Maiolino (C) 2020-2024 *** "

if [ $destination -eq 1 ]; then
	destination_file="/home/dosbian/.dosbox/dosbox-SVN.conf"
fi;

if [ $destination -eq 2 ]; then
	destination_file="/home/dosbian/.dosbox/dosbox-ECE.conf"
fi;

if [ $destination -eq 3 ]; then
	destination_file="/home/dosbian/programs/launchbx/launchbox.conf"
fi;

if [ $destination -eq 4 ]; then
	destination_file="/home/dosbian/.config/dosbox/dosbox-staging.conf"
fi;


config_content=$(sudo cat $destination_file)
config_content_lower=${config_content,,}


function choose_drive_to_remove
{
   drive=$(dialog --max-input 1 --title "Remove drive" --backtitle "$BACKTITLE" --inputbox "Enter the drive to remove: " 8 46 3>&1 1>&2 2>&3 3>&-)

   while :
   do
      if [[ $drive =~ [^a-zA-Z] ]]; then
            dialog --title "ERROR" --backtitle "$BACKTITLE" --msgbox "\nInvalid input, only letters allowed\n\nExamples: [A..Z] [a..z]" 14 40
            drive=$(dialog --max-input 1 --title "Remove drive" --backtitle "$BACKTITLE" --inputbox "Enter the drive to remove: " 8 46 3>&1 1>&2 2>&3 3>&-) 
      else
	break
      fi;
   done

   drive_lower=${drive,,}
   exit_status6=$?
   if [ $exit_status6 -ne 0 ]; then
	clear
	exit
   fi;

   line_to_remove="mount $drive_lower "
   if [[ $config_content_lower =~ $line_to_remove ]]; then
        #   remove the line
	sudo sed -i -e "/$line_to_remove/Id" $destination_file
        dialog --title "SUCCESS" --backtitle "$BACKTITLE" --msgbox "\nThe drive $drive has been unmounted successfully" 14 50
        exit
   else
        clear
        dialog --title "ERROR" --backtitle "$BACKTITLE" --msgbox "\nThe drive $drive was not found in the configuration file!\n\nNothing will be done" 14 40
        exit
   fi;


}


choose_drive_to_remove
