# This file is part of the 'dragora-installer'.
#
# Purpose: Medium selector (CD-DVD/Pendrive/SD/Hard disk).

# Show media menu

while true
do
    dialog --colors \
     --backtitle "\\ZbSelecting the installation medium" \
     --title "INSTALLATION ORIGIN" \
     --menu \
"Please select the installation medium containing the software packages \
to install Dragora:" 10 54 2 \
     "0" "CDROM" \
     "1" "Local or mounted directory" \
     2> "${SUBTMPDIR}/return-MenuMedia"

    # Check for selected options
    case "$(cat -- "${SUBTMPDIR}"/return-MenuMedia)" in
    0) ## CDROM

        # Scan device name(s) containing the packages to install Dragora
        for name in hda hdb hdc hdd hde hdf hdh sr0 sr1 sr2 sr3 \
                    pcd0 pcd1 pcd2 pcd3 aztcd cdu535 gscd0 \
                    sonycd optcd sjcd mcdx0 mcdx1 sbpcd \
                    cm205cd cm206cd mcd dvd cdrom ; \
        do
            # Prepend path for device files
            name=/dev/${name}

            dialog --colors \
             --backtitle "\\ZbCDROM: Discovering drive name" \
             --title "Searching CDROM drive" --sleep 1 \
             --infobox "Checking device: $name" 3 33

            test -b "$name" || continue

            # Prepare to mount and check the package directory

            if mountpoint -q /media/dragora-packages
            then
                umount /media/dragora-packages
            fi

            echo  "Mounting ${name} under /media/dragora-packages ..."
            sleep 3

            if mount -t iso9660 $name /media/dragora-packages > /dev/null 2>&1
            then
                if test ! -r /media/dragora-packages/packages
                then
                    dialog --colors \
                     --backtitle "\\ZbCDROM: Dragora packages" \
                     --title "CD/DVD Content" \
                     --msgbox \
"The ${name} device was successfully mounted under the\\n\
/media/dragora-packages directory.  However, the contents\\n\
for the Dragora packages were not found.  They are\\n\
read through /media/dragora-packages/packages.\\n\
\\n\
The installer can check for another device name (such as\\n\
a secondary CDROM drive) looking for the package directory\\n\
in order to install Dragora.\\n\
\\n\
Press \\Z3[Enter]\\Zn to proceed..." 14 63 || break 1;
                continue
                else
                    # Save reference directory
                    echo "/media/dragora-packages/packages" \
                     > "${SUBTMPDIR}/MediumFound"

                    # Save device name to eject later
                    echo "$name" > "${SUBTMPDIR}/CDDevice"
                    break 2;
                fi
            fi
        done
        unset -v name

        dialog --colors \
         --backtitle "\\ZbSelecting the installation medium: CDROM (medium not found)" \
         --title "CDROM detection" \
         --ok-label "Return" \
         --msgbox \
"It was not possible to detect the device name for your CDROM drive.\\n\\n\
Please return to the previous menu by pressing \\Z3[Enter]\\Zn in order to\\n\
try by other means..." 9 71
        ;;
    1) ## Local or mounted directory
        while true
        do
            dialog --colors \
             --backtitle "\\ZbSelecting the installation medium: Local or mounted directory" \
             --title "Define valid software directory" \
             --cancel-label "- Back" --inputbox \
"Please enter a directory path containing the packages to\\n\
install Dragora.  Note: the package directory search is\\n\
relative to the specified directory.  For example, if you\\n\
enter \"/var/cache/qi\" the installer will complete the\\n\
path by adding \"/var/cache/qi/packages\"." 12 63 \
             2> "${SUBTMPDIR}/return-MenuMedia_case1" || continue 2;

            answer="$(cat -- "${SUBTMPDIR}"/return-MenuMedia_case1)"
            test -n "$answer" || continue

            if test ! -d "/${answer}/packages"
            then
                dialog --colors \
                 --backtitle "\\ZbSelecting the installation medium: Local or mounted directory" \
                 --title "Directory not found" \
                 --ok-label "Return" --msgbox \
"The \"packages\" subdirectory does not exist\\n\
under the specified directory:\\n\
/${answer#/*}" 8 47
                continue
            fi

            echo "/${answer}/packages" > "${SUBTMPDIR}/MediumFound"
            unset -v answer
            break 2;
        done
        ;;
    esac
done

