#!/bin/bash
# Under the terms of the 'COPYRIGHT.TXT' file.

# Funciones.

# Una funcin para establecer/montar el dispositivo:
set_device() {
  local DRV_FOUND CODE
  DRV_FOUND="$1"
  if [[ -z $DRV_FOUND ]]; then
    return 1;
  fi
  if [[ -b $DRV_FOUND ]]; then
    dialog \
     --title "CD/DVD-ROM" --sleep 1 \
     --infobox "Mounting ${DRV_FOUND}..." 3 26
    umount $DRV_FOUND 2> /dev/null
    umount /cdrom 2> /dev/null
    echo "mounting $DRV_FOUND on /cdrom..." 1> $OUTPUT_TTY
    mount $DRV_FOUND /cdrom 1> $OUTPUT_TTY 2>&1
    CODE=$?
    if (( $CODE == 0 )); then
      echo "$DRV_FOUND" > ${TMP}/SRC_INS
      return 0;
    else
      dialog --title "ERROR" --sleep 1 --infobox \
"Error while trying to mount the \
CD/DVD-ROM (${DRV_FOUND}).\n\n\
mount error code $CODE" 5 60
      return $CODE;
    fi
  else
    dialog \
     --title ":-(" --sleep 2 --infobox "I can't detect the device name." 3 35
    return 2;
  fi
}

# Funcin que comprueba el directorio de paquetes:
check_directory() {
  if [[ ! -d /cdrom/packages ]]; then
    dialog \
     --title "CDROM drive (/dev/${i})" --sleep 2 \
     --infobox "The CDROM that contains the packages was not found." 3 55
    return 1;
  fi
  return 0;
}

# Loop:
while :; do
  dialog \
   --backtitle "Selecting the installation medium" \
   --title "SELECT THE MEDIUM OF INSTALLATION" \
   --menu "Choose the device from which Dragora will be installed:" 8 60 0 \
   "1" "CD/DVD-ROM" \
   "2" "Specify a mounted directory" \
    2> ${TMP}/return-medium
  CODE=$?
  if (( $CODE != 0 )); then
    break;
  fi
  # Mira por las respuestas:
  case "$(cat ${TMP}/return-medium)" in
    1) # CD/DVD-ROM:
      while :; do
        dialog --title "Dragora CD/DVD-ROM" --menu \
"Insert the CD/DVD of Dragora and \
select one from the following options:" 0 0 0 \
         "Auto"   "Auto-detect the device name" \
         "Manual" "Manually specified" \
          2> ${TMP}/return-medium-cd
        if (( $? != 0 )); then
          break;
        fi
        DRIVER=$(cat ${TMP}/return-medium-cd)

        if [[ $DRIVER = Auto ]]; then
          if [[ -f /proc/sys/dev/cdrom/info ]]; then
            arr=( $(awk '/drive name:/{sub(/drive name:/,""); for (i=1; i<=NF; i++) print $i}' /proc/sys/dev/cdrom/info) )
            if (( ${#arr[*]} >= 1 )); then
              for i in "${arr[@]}"; do
                dialog \
                 --title "CDROM drive (/dev/${i})" --sleep 1 \
                 --infobox "Checking the CDROM of Dragora..." 3 36
                if ! set_device "/dev/${i}" ; then
                  # Si no se ha podido detectar el dispositivo
                  # de forma automtica, intentaremos probando:
                  for DEVICE in cdrom dvd 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
                  do
                   echo "Checking /dev/${DEVICE}..."
                   sleep 1
                   if set_device "/dev/${DEVICE}" ; then
                     check_directory && exit 0;
                   fi
                  done
                else
                  check_directory && exit 0;
                fi
              done
            fi
          fi
        fi
        if [[ $DRIVER = Manual ]]; then
          while :; do
            dialog \
             --title "CD/DVD-ROM" --help-button \
             --inputbox "Enter the device name:" 8 36 \
              2> ${TMP}/return-medium-cd-manual
            CODE=$?
            if (( $CODE == 0 )); then
              if set_device "$(cat ${TMP}/return-medium-cd-manual)"; then
                check_directory && exit 0;
              fi
            elif (( $CODE == 2 )); then
              # TODO: dialog --title "CD/DVD-ROMs" --textbox "CDROM-HELP.TXT" 0 0
              dialog \
               --title "SORRY" --sleep 2 \
               --infobox "The help is not available." 3 30
            else
              break;
            fi
          done
        fi
      done
      ;;
    2) # Specify a mounted directory:
      dialog \
       --title "MOUNTED DIRECTORY" \
       --inputbox \
"You need to specify a mounted directory. For example:\n\n\
If you specify \"/var/mnt/hd/foo\", the installer will look \
for the subdirectory \"packages\" (inside of the \"foo\" directory.):" 0 0 \
        2> ${TMP}/return-medium-dir
      if (( $? == 0 )); then
        DIRMNT=$(cut -f1 -d ' ' ${TMP}/return-medium-dir)
        if [[ ! -d ${DIRMNT}/packages ]]; then
          dialog \
           --title "INVALID DIRECTORY" \
           --msgbox "\nThe \"packages\" directory does not exist in:\n\n${DIRMNT}\n\n" 0 0
        else
          echo "$DIRMNT" > ${TMP}/SRC_INS
          break;
        fi
      else
        continue;
      fi
      ;;
  esac
done

exit $CODE

