#!/bin/sh -
#
# $Id: find-cd,v 1.2 2002/11/07 00:44:44 root Exp $
#
# Try to find the rescue CD-ROM
#

tmpdir=/tmp/mnt.$$

try-mount () {
        echo -n "Looking for CD-ROM at $1... "
        mount -r -t iso9660 $1 $tmpdir > /dev/null 2>&1
        if [ $? -ne 0 ]; then
                echo "not found"
                return 1
        fi
        if [ ! -f $tmpdir/gpl_license.txt ]; then
                umount $tmpdir
                echo "not found"
                return 1
        fi
        umount $tmpdir
        ln -sf $1 /dev/cdrom
        echo "found!"
        return 0
}

# Create temporary directory
mkdir -p $tmpdir

# First, try IDE CD-ROMs
cd /proc/ide
for ide in *; do
        # Actual drive names are links
        if [ -L "$ide" ]; then
                media=`cat $ide/media`
                if [ "$media" = 'cdrom' ]; then
                        if try-mount /dev/$ide; then
                                rmdir $tmpdir
                                exit 0        # Found!
                        fi
                fi
        fi
done
cd /

# Second, try SCSI CD-ROMs
(
        scdn=0
        while read title type rest ; do
                if [ "$title" = 'Type:' -a \( "$type" = 'CD-ROM' -o "$type" = 'WORM' \) ]; then
                        if try-mount /dev/scd$scdn; then
                                rmdir $tmpdir
                                exit 0        # Found
                        fi
                        scdn=`expr $scdn + 1`
                fi
        done
        exit 1        # Not found
) < /proc/scsi/scsi

if [ $? -eq 0 ]; then
        exit 0        # Found it
fi

rmdir $tmpdir
exit 1        # Now what?
