#!/bin/sh
# @vasm : CD
# @level: root
# @description: Setup CDROM/CDWRITER and enable it for users
#
# Linux (Sat Jul 14 03:54:58 2001)
# Updated for CD writers (Sat Nov 9 22:39:30 2002)
# 2004-01-01: Kocil
#   - fix broken conditional expressions (not all though !)
#   - fix grep checking on updating /etc/fstab
#   - Add allowing users access for CD and CD WRITER
#
# THIS SCRIPT REALLY NEED UPDATING !

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 "
BACKTITLE=""
else
DCMD="dialog"
CMD=""
BACKTITLE="VLocity Linux ... at the speed of light"
BACKTITLE1="VLocity Linux ... at the speed of sound"
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; rm -f cdrom; ln -sf $DEVICE cdrom )
$DCMD --backtitle "$BACKTITLE" --title "CDROM DRIVE MOUNTED!" --msgbox \
"\nThe 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 --backtitle "$BACKTITLE." --title "CDROM/DVDROM USERS PERMISSION" --yesno \
"\nWould you permit ordinary users to use the CDROM/DVDROM ?
Answer yes will enable them to play Audio CD, Video CD, or DVD 
using XMMS, Xine, MPlayer etc. Otherwise, they must bother you, 
the great root, each time they want a little fun " 0 0
if [ $? = 0 ]; then
  chmod a+r $1
else
  chmod o-r $1  
fi
$DCMD --backtitle "$BACKTITLE" --title "HOORAY DONE!" --msgbox \
"\nThe CDROM/DVDROM has been configured.\nThe system files have been updated.
Enjoy ..." 0 0 
exit
}

DONECDW() {
$DCMD --backtitle "$BACKTITLE" --title "CDWRITER USERS PERMISSION" --yesno \
"\nWould you permit ordinary users to use the CD WRITER ?
Answer yes so they can run program like XCDRoast by themselves.\n
No will restrict the access for root only" 9 66
if [ $? -gt 0 ]; then
  chmod a+rw $1
else
  chmod o-rw $1    
fi
$DCMD --backtitle "$BACKTITLE" --title "CDWRITER DONE" --msgbox \
"\nThe 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 --backtitle "$BACKTITLE" --title "ATAPI CD WRITER $CDW_DEVICE detected" --yesno \
"\nYou 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 --backtitle "$BACKTITLE" --title "No Lilo Config found" --msgbox \
"\nI 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; rm -f cdwriter; ln -sf scd0 cdwriter)
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  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"
sleep 2
fi
DONECDW $CDW_DEVICE
;;
1)
DONE $CDW_DEVICE
;;
255)
exit
;;
esac 
}  

likemount() {
if [ "$DVD" = "yes" ]; then
$DCMD --backtitle "$BACKTITLE" --title "DVD-ROM device $DVD_DEVICE detected!" --msgbox \
"\nA 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 -e "^/dev/cdrom" /etc/fstab 1>/dev/null
   if [ ! $? = 0 ]; then
      echo "/dev/cdrom  /mnt/cdrom  iso9660  defaults,noauto,ro,user 0 0" >> /etc/fstab
      mkdir -p /mnt/cdrom
   else
      echo "Fstab already updated.....skipping"
   fi
fi
   
if [ ! "$CDW" = "yes" ]; then
  $DCMD --backtitle "$BACKTITLE" --title "ADDITIONAL CHECK FOR CDWRITER" --yesno \
"\nWe 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. 
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 --backtitle "$BACKTITLE" \
--title "CD-ROM device $CD_DEVICE detected!" --msgbox \
"\nA symbolic link /dev/cdrom has been created pointing to $CD_DEVICE, 
which is the actual device  for your CD-ROM drive.
" 0 0

clear
echo
echo "Updating Fstab..."
grep -e "^/dev/cdrom" /etc/fstab 1>/dev/null
if [ ! $? = 0 ]; then
   echo "/dev/cdrom  /mnt/cdrom  iso9660  defaults,noauto,ro,user 0 0" >> /etc/fstab
   mkdir -p /mnt/cdrom
else
   echo "Fstab already updated.....skipping"
fi
( cd /dev ; rm -f cdrom; ln -sf $CD_DEVICE cdrom )
if [ "$CDW" = "yes" ]; then
   cdwmount
else  
  DONE $CD_DEVICE
fi
}

$DCMD --backtitle "$BACKTITLE" \
--title "CDROM/DVDROM CONFIGURATION" --yesno \
"\nCdset will create a symbolic link /dev/cdrom that will point   
to your actual cdrom/dvdrom device (hdx scdx etc.).  
You may change this link later if you have your cdrom drive on a different device.
" 0 0
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; rm -f dvd; ln -sf $DVD_DEVICE dvd)
( cd /dev; rm -f cdrom; ln -sf $DVD_DEVICE cdrom ) 

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; rm -f cdrom; ln -sf $CD cdrom )
  CD_DEVICE="/dev/$CD"
  fi
  fi

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

$DCMD --backtitle "$BACKTITLE" \
--title "CONFIGURE CD-ROM DRIVE?" --yesno \
"
Excuse me, do you have a CD-ROM DRIVE ?
" 0 0
if [ $? = 1 -o $? = 255 ]; then
 echo 
 exit
fi

$DCMD --backtitle "$BACKTITLE" \
--title "CD-ROM DEVICE SELECTION" --menu \
"\nAllright. 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 ; rm -f cdrom; ln -sf $CD_DEVICE cdrom )
 rm -f /tmp/idecd
 DONE $CD_DEVICE
else
 
$DCMD --backtitle "$BACKTITLE" \
--title "PLACE DISC IN CD-ROM DRIVE" --msgbox \
"\nMake sure you have a disc in your CD-ROM drive 
(the scan will fail without one) 
and then press ENTER to begin the scanning
" 0 0

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 --backtitle "$BACKTITLE1" \
--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 --backtitle "$BACKTITLE1" \
--title "DRIVE NOT FOUND" --msgbox \
"\nI'm sorry, but an IDE CD-ROM drive could not be found 
on any of the devices that were scanned.
You are on your own now ...
" 6 55
rmdir /tmp/cdrom 
rm -f /tmp/idecd
echo
exit
  else
$DCMD --backtitle "$BACKTITLE" \
--title "IDE CD-ROM DRIVE DETECTED SUCCESSFULLY" --msgbox \
"\nAn 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 ; rm -f cdrom; ln -sf $CD_DEVICE cdrom )
fi
umount /tmp/cdrom 2>/dev/null
rmdir /tmp/cdrom 2>/dev/null 
rm -f /tmp/idecd
DONE $CD_DEVICE
