#!/bin/sh
RDIR=/dev/tty4
NDIR=/dev/null
TMP=/var/log/setup/tmp
if [ ! -d $TMP ]; then
  mkdir -p $TMP
fi
T_PX="`cat $TMP/SeTT_PX`"
rm -f $TMP/SeTmount $TMP/SeTDS $TMP/SeTCDdev $TMP/reply

dialog --title "SCAN FOR CD-ROM DRIVE?" --menu \
"Would you like to have Setup scan for your drive automatically \
(recommended), or would you like to choose your CD-ROM drive \
manually from a list of devices?" \
11 70 2 \
"auto" "Scan for the CD-ROM drive automatically" \
"manual" "Manually select CD-ROM device" \
2> $TMP/reply
if [ ! -r $TMP/reply ]; then
 # cancel or esc
 rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTCDdev $TMP/errordo
 exit
fi

#***********************
# testing hack:
#echo "auto" > $TMP/reply
#***********************

if [ "`cat $TMP/reply`" = "manual" ]; then
 dialog --title "MANUAL CD-ROM DEVICE SELECTION" --menu \
 "Please select your CD-ROM device from the list below.  \
If you don't see your device listed, choose 'custom'.  \
This will let you type in any device name. (and if necessary, \
will create the device)" \
 18 70 9 \
 "custom" "Type in the CD-ROM device to use" \
 "/dev/hdb" "CD-ROM slave on first IDE bus" \
 "/dev/hda" "CD-ROM master on first IDE bus (unlikely)" \
 "/dev/hdc" "CD-ROM master on second IDE bus" \
 "/dev/hdd" "CD-ROM slave on second IDE bus" \
 "/dev/hde" "CD-ROM master on third IDE bus" \
 "/dev/hdf" "CD-ROM slave on third IDE bus" \
 "/dev/hdg" "CD-ROM master on fourth IDE bus" \
 "/dev/hdh" "CD-ROM slave on fourth IDE bus" \
 "/dev/scd0" "First SCSI CD-ROM drive" \
 "/dev/scd1" "Second SCSI CD-ROM drive" \
 "/dev/scd2" "Third SCSI CD-ROM drive" \
 "/dev/scd3" "Fourth SCSI CD-ROM drive" \
 "/dev/pcd0" "First parallel port ATAPI CD" \
 "/dev/pcd1" "Second parallel port ATAPI CD" \
 "/dev/pcd2" "Third parallel port ATAPI CD" \
 "/dev/pcd3" "Fourth parallel port ATAPI CD" \
 "/dev/aztcd" "Non-IDE Aztech CD-ROM" \
 "/dev/cdu535" "Sony CDU-535 CD-ROM" \
 "/dev/gscd" "Non-IDE GoldStar CD-ROM" \
 "/dev/sonycd" "Sony CDU-31a CD-ROM" \
 "/dev/optcd" "Optics Storage CD-ROM" \
 "/dev/sjcd" "Sanyo non-IDE CD-ROM" \
 "/dev/mcdx0" "Non-IDE Mitsumi drive 1" \
 "/dev/mcdx1" "Non-IDE Mitsumi drive 2" \
 "/dev/sbpcd" "Old non-IDE SoundBlaster CD-ROM" \
 "/dev/cm205cd" "Philips LMS CM-205 CD-ROM" \
 "/dev/cm206cd" "Philips LMS CM-206 CD-ROM" \
 "/dev/mcd" "Old non-IDE Mitsumi CD-ROM" \
 2> $TMP/reply
 if [ ! -r $TMP/reply ]; then
  # cancel or esc
  rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTCDdev $TMP/errordo
  exit
 fi
 REPLY="`cat $TMP/reply`"
 if [ "$REPLY" = "custom" ]; then
  dialog --title "ENTER CD-ROM DEVICE MANUALLY" --inputbox \
"Please enter the name of the CD-ROM device (such as /dev/hdc) that \
you wish to use to mount the Slackware CD-ROM:" \
9 70 2> $TMP/reply
  if [ ! -r $TMP/reply ]; then
   # cancel or esc
   rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTCDdev $TMP/errordo
   exit
  fi
  DRIVE_FOUND="`cat $TMP/reply`"
  if [ ! -r $DRIVE_FOUND ]; then # no such device
   rm -f $TMP/majorminor
   dialog --title "MKNOD CD-ROM DEVICE" --inputbox \
   "There doesn't seem to be a device by the name of $DRIVE_FOUND in the \
/dev directory, so we will have to create it using the major and minor \
numbers for the device.  If you're using a bootdisk with a custom CD-ROM \
driver on it, you should be able to find these numbers in the \
documentation.  Also, see the 'devices.txt' file that comes with the \
Linux kernel source.  If you don't know the numbers, you'll have to hit \
Esc to abort. Enter the major and minor numbers for the new device, \
separated by one space:" \
   15 72 2> $TMP/majorminor
   if [ ! -r $TMP/majorminor ]; then
    # cancel or esc
    rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTCDdev $TMP/errordo
    exit
   fi
   MAJOR="`cat $TMP/majorminor`"
   dialog --title "MAKING DEVICE IN /dev" --infobox \
   "mknod $DRIVE_FOUND b $MAJOR" 3 40
   mknod $DRIVE_FOUND b $MAJOR 2> $RDIR
   sleep 3
   if [ ! -r $DRIVE_FOUND ]; then
    dialog --title "MKNOD FAILED" --msgbox \
    "Sorry, but the mknod command failed to make the device.  You'll need to \
go back and try selecting your source media again.  Press ENTER to abort \
the source media selection process." 8 60
    rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTCDdev $TMP/errordo
    exit
   fi
  fi
 else
  DRIVE_FOUND=$REPLY
 fi
fi

# Search the IDE interfaces:
if [ "$DRIVE_FOUND" = "" ]; then

 dialog --title "PLACE DISC IN CD-ROM DRIVE" --msgbox \
 "Make sure the Slackware CD-ROM is in your CD-ROM drive, \
and then press ENTER \
to begin the scanning process." \
 6 60

 dialog --title "SCANNING" --infobox "Scanning for an IDE/ATAPI \
CD-ROM drive containing a Slackware CD-ROM..." 4 56
 sleep 3
 for device in \
  /dev/hdd /dev/hdc /dev/hdb /dev/hda \
  /dev/hde /dev/hdf /dev/hdg /dev/hdh \
  ; do
  mount -o ro -t iso9660 $device /var/log/mount 1> $RDIR 2> $RDIR
  if [ $? = 0 ]; then
   DRIVE_FOUND=$device
   umount /var/log/mount
   break
  fi
 done
fi

# Search for SCSI CD-ROM drives:
if [ "$DRIVE_FOUND" = "" ]; then
 dialog --title "SCANNING" --infobox "Scanning for a SCSI \
CD-ROM drive containing a Slackware CD-ROM..." 4 50
 sleep 3
 for device in \
  /dev/scd0 /dev/scd1 /dev/scd2 /dev/scd3 \
  ; do
  mount -o ro -t iso9660 $device /var/log/mount 1> $RDIR 2> $RDIR
  if [ $? = 0 ]; then
   DRIVE_FOUND=$device
   umount /var/log/mount
   break
  fi
 done
fi

# Search for parallel port ATAPI CD-ROM drives:
if [ "$DRIVE_FOUND" = "" ]; then
 dialog --title "SCANNING" --infobox "Scanning for a parallel port \
ATAPI CD-ROM drive containing a Slackware CD-ROM..." 4 52
 sleep 3
 for device in \
  /dev/pcd0 /dev/pcd1 /dev/pcd2 /dev/pcd3 \
  ; do
  mount -o ro -t iso9660 $device /var/log/mount 1> $RDIR 2> $RDIR
  if [ $? = 0 ]; then
   DRIVE_FOUND=$device
   umount /var/log/mount
   break
  fi
 done
fi

# Still not found?  OK, we will search for CD-ROM drives on old, pre-ATAPI
# proprietary interfaces.  There aren't too many of these still around, and
# the scan won't actually work unless a bootdisk that supports the drive is
# used, and any necessary parameters have been passed to the kernel.
if [ "$DRIVE_FOUND" = "" ]; then
 dialog --title "SCANNING" --msgbox "No IDE/SCSI drive, so we will try \
scanning for CD-ROM drives on \
old proprietary interfaces, such as SoundBlaster pre-IDE CD-ROM drives, \
Sony CDU-31a, Sony 535, old Mitsumi pre-IDE, old Optics, etc.  For this \
scan to work at all, you'll need to be using a bootdisk that supports \
your CD-ROM drive.  Please press ENTER to begin this last-chance scan \
for old, obsolete hardware." 11 60
 for device in \
  /dev/sonycd /dev/gscd /dev/optcd /dev/sjcd /dev/mcdx0 /dev/mcdx1 \
  /dev/cdu535 /dev/sbpcd /dev/aztcd /dev/cm205cd /dev/cm206cd \
  /dev/bpcd /dev/mcd \
  ; do
  mount -o ro -t iso9660 $device /var/log/mount 1> $RDIR 2> $RDIR
  if [ $? = 0 ]; then
   DRIVE_FOUND=$device
   umount /var/log/mount
   break
  fi
 done
fi

if [ "$DRIVE_FOUND" = "" ]; then
 dialog --title "CD-ROM DRIVE NOT FOUND" --msgbox \
 "A CD-ROM drive could not be found on any of the devices that were \
scanned.  Possible reasons include using a bootdisk or kernel that \
doesn't support your drive, failing to pass parameters needed by some \
drives to the kernel, not having the Slackware CD-ROM in your CD-ROM \
drive, or using a drive connected to a Plug and Play soundcard (in this \
case, connecting the drive directly to the IDE interface often helps). \
Please make sure you are using the correct bootdisk for your hardware, \
consult the BOOTING file for possible information on \
forcing the detection of your drive, and then reattempt installation.  \
If all else fails, see FAQ.TXT for information about copying \
parts of this CD to your DOS partition and installing it from there.\n\
\n\
You will now be returned to the main menu.  If you want to try looking \
for the CD again, you may skip directly to the SOURCE menu selection." \
 0 0
 rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTCDdev $TMP/errordo
 exit
fi

while [ 0 ]; do # time to try to mount the CD-ROM device:
 mount -o ro -t iso9660 $DRIVE_FOUND /var/log/mount 1> $RDIR 2> $RDIR
 if [ $? = 0 ]; then # mounted successfully
  dialog --title "CD-ROM DRIVE FOUND" --sleep 3 --infobox "A CD-ROM was found in device $DRIVE_FOUND." 3 47
  break
 else # failed to mount
  dialog --title "MOUNT ERROR" --menu "There was an error \
attempting to mount the CD-ROM on $DRIVE_FOUND.  \
Maybe the device name is incorrect, the disc is not in the \
drive, or the kernel you are using does not support the \
device. (possibly an incorrect bootdisk) \
What would you like to do now?" \
  13 70 3 \
  "1" "Abort CD installation" \
  "2" "Attempt to mount the CD-ROM device again" \
  "3" "Ignore the error and continue" 2> $TMP/errordo
  if [ ! $? = 0 ]; then
   rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTCDdev $TMP/errordo
   exit;
  fi
  DOWHAT="`cat $TMP/errordo`"
  rm -f $TMP/errordo
  if [ $DOWHAT = 1 ]; then
   rm -f $TMP/SeTDS $TMP/SeTmount $TMP/SeTCDdev
   exit
  elif [ $DOWHAT = 2 ]; then
   dialog --title "ATTEMPTING TO MOUNT CD-ROM on $DRIVE_FOUND" \
   --infobox "Attempting to mount your CD-ROM again with the \
following command: mount -o ro -t iso9660 $DRIVE_FOUND \
/var/log/mount" 4 69 
   umount /var/log/mount 1> $NDIR 2> $NDIR
   sleep 5
  elif [ $DOWHAT = 3 ]; then
   # Ignore error, really bad plan but whatever
   break
  fi
 fi
done
# since we should have mounted successfully, let's refresh 
# the link for /dev/cdrom on the target partition:
if [ ! -d $T_PX/dev ]; then
 mkdir $T_PX/dev
 chown root.root $T_PX/dev
 chmod 755 $T_PX/dev
fi
( cd $T_PX/dev ; ln -sf $DRIVE_FOUND cdrom )

# Pass information about the source directory to the next install stage:
echo "/var/log/mount/slackware" > $TMP/SeTDS
echo "-source_mounted" > $TMP/SeTmount
echo "$DRIVE_FOUND" > $TMP/SeTCDdev
echo "/dev/null" > $TMP/SeTsource
