#!/bin/sh



InsistOnCD() {
    local the_next_cd cd_inserted_correctly line present_cd
    the_next_cd=$1

    if [ "`ThisCDsNumber`" = "$the_next_cd" ] ; then
#        echo "CD #$the_next_cd is already inserted. Proceeding."
        return 0
    fi

    cd_inserted_correctly="no"
    while [ "$cd_inserted_correctly" != "yes" ] ; do
	umount /mnt/cdrom &> /dev/null
        eject /dev/cdrom
	echo -en "\rPlease insert CD #$the_next_cd in this set, then press ENTER."
 	read line
	sync
	eject -t /dev/cdrom
	sync
	mount /mnt/cdrom
	ls /mnt/cdrom/archives &> /tmp/try_to_read_cd.txt
	if [ "$?" -ne "0" ] ; then
	    echo "Bad CD? No CD? I can't find /mnt/cdrom/archives. \
Please try again."
        elif [ "`ThisCDsNumber`" != "$the_next_cd" ] ; then
            echo "This CD is #`ThisCDsNumber`; \
I want #$the_next_cd. Try again."
	else
	    echo -e -n "Thankyou. "
	    cd_inserted_correctly="yes"
	fi
    done
    LogIt "Now processing CD#$the_next_cd." 1
}


ThisCDsNumber() {
    if [ ! -f "/mnt/cdrom/archives/THIS-CD-NUMBER" ] ; then
        echo "(absent)"
    else
        cat /mnt/cdrom/archives/THIS-CD-NUMBER
    fi
}

InsistOnCD $1
exit $?
