#!/bin/sh
#
# Linux (4-4-2000)
#
if [ ! "$UID" = "0" ]; then
echo
echo "[$LOGNAME] You need to run this script as root."
echo "Try one of these to run it as root: # sudo cdset"
echo "                                    # su -c cdset"
echo
exit 
fi

if ! type -all dialog >/dev/null 2>&1 ; then
echo "Can't find 'dialog', i can't run without 'dialog' on your system."
exit 
fi

if mount | grep -F iso9660 1>/dev/null 2>/dev/null ; then # CD is mounted
  DEVICE=`mount | grep -F iso9660 | cut -d ' ' -f 1`
  ( cd /dev && ln -sf $DEVICE cdrom )
  dialog --title "CDROM DRIVE MOUNTED!" --msgbox "The cdrom drive\
 is mounted, there's a link: /dev/cdrom -> $DEVICE\n\
If you want to change the link, unmount the cdrom drive first.\n 
umount /dev/cdrom" 8 76
echo
exit
fi

DONE() {
  dialog --title "DONE!" --msgbox "You can put this\
 entry in the /etc/fstab file for your cdrom drive.\n\
/dev/cdrom  /mnt/cdrom  iso9660  noauto,user,ro  0  0\n\n\
You can then mount it like this.\n\
# mount /mnt/cdrom" 9 72
echo
exit
}
  
likemount() {
  dialog --title "CD-ROM device $CD_DEVICE detected!" --msgbox "A symbolic link \
  /dev/cdrom has been created pointing to $CD_DEVICE, which is the actual device \
  for your CD-ROM drive." 7 66  
  DONE
}

dialog --title "CDROM CONFIGURATION" --yesno \
"Cdset will create a symbolic link /dev/cdrom that will point   
to your actual cdrom device (hdx scdx etc.).\n  
You can change this link later if you have your cdrom drive  
on a different device." 9 66
if [ $? -gt 0 ]; then
echo
exit 
fi

if dmesg | grep -F ATAPI | grep -F CD 1> /dev/null 2> /dev/null ; then
  CD=`dmesg | grep -F ATAPI | grep -F CD | cut -f 1 -d ':' | sed -n "1 p"`
  ( cd /dev && ln -sf $CD cdrom && chmod 644 cdrom)
  CD_DEVICE="/dev/$CD"
  likemount 
fi

if dmesg | grep -F "scsi CD-ROM sr" 1> /dev/null 2> /dev/null ; then
  N=`dmesg | grep -F "scsi CD-ROM sr" | cut -d ' ' -f 4 | sed -n "1 p" | cut -b3-4`
  CD=scd$N  
  ( cd /dev && ln -sf $CD cdrom )
  CD_DEVICE="/dev/$CD"
  likemount 
fi

dialog --title "CONFIGURE CD-ROM DRIVE?" --yesno "\n  Do you have a CD-ROM DRIVE?" 7 35
if [ $? = 1 -o $? = 255 ]; then
 echo 
 exit
fi

 dialog --title "CD-ROM DEVICE SELECTION" --menu \
 "Please select your CD-ROM device from the list below. \
 If you don't know or see your device listed, choose 'scan'." \
 18 70 9 \
 "scan" "Try to scan for your cdrom drive" \
 "/dev/hda" "CD-ROM master on first IDE bus (unlikely)" \
 "/dev/hdb" "CD-ROM slave on first IDE bus" \
 "/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/idecd

 if [ ! "`cat /tmp/idecd`" = "scan" ]; then
 CD_DEVICE="`cat /tmp/idecd`"
 ( cd /dev && ln -sf $CD_DEVICE cdrom )
 rm -f /tmp/idecd
 DONE
 else
 
 dialog --title "PLACE DISC IN CD-ROM DRIVE" --msgbox \
 "Make sure you have a disc in your CD-ROM drive (the scan will fail \
 without one) and then press ENTER to begin the scanning process." \
 7 60

   mkdir -p /tmp/cdrom

   for device in \
  /dev/hdd /dev/hdc /dev/hdb /dev/hda \
  /dev/hde /dev/hdf /dev/hdg /dev/hdh \
  /dev/scd0 /dev/scd1 /dev/scd2 /dev/scd3 \
  /dev/pcd0 /dev/pcd1 /dev/pcd2 /dev/pcd3 \
  /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
   
  dialog --infobox "Scanning $device ..." 3 30
   mount -o ro -t iso9660 $device /tmp/cdrom 1>/dev/null 2>/dev/null
   if [ $? = 0 ]; then
    DRIVE_FOUND=$device
    break
   fi
  done
  if [ "$DRIVE_FOUND" = "" ]; then
   dialog --title "DRIVE NOT FOUND" --msgbox \
"An IDE CD-ROM drive could not be found on any of the devices that \
were scanned." 6 55
rmdir /tmp/cdrom 
rm -f /tmp/idecd
echo
exit
  else
   dialog --title "IDE CD-ROM DRIVE DETECTED SUCCESSFULLY" --msgbox \
"An IDE CD-ROM drive was found on device $DRIVE_FOUND, \
a symbolic link /dev/cdrom will point to it." 6 65
   CD_DEVICE="$DRIVE_FOUND"
  fi
 fi
if [ ! "$CD_DEVICE" = "" ]; then
( cd /dev && ln -sf $CD_DEVICE cdrom )
fi
umount /tmp/cdrom 2>/dev/null
rmdir /tmp/cdrom 2>/dev/null 
rm -f /tmp/idecd
DONE
