#!/bin/sh
#
# Linux (Sat Jul 14 03:54:58 2001)
# Updated for CD writers (Sat Nov 9 22:39:30 2002)
#
# Change permission automatically, by Kocil


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 [ ! $DISPLAY = "" ]; then
DCMD="Xdialog --wrap" 
CMD="rxvt -e "
else
DCMD="dialog"
CMD=""
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 && chmod a+r $DEVICE)
$DCMD --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() {
$DCMD --title "DONE!" \
 --msgbox "The CDROM/DVDROM has been configured. The files on your system have been updated." 0 0
echo
exit
}
DONECDW() {
$DCMD --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
}

cdwmount () {

$DCMD --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
$DCMD --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. You can edit /etc/lilo.conf and add 
append = \"$CDW_DEVICE=ide-scsi\" near the top of the file or return and rerun this program." 0 0
LILO=NO
else
LILO=YES
fi
if [ $LILO = "YES" ]; then

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 "boot =" 1> /dev/null ; then
echo "append = \"$CDW_DEV=ide-scsi\"" >> /etc/lilo.conf.new
fi
done
mv /etc/lilo.conf.new /etc/lilo.conf
else
echo "lilo.conf already updated.....skipping."
sleep 2

fi
fi
sleep 1
ls /dev | grep cdwriter 1>/dev/null
if [ $? = 1 ]; then
cd /dev
ln -sf scd0 cdwriter
chmod a+rw scd0
else
echo "cdwriter links already done....skipping"
sleep 2
fi
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  subfs fs=auto,ro  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"
sleep 2
fi

 
DONECDW
;;
1)
DONE
;;
255)
exit
;;
esac 
}  

likemount() {
if [ $DVD = "yes" ]; then
$DCMD --title "DVD-ROM device $DVD_DEVICE detected!" --msgbox "A symbolic link \
  /dev/cdrom and /dev/dvd have been created pointing to $DVD_DEVICE, which is the actual device \
  for your DVD-ROM drive." 7 66
   clear
   echo
   echo "Updating Fstab..."
   grep "/dev/cdrom" /etc/fstab 1>/dev/null
   if [ ! $? = 0 ]; then
   echo "/dev/cdrom  /mnt/cdrom  subfs fs=auto,ro,user 0 0" >> /etc/fstab
   mkdir -p /mnt/cdrom
   else
   echo "Fstab already updated.....skipping"
   fi
   fi
   
if [ ! "$CDW" = "yes" ]; then
$DCMD --title "unknown" --yesno \
"We have found a CD device on your system but were unable to determine if its a standard CD
or a CDWRITER. Please indicate if $CD_DEVICE is a CDWRITER or a standard CDROM. Please enter YES if
it's a CDWRITER and NO if a standard CDROM." 0 0
if [ $? = 0 ]; then
CDW_DEVICE=$CD_DEVICE
cdwmount
fi
fi

$DCMD --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
   clear
   echo
   echo "Updating Fstab..."
   grep "/dev/cdrom" /etc/fstab 1>/dev/null
   if [ ! $? = 0 ]; then
   echo "/dev/cdrom  /mnt/cdrom  subfs fs=auto,ro,user 0 0" >> /etc/fstab
   mkdir -p /mnt/cdrom
   else
   echo "Fstab already updated.....skipping"
   fi
   ( cd /dev && ln -sf $CD_DEVICE cdrom && chmod a+r $CD_DEVICE)
   if [ $CDW = yes ]; then
   cdwmount
   else  
  DONE
fi
  }

$DCMD --title "CDROM/DVDROM CONFIGURATION" --yesno \
"Cdset will create a symbolic link /dev/cdrom that will point   
to your actual cdrom/dvdrom 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

for i in hda hdb hdc hdd ; do
cat /proc/ide/$i/media | grep -F cdrom >/dev/null
if [ $? = 0 ]; then
if dmesg | grep -F "CD-RW" | grep -F "ATAPI" | grep -F $i >/dev/null ; then
CDW_DEVICE=/dev/$i
CDW_DEV=$i
CDW=yes
elif dmesg | grep -F "CD-W" | grep -F "ATAPI" | grep -F $i >/dev/null ; then
CDW_DEVICE=/dev/$i
CDW_DEV=$i
CDW=yes
else
CD_DEVICE=/dev/$i
if dmesg | grep -F "IDE DVD-ROM" | grep -F $i >/dev/null ; then
DVD_DEVICE=/dev/$i
DVD=yes
( cd /dev && ln -sf $DVD_DEVICE dvd && ln -sf $DVD_DEVICE cdrom && chmod a+r $DVD_DEVICE) 

fi
fi
fi
done


if dmesg | grep -F "scsi CD-ROM sr" 1> /dev/null 2> /dev/null ; then
dmesg | grep -F "ide-scsi" 1> /dev/null 2> /dev/null
if [ ! $? = 0 ]; 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 && chmod a+r $CD )
  CD_DEVICE="/dev/$CD"
  fi
  fi

if [ ! $CD_DEVICE = "" ]; then
likemount
fi 

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

$DCMD --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 && chmod a+r $CD_DEVICE)
 rm -f /tmp/idecd
 DONE
 else
 
$DCMD --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
   
$DCMD --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
$DCMD --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
$DCMD --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 && chmod a+r $CD_DEVICE )
fi
umount /tmp/cdrom 2>/dev/null
rmdir /tmp/cdrom 2>/dev/null 
rm -f /tmp/idecd
DONE
