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

BACKTITLE="*** DOSBIAN IMAGE MOUNT 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 ()
{
#  First param can be 1 stands for Floppy, 2 for CD-ROM, 3 for HDD
   image_type=$1

#  Second param is the file_path
   file_path=$2

#  Third param is the HDD cylinder
    cylinder=$3

   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

#  Floppy Drive

   if [ $image_type -eq 1 ]; then
   	sudo addline "imgmount $drive $file_path -t floppy" /root/temp/added.txt
   fi;

# CD-ROM drive

   if [ $image_type -eq 2 ]; then
        if [[ $file_path == *cue ]]; then
           sudo addline "imgmount $drive $file_path -t iso -fs iso" /root/temp/added.txt
	else
   	   sudo addline "imgmount $drive $file_path -t iso" /root/temp/added.txt
        fi;
   fi;

# HDD drive

   if [ $image_type -eq 3 ]; then
   	sudo addline "imgmount $drive $file_path -t hdd  -size 512,63,16,$cylinder" /root/temp/added.txt
   fi;

   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
else
   image_type_choice=$(dialog --clear  --output-fd 1 --title "Choose image type" --backtitle "$BACKTITLE" \
               --radiolist "Please, choose what kind of image you want to mount" 16 40 3 \
               1 "Floppy Disk" off \
               2 "CD-ROM" off \
               3 "Hard Disk" on )

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

   case $image_type_choice in

	1) file_selected=$(dialog  --clear --stdout --title "Mount a Floppy Disk Image"  --backtitle "$BACKTITLE" --fselect /home/dosbian/ 16 74 )
	   exit_status70=$?
   	   if [ $exit_status70 -ne 0 ]; then
              clear
              exit
           fi;
	   while :
           do
              if [[ -f $file_selected ]]; then
                  copy_config 1 $file_selected
          	  dialog --title "SUCCESS" --backtitle "$BACKTITLE" --msgbox "\nThe file image\n\n$file_selected \n\nwas mounted successfully on Floppy drive $drive " 14 50
                  break
              else
	          dialog --title "ERROR" --backtitle "$BACKTITLE" --msgbox "\nThe file you have choosen is not a valid file!\n\nPlease, select a new one" 14 40
                  file_selected=$(dialog  --clear --stdout  --title "Mount a Floppy Disk Image" --backtitle "$BACKTITLE" --fselect /home/dosbian/ 16 74 )
	          exit_status80=$?
                 if [ $exit_status80 -ne 0 ]; then
	             clear
                     break
                 fi;
              fi;
          done
	  exit
	  ;;


	2) file_selected=$(dialog  --clear --stdout --title "Mount a CD-ROM Disk Image"  --backtitle "$BACKTITLE" --fselect /home/dosbian/ 16 74 )
	   exit_status70=$?
   	   if [ $exit_status70 -ne 0 ]; then
              clear
              exit
           fi;
	   while :
           do
              if [[ -f $file_selected ]]; then
                  copy_config 2 $file_selected
                  dialog --title "SUCCESS" --backtitle "$BACKTITLE" --msgbox "\nThe file image\n\n$file_selected \n\nwas mounted successfully on CD-ROM drive $drive " 14 50
                  break
              else
	          dialog --title "ERROR" --backtitle "$BACKTITLE" --msgbox "\nThe file you have choosen is not a valid file!\n\nPlease, select a new one" 14 40
                  file_selected=$(dialog  --clear --stdout  --title "Mount a CD-ROM Disk Image" --backtitle "$BACKTITLE" --fselect /home/dosbian/ 16 74 )
	          exit_status80=$?
                 if [ $exit_status80 -ne 0 ]; then
	             clear
                     break
                 fi;
              fi;
          done
	  exit
	  ;;

	3) file_selected=$(dialog  --clear --stdout --title "Mount a HDD Image"  --backtitle "$BACKTITLE" --fselect /home/dosbian/ 16 74 )
	   exit_status70=$?
   	   if [ $exit_status70 -ne 0 ]; then
              clear
              exit
           fi;
	   while :
           do
              if [[ -f $file_selected ]]; then
                 break
              else
	          dialog --title "ERROR" --backtitle "$BACKTITLE" --msgbox "\nThe file you have choosen is not a valid file!\n\nPlease, select a new one" 14 40
                  file_selected=$(dialog  --clear --stdout  --title "Mount a CD-ROM Disk Image" --backtitle "$BACKTITLE" --fselect /home/dosbian/ 16 74 )
	          exit_status80=$?
                 if [ $exit_status80 -ne 0 ]; then
	             clear
                     break
                 fi;
              fi;
          done

          cylinder=$(dialog  --title "Cylynders" --backtitle "$BACKTITLE" --inputbox "Enter number of cylinders: " 8 46 3>&1 1>&2 2>&3 3>&-)
	  exit_status0=$?
   	  if [ $exit_status0 -ne 0 ]; then
	     clear
	     exit
          fi;

   	  while :
          do
      	    if [[ $cylinder =~ [^0-9] ]]  || [[ $cylinder == "" ]]; then
                dialog --title "ERROR" --backtitle "$BACKTITLE" --msgbox "\nInvalid input, only numeric values are allowed" 14 40
                cylinder=$(dialog  --title "Cylynders" --backtitle "$BACKTITLE" --inputbox "Enter number of cylinders: " 8 46 3>&1 1>&2 2>&3 3>&-)
	        exit_status9=$?
                if [ $exit_status9 -ne 0 ]; then
	           clear
	           exit
                fi;
            else
	        break
            fi;
         done
         copy_config 3 $file_selected $cylinder
         dialog --title "SUCCESS" --backtitle "$BACKTITLE" --msgbox "\nThe file image $file_selected \n\nwas mounted successfully on HDD drive $drive \n\nPLEASE NOTE: If your HDD is not mounted correctly at startup, you  may need to adjust HDD parameters configuration manually.\n\nPlease, refer to the following reference table for correct values:\n\nHDD 256MB ---> 512,63,16,520\nHDD 512MB ---> 512,63,32,520\nHDD 1GB   ---> 512,63,64,520\nHDD 2GB   ---> 512,63,64,1023 " 20 88
	 exit
	 ;;

   esac


fi;


}


choose_drive
