#!/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 copy_config
{
   found=0
   skip_line=0
   line_number=0
   end_reached=0
   while read LINE; do
	if [ $found -eq 1 ]; then
           if [ $skip_line -ge 2 ]; then
		sudo truncfile $line_number $destination_file /root/temp/first.txt 
		end_reached=1
		found=0
           fi;
	   ((skip_line+=1))
	fi;

	if [[ $LINE  =~ "[autoexec]" ]]; then
	   found=1
	fi;
        ((line_number+=1))
	if [ $end_reached -eq 1 ]; then
	   echo $LINE | sudo tee -a /root/temp/second.txt > /dev/null
	fi;

   done < $destination_file
   found=0
   skip_line=0
   end_reached=0

   sudo addline "mount $drive $folder" /root/temp/added.txt
   sudo joinfile /root/temp/second.txt  /root/temp/added.txt
   sudo joinfile /root/temp/added.txt  /root/temp/first.txt
   sudo copyfile /root/temp/first.txt $destination_file
   sudo rm /root/temp/first.txt
   sudo rm /root/temp/second.txt
   sudo rm /root/temp/added.txt

}



function choose_drive
{
   drive=$(dialog --max-input 1 --title "Assign a drive letter (ex: C, D, E)" --backtitle "$BACKTITLE" --inputbox "Enter a drive letter: " 8 46 3>&1 1>&2 2>&3 3>&-)

   exit_status0=$?
   if [ $exit_status0 -ne 0 ]; then
	clear
	exit
   fi;

   while :
   do
      if [[ $drive =~ [^a-zA-Z] ]]  || [[ $drive == "" ]]; 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 "Assign a drive letter (ex: C, D, E)" --backtitle "$BACKTITLE" --inputbox "Enter a drive letter: " 8 46 3>&1 1>&2 2>&3 3>&-) 
	   exit_status9=$?
           if [ $exit_status9 -ne 0 ]; then
	      clear
	      exit
           fi;
      else
	   break
      fi;
   done

   drive_lower=${drive,,}

if [[ $config_content_lower =~ "mount $drive_lower " ]]  ||  [[ $config_content_lower =~ "imgmount $drive_lower " ]]; then
   clear
   dialog --title "ERROR" --backtitle "$BACKTITLE" --msgbox "\nThe drive letter $drive is already used in your configuration file!\n\nIf you want to use it, you need to unmount the drive first" 14 40
   choose_drive
   exit
else
   folder=$(dialog  --clear --stdout --title "Mount a folder - Spacebar to select, return to confirm" --backtitle "$BACKTITLE" --dselect /home/dosbian/ 16 68 )
   exit_status7=$?
   if [ $exit_status7 -ne 0 ]; then
	clear
	exit
   fi;
 while :
   do
      if [[ -d $folder ]] && [[  $folder != *".."*  ]]  && [[  $folder != *"."*  ]]; then
         break
      else
            dialog --title "ERROR" --backtitle "$BACKTITLE" --msgbox "\nThis is not a valid folder!\n\nPlease, select another one" 14 40
            folder=$(dialog  --clear --stdout --title "Mount a folder - Spacebar to select, return to confirm" --backtitle "$BACKTITLE" --dselect /home/dosbian/ 16 68 )
	    exit_status8=$?
            if [ $exit_status8 -ne 0 ]; then
	       clear
	       exit
            fi;
fi;
   done
   copy_config
   dialog --title "SUCCESS" --backtitle "$BACKTITLE" --msgbox "\nThe drive $drive was mounted successfully on folder\n\n$folder " 14 50

fi;


}


choose_drive
