#!/bin/sh


TryToFindCDROM() {
# hda1 is there for testing
    LogIt "find-and-mount-cdrom --- starting"
    for device in hda hdb hdc hdd hde hdf hdg hdh rcd0 rcd1 rcd2 rcd3 sr0 sr1 sr2 sr3 cd0 cd1 cd2 cd3 scd0 scd1 scd2 scd3 ; do
	[ ! "$SECOND_TRY" ] && LogIt "Trying /dev/$device"
        if [ "`cat /tmp/mondo-restore.cfg | grep "using-cdstream yes"`" ]; then
	    pwd=`pwd`
	    cd /mnt/groovy-stuff
            tar -zxf /dev/$device 2> /tmp/mount.log
            res=$?
	    cd $pwd
            if [ "$res" -eq "0" ] ; then
		clear
		LogIt "Using cdstream as extended datadisk (/dev/$device). Good." 3
		echo "Using cdstrea as extd dsk." > /tmp/TAPEDEV-HAS-DATA-DISKS
                ln -sf /dev/$device /dev/cdrom
                exit 0
	    fi
        else
	    mount /dev/$device -t iso9660 -o ro /mnt/cdrom 2> /tmp/mount.log
	    res=$?
        fi
	if [ "$res" -ne "0" ] ; then
	    res=`cat /tmp/mount.log`
	    if [ "$res" = "mount: No medium found" ] ; then
		[ ! "$SECOND_TRY" ] && LogIt "There is a CD-ROM drive at /dev/$device but no CD in it."
	    else
		[ ! "$SECOND_TRY" ] && LogIt "It's not in /dev/$device; I'll keep looking"
	    fi
	    continue
	fi
	LogIt "/dev/$device has a CD-ROM in it"
	umount /mnt/cdrom
	ln -sf /dev/$device /dev/cdrom
	if [ "$?" -ne "0" ]; then
	    LogIt "Unable to softlink /dev/$device to /dev/cdrom. That's weird."
	    continue
	fi
        LogIt "CDROM found at $device"
	mount /mnt/cdrom
        if [ "$?" -ne "0" ] ; then
	    LogIt "Cannot mount /dev/cdrom (type $format) (dev=/dev/$device)"
            continue
        elif [ ! -d "/mnt/cdrom/archives" ] ; then
	    LogIt "There is a CD in /dev/$device but it's not a Mondo CD"
            continue
        else
            LogIt "Wu-Tang Forever! Oh, um, that is,"
	    LogIt "/dev/$device is where the Mondo CD lives."
            which hdparm > /dev/null 2> /dev/null && hdparm -u1 -c3 -d1 /dev/$device
	    return 0
	fi
    done
    LogIt "Failed to find CDROM"
    return 1
}


# -------------- main ------------

[ "$1" = "--second-try" ] && SECOND_TRY=yes

TryToFindCDROM
if [ "$?" -eq "0" ] ; then
    [ "$SECOND_TRY" ] && add="At 2nd attempt, " || add=""
    LogIt $add"CDROM found and mounted at /dev/$device" 3
    echo "/dev/$device" > /tmp/CDROM-LIVES-HERE
    LogIt "find-and-mount-cdrom --- leaving (0)"
    exit 0
fi
#mount /dev/fd0u1722 -t ext2 /mnt/floppy
[ "$1" = "--second-try" ] && exit 1; # don't try to mount floppy drive
if [ "`cat /tmp/mondo-restore.cfg | grep "using-cdstream yes"`" ] ; then
    LogIt "Because you are using cdstream, I won't try to mount CD."
    exit 0
fi
mount /dev/fd0 -t ext2 -o ro /mnt/floppy 2> /dev/null
if [ "$?" -eq "0" ] ; then
    umount /mnt/floppy 2> /dev/null
    exit 1
else
    LogIt "Please go to another PC, mount this CD and copy the data disk images" 1
    LogIt "from the CD's /images directory to blank 1.44MB floppy disks. You should" 1
    LogIt "use something like 'dd if=/mnt/cdrom/images/mindi-data-1.img of=/dev/fd0'" 1
    LogIt "for the first data disk, mindi-data-2.img for the second, and so on." 1
    exit 2
fi

