#!/bin/sh
# If we are using udev, we should set up some extra symbolic links
# for the optical drives as specified in the FHS.  The other way
# to do this when using udev (this file is not used unless udev and
# a 2.6.x or newer kernel is running), is to add a new rule such as
# /etc/udev/rules.d/cdrom.rules containing (uncommented, of course):
#

#BUS=="ide", KERNEL=="*[!0-9]", ENV{PHYSDEVDRIVER}=="ide-cdrom", NAME="%k", GROUP="cdrom", MODE="0660", RUN+="/lib/udev/cdrom-symlinks.sh %k"

# This will create rules in /etc/udev/rules which you should then
# uncomment and/or edit to your liking.

# Patrick Volkerding is mostly to blame for this script.  ;-)

# Copyright 2006  Patrick Volkerding, Sebeka, Minnesota, USA
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if cat /proc/mounts | grep -w sysfs 1> /dev/null 2> /dev/null ; then
  if ! grep -w nohotplug /proc/cmdline 1> /dev/null 2> /dev/null ; then
    if [ -x /etc/rc.d/rc.udev ]; then
      if [ -z $udev_root ]; then
        . /etc/udev/udev.conf
      fi
      
      # If you already have a /dev/cdrom symlink we will assume that you've either
      # made your own symlink or you are running a 2.4 kernel.  In such cases this
      # script will make no changes to your /dev (or ${udev_root}, or whatever...)

      if [ ! -L ${udev_root}/cdrom ]; then
        # Make extra udev optical drive symlinks:
        ( cd $udev_root
          DRIVE_NUMBER=0
          /bin/ls -l --full-time . | grep -w cdrom | cut -f 3 -d : | cut -f 3 -d ' ' | while read optical_device ; do
            if [ -z "$optical_device" ]; then
              break;
            else
              # Gather this device's capabilities with cdrom_id:
              CDROM_ID_OUTPUT="$(/lib/udev/cdrom_id --export ${udev_root}${optical_device})"
              if [ -z "$CDROM_ID_OUTPUT" ]; then
                # No drive capabilities reported, skipping to the next device:
                continue;
              else
                # Set the capability variables for the loop below:
                let $CDROM_ID_OUTPUT
              fi

              for drivecap in ID_CDROM ID_CDROM_CD_R ID_CDROM_CD_RW ID_CDROM_DVD ID_CDROM_DVD_R ID_CDROM_MRW ID_CDROM_MRW_W ID_CDROM_RAM ; do
                # Some of these we ignore, and others we aren't entirely accurate about,
                # like using ${udev_root}cdrw whether it's rewritable or not.  I think
                # the fewer devices there are the simpler it is for everyone.

                # Make numbered cdrom device, and one cdrom symlink:
                if [ $drivecap = ID_CDROM -a $[$drivecap] = 1 ]; then
                  if [ ! -L cdrom${DRIVE_NUMBER} ]; then
                    ln -sf ${optical_device} cdrom${DRIVE_NUMBER}
                  fi
                  if [ ! -L cdrom ]; then
                    ln -sf cdrom${DRIVE_NUMBER} cdrom
                  fi
                fi

                # Make numbered cdr device, one cdr symlink, and a cdrom symlink if none exists yet:
                if [ $drivecap = ID_CDROM_CD_R -a $[$drivecap] = 1 ]; then
                  if [ ! -L cdr${DRIVE_NUMBER} ]; then
                    ln -sf ${optical_device} cdr${DRIVE_NUMBER}
                  fi
                  if [ ! -L cdr ]; then
                    ln -sf cdr${DRIVE_NUMBER} cdr
                  fi
                  if [ ! -L cdrom ]; then
                    ln -sf cdr${DRIVE_NUMBER} cdrom
                  fi
                fi

                # Make numbered cdrw device, one cdrw symlink, and a cdrom symlink if none exists yet:
                if [ $drivecap = ID_CDROM_CD_RW -a $[$drivecap] = 1 ]; then
                  if [ ! -L cdrw${DRIVE_NUMBER} ]; then
                    ln -sf ${optical_device} cdrw${DRIVE_NUMBER}
                  fi
                  if [ ! -L cdrw ]; then
                    ln -sf cdrw${DRIVE_NUMBER} cdrw
                  fi
                  if [ ! -L cdrom ]; then
                    ln -sf cdrw${DRIVE_NUMBER} cdrom
                  fi
                fi

                # Make numbered dvd device, one dvd symlink, and a cdrom symlink if none exists yet:
                if [ $drivecap = ID_CDROM_DVD -a $[$drivecap] = 1 ]; then
                  if [ ! -L dvd${DRIVE_NUMBER} ]; then
                    ln -sf ${optical_device} dvd${DRIVE_NUMBER}
                  fi
                  if [ ! -L dvd ]; then
                    ln -sf dvd${DRIVE_NUMBER} dvd
                  fi
                  if [ ! -L cdrom ]; then
                    ln -sf dvd${DRIVE_NUMBER} cdrom
                  fi
                fi

                # Make numbered dvdrw device, one dvdrw symlink, and a cdrom symlink if none exists yet:
                if [ $drivecap = ID_CDROM_DVD_R -a $[$drivecap] = 1 ]; then
                  if [ ! -L dvdrw${DRIVE_NUMBER} ]; then
                    ln -sf ${optical_device} dvdrw${DRIVE_NUMBER}
                  fi
                  if [ ! -L dvdrw ]; then
                    ln -sf dvdrw${DRIVE_NUMBER} dvdrw
                  fi
                  if [ ! -L cdrom ]; then
                    ln -sf dvdrw${DRIVE_NUMBER} cdrom
                  fi
                fi

              done
              unset ID_CDROM ID_CDROM_CD_R ID_CDROM_CD_RW ID_CDROM_DVD ID_CDROM_DVD_R ID_CDROM_MRW ID_CDROM_MRW_W ID_CDROM_RAM
              DRIVE_NUMBER=$(expr $DRIVE_NUMBER + 1)
            fi
          done
        )
      fi
    fi
  fi
fi

# We should at least try to have a /dev/cdrom and /dev/cdwriter that are on different devices, right?
if [ -L ${udev_root}cdrom0 -a -L ${udev_root}cdrw1 ]; then
  ( cd ${udev_root}
    rm -f cdrom cdwriter
    ln -sf cdrom0 cdrom
    ln -sf cdrw1 cdwriter
  )
elif [ -L ${udev_root}cdrw0 -a -L ${udev_root}cdrom1 ]; then
  ( cd ${udev_root}
    rm -f cdrom cdwriter
    ln -sf cdrom1 cdrom
    ln -sf cdrw0 cdwriter
  )
elif [ -L ${udev_root}cdrom0 -a -L ${udev_root}cdr1 ]; then
  ( cd ${udev_root}
    rm -f cdrom cdwriter
    ln -sf cdrom0 cdrom
    ln -sf cdr1 cdwriter
  )
elif [ -L ${udev_root}cdr0 -a -L ${udev_root}cdrom1 ]; then
  ( cd ${udev_root}
    rm -f cdrom cdwriter
    ln -sf cdrom1 cdrom
    ln -sf cdr0 cdwriter
  )
else
  # If there is no suitable combination among drives 0 and 1, we will at
  # least try to add a cdwriter link if possible:
  if [ -L ${udev_root}cdrw1 ]; then
    ( cd ${udev_root} ; ln -sf cdrw1 cdwriter )
  elif [ -L ${udev_root}cdrw0 ]; then
    ( cd ${udev_root} ; ln -sf cdrw0 cdwriter )
  elif [ -L ${udev_root}cdr1 ]; then
    ( cd ${udev_root} ; ln -sf cdr1 cdwriter )
  elif [ -L ${udev_root}cdr0 ]; then
    ( cd ${udev_root} ; ln -sf cdr0 cdwriter )
  fi
fi

# Although /dev/dvdwriter is not on www.lanana.org's device list, I've seen
# it used before, so I think a similar arrangement with /dev/dvd and
# /dev/dvdwriter would be a good thing:
if [ -L ${udev_root}dvd0 -a -L ${udev_root}dvdrw1 ]; then
  ( cd ${udev_root}
    rm -f dvd dvdwriter
    ln -sf dvd0 dvd
    ln -sf dvdrw1 dvdwriter
  )
elif [ -L ${udev_root}dvdrw0 -a -L ${udev_root}dvd1 ]; then
  ( cd ${udev_root}
    rm -f dvd dvdwriter
    ln -sf dvd1 dvd
    ln -sf dvdrw0 dvdwriter
  )
elif [ -L ${udev_root}dvd0 -a -L ${udev_root}dvdr1 ]; then
  ( cd ${udev_root}
    rm -f dvd dvdwriter
    ln -sf dvd0 dvd
    ln -sf dvdr1 dvdwriter
  )
elif [ -L ${udev_root}dvdr0 -a -L ${udev_root}dvd1 ]; then
  ( cd ${udev_root}
    rm -f dvd dvdwriter
    ln -sf dvd1 dvd
    ln -sf dvdr0 dvdwriter
  )
else
  # If there is no suitable combination among drives 0 and 1, we will at
  # least try to add a dvdwriter link if possible:
  if [ -L ${udev_root}dvdrw1 ]; then
    ( cd ${udev_root} ; ln -sf dvdrw1 dvdwriter )
  elif [ -L ${udev_root}dvdrw0 ]; then
    ( cd ${udev_root} ; ln -sf dvdrw0 dvdwriter )
  elif [ -L ${udev_root}dvdr1 ]; then
    ( cd ${udev_root} ; ln -sf dvdr1 dvdwriter )
  elif [ -L ${udev_root}dvdr0 ]; then
    ( cd ${udev_root} ; ln -sf dvdr0 dvdwriter )
  fi
fi

# This is NOT standard, so tape a kick-me sign on my back.  Point a link
# called /dev/writer at the most powerful burner we have located:

if [ ! -e ${udev_root}writer ]; then
  if [ -L ${udev_root}dvdwriter ]; then
    ( cd ${udev_root} ; ln -sf ${udev_root}dvdwriter writer )
  elif [ -L ${udev_root}cdwriter ]; then
    ( cd ${udev_root} ; ln -sf ${udev_root}cdwriter writer )
  fi
fi

# If you have more complicated needs than this, then I'd suggest putting some
# of your own custom code in /etc/rc.d/rc.local.  :-)

