#!/bin/sh
#
# Linux (Sat Jul 14 03:54:58 2001)
# Updated for CD writers (Sat Nov 9 22:39:30 2002)
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 --backtitle "Vector Linux ...at the speed of light." --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 --backtitle "Vector Linux ...at the speed of light." --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
}
DONECDW() {
dialog --backtitle "Vector Linux ...at the speed of light." --title "CDWRITER DONE" \
 --msgbox "The CDwriter has been configured. The files on your system have been updated. The configuration involved some changes to the LILO bootloader which means the system will require a reboot for the changes to take place.You can access a cd on CDwriter by doing mount /dev/cdwriter or mount /mnt/cdwriter." 0 0
exit 0
}
  
likemount() {
dialog --backtitle "Vector Linux ...at the speed of sound." --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
  if [ ! $go = "1" ]; then  
  DONE
  fi
dialog --backtitle "Vector Linux ...at the speed of light." --title "ATAPI CD WRITER" \
--yesno "You can choose to search for a ATAPI style CDRW (cdwriter) now. If you have manualy configured this earlier in the configuration process then say NO here, else say YES" 0 0
case $? in
0)

dialog --backtitle "Vector Linux ...at the speed of light." --title "ATAPI CD WRITER $CDW_DEVICE detected" \
--yesno "You have an ATAPI CD Writer $CDW_DEVICE attached to your system. To be able to use an ATAPI CDRW in linux you need to update several configuration files. This program can do the updates automatically. Please choose YES to get the files updated or NO to skip this option." 0 0
case $? in
0)
if [ ! -f /etc/lilo.conf ]; then
dialog --backtitle "Vector Linux ...at the speed of light" --title "No Lilo Config found" \
--msgbox "I couldn't find a lilo.conf file. You need to run the liloconfig program so that we have something to work with. You can run liloconfig from the vasm menu. Please do so then come back and try again." 0 0
exit 0
fi
clear
echo "Updating files......."
sleep 2
grep "ide-scsi" /etc/lilo.conf 1>/dev/null
if [ $? = 1 ]; then
cat /etc/lilo.conf | while read LINE; do
echo "$LINE" >> /etc/lilo.conf.new
if echo $LINE | grep message 1> /dev/null ; then
echo "append = \"$CDW=ide-scsi\"" >> /etc/lilo.conf.new
fi
done
mv /etc/lilo.conf.new /etc/lilo.conf
else
echo "lilo.conf already updated.....skipping."

fi
sleep 1
ls /dev | grep cdwriter 1>/dev/null
if [ $? = 1 ]; then
cd /dev
ln -sf scd0 cdwriter
else
echo "cdwriter links already done....skipping"
fi
sleep 1
grep "/dev/cdwriter" /etc/fstab 1>/dev/null
if [ $? = 1 ]; then
cat /etc/fstab | while read LINE; do
echo $LINE  >> /etc/fstab.new
if echo $LINE | grep "/dev/cdrom" 1> /dev/null; then
echo "/dev/cdwriter  /mnt/cdwriter  iso9660  defaults,noauto,ro,user 0 0" >> /etc/fstab.new
fi
done
mv /etc/fstab.new /etc/fstab
mkdir -p /mnt/cdwriter
/sbin/lilo 1>/dev/null 2>/dev/null
else
echo "Fstab already updated.....skipping"
fi
sleep 1 
  DONECDW
  ;;
1)
echo "Skipping file updates.........."
sleep 1
;;
255)
;;
esac
;;
1)
;;
255)
;;
esac
  exit
}

dialog --backtitle "Vector Linux ...at the speed of light." --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-ROM" 1> /dev/null 2> /dev/null ; then
  CD=`dmesg | grep -F ATAPI | grep -F CD-ROM | cut -f 1 -d ':' | sed -n "1 p"`
  ( cd /dev && ln -sf $CD cdrom )
  CD_DEVICE="/dev/$CD"
  fi

if dmesg | grep -F "scsi CD-ROM sr" 1> /dev/null 2> /dev/null ; then
if [ ! -r /proc/scsi/scsi ]; 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"
  fi
  fi
if dmesg | grep -F ATAPI | grep -F "CD-RW" 1> /dev/null 2> /dev/null ; then
CDW=`dmesg | grep -F ATAPI | grep -F CD-RW | cut -f 1 -d ':' | sed -n "1 p"`
CDW_DEVICE="/dev/$CDW"
fi
if [ ! $CDW_DEVICE = "" ]; then
go=1
likemount
fi 

dialog --backtitle "Vector Linux ...at the speed of light." --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 --backtitle "Vector Linux ...at the speed of light." --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 --backtitle "Vector Linux ...at the speed of light." --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 --backtitle "Vector Linux ...at the speed of light." --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 --backtitle "Vector Linux ...at the speed of light." --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 --backtitle "Vector Linux ...at the speed of light." --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
