#!	/bin/sh

TEMP_FILE=/tmp/dsetup.$$
trap "rm -f $TEMP_FILE;exit 255" 2 3 15

. /lib/dialog.sh
clear=`tput clear`

if [ $# -ge 1 ]; then
	TARGET=$1
fi

# All of the devices to be tested for the presence of a CD drive.
CD_DEVICES="/dev/aztcd0 /dev/cdu31a /dev/cdu535 /dev/lmscd \
	/dev/mcd /dev/sbpcd /dev/sbpcd0 /dev/scd0 /dev/sonycd /dev/hda \
	/dev/hdb /dev/hdc /dev/hdd"

# All of the devices/directories to be unmounted before testing for the
# presence of a CD drive. This is a subset of the above because I don't
# want to unmount any IDE hard disks because I think they are IDE CD drives.
# I could perhaps do better here by checking for a ISO9660 filessytem using
# my own code, instead of using mount() to do that.

UMOUNT_LIST="/mnt /cdrom /dev/aztcd0 /dev/cdu31a /dev/cdu535 /dev/lmscd \
	/dev/mcd /dev/sbpcd /dev/sbpcd0 /dev/scd0 /dev/sonycd"

initialize () {
	if [ ! -f /proc/version ]; then
		mount -t proc /proc /proc >/dev/null 2>&1
	fi
	local v=`third \`cat /proc/version\``
	local p="/target/lib/modules/$v"
	MODPATH="$p/block:$p/cdrom:$p/char:$p/fs:$p/misc:$p/net:$p/ipv4:."
	export MODPATH
}

loadModule () {
	sync
	rmmod $1 > /dev/null 2>&1
	dmesg -c >/dev/null
	insmod `first $1` >>$TEMP_FILE 2>&1
	if [ $? -ne 0 ]; then
		message="The $1 module could not be loaded. This is the error message: "
		(echo "$message"; dmesg; cat $TEMP_FILE) | textBox "Problem"
		rm -f $TEMP_FILE
		return 255
	else
		rm -f $TEMP_FILE
		return 0
	fi
}

# Sort of like "grep", but only works on line beginnings, and on the
# standard input. It happens that grep isn't on the root disk, so I
# need this.
# lookFor word
lookFor () {
	local first
	local second
	while read first second; do
		if [ $first = $1 ]; then
			exit 0
		fi
	done
	exit 1
}

loadISO9660Module () {
	(lookFor "iso9660" < /proc/filesystems)
	if [ $? -eq 0 ]; then
		echo "The ISO 9660 filesystem driver is present."
		return 0;
	fi
	loadModule isofs
	if [ $? -ne 0 ]; then
		msgBox \
"The ISO-9660 filesystem module could not be loaded.
It will not be possible to mount a CD-ROM until this
problem is resolved." "Problem"
		exit 255
	else
		echo "Loaded the ISO 9660 filesystem driver."
	fi
}

lookForCD () {
	for i in $UMOUNT_LIST; do
		umount $i >/dev/null 2>&1 
	done

	echo "Looking for possible CD devices (including IDE CD drives)."
	echo "This may wait up to 10 seconds for each device."

	CD_CANDIDATES=`tryopen $CD_DEVICES`

	if [ -n "$CD_CANDIDATES" ]; then
		for i in $CD_CANDIDATES; do
			echo "Checking if" $i "contains a CD."
			sync
			mount -t iso9660 -o ro $i /cdrom >/dev/null 2>&1
			if [ $? -eq 0 ]; then
				if [ -f /cdrom/cdrom/base.tgz ]; then
					echo "GOT IT!" $i "contains the Debian CD."
					echo "Extracting the base system from the CD..."
					(cd $TARGET; gunzip < /cdrom/cdrom/base.tgz | cpio --dot --extract --unconditional)
					if [ $? -eq 0 ]; then
						echo "Extracted the base system sucessfully."
						umount /cdrom >/dev/null 2>&1
						ln -fs $i $TARGET/dev/cdrom
						return 0
					else
						umount /cdrom >/dev/null 2>&1
						echo "Failed to extract the base system."
						return 2
					fi
				else
					umount /cdrom >/dev/null 2>&1
					echo $i "contains a CD, but not the Debian CD."
				fi
			else
				echo "No CD in" $i
			fi
		done
	fi
	echo "Did not find the Debian CD in any CD-ROM drive."
	return 1
}

warnAndLoadModule () {
	local name=`first $1`
	msgBox \
"About to load the $name module. This could make you wait
for 30 seconds or more. If the system hangs, press RESET and
go through the installation procedure again, but don't load
the $name driver the same way next time." "Warning"
	echo $clear
	loadModule $1
	return $?
}

loadCustomModule () {
	dialog $DIALOG_OPTIONS \
	--backtitle "$BACKTITLE" \
	--title "Custom Module Selection" \
	--inputbox \
"Please enter the module name and command line parameters." \
	10 70 2>$TEMP_FILE
	local result=`cat $TEMP_FILE`
	if [ -n "$result" ]; then
		warnAndLoadModule "$result"
		return $?
	fi
	return 1
}

installCDModules () {
	dialog $DIALOG_OPTIONS \
		--backtitle "$BACKTITLE" \
		--title "CD Driver Selection" \
		--menu \
"These are the CD-ROM driver modules you can install. Don't install
a driver for hardware that you don't have. Please select a driver
or press CANCEL." \
		15 78 6 \
		sbpcd	"Sound Blaster Pro CD." \
		aztcd	"Orchid, Aztech, Okeano, and Wearnes." \
		sony535	"SONY CDU-535." \
		custom	"Select custom driver and parameters." \
		quit	"Give up on the CD and return to the main menu." 2>$TEMP_FILE
	local result=`cat $TEMP_FILE`
	rm -f $TEMP_FILE
	case $result in
	""|quit)
		exit 1
		;;
	sbpcd|aztcd|sony535)
		warnAndLoadModule $result
		return $?
		;;
	custom)
		loadCustomModule
		return $?
		;;
	esac

}


main () {
BACKTITLE="Debian 0.93R6 Installation"
title="CD Set-Up"
msgBox \
"Please insert the Debian CD in your CD-ROM drive, and wait for the drive to
become ready before you press return. We'll attempt to load the Debian Base
System from the CD.

We'll first try to find the CD in all of the easy places. If that doesn't
work, we'll install some CD-ROM device drivers and try again.

Notes:  Your CD drive won't be found unless it was already turned on and
connected to the system when you bootstrapped the Debian Boot disk. If
you simply can't get the CD based installation to work, you can create
base floppy disks (given that you can read the /dos directory of the
CD from a DOS, Unix, or Linux system) and load the Base system that way." \
"$title"

echo $clear
loadISO9660Module
lookForCD
local result=$?
case $result in
	0)
		msgBox \
		 "The base system was successfully\ninstalled from the CD." \
		 "$title"
		exit 0
		;;
	1)
			msgBox \
"The CD wasn't located on your system. Now, we'll try to set up some
additional CD-ROM device drivers." "$title"
		while true; do
			installCDModules
			dialog $DIALOG_OPTIONS \
				--backtitle "$BACKTITLE" \
				--title "Select Installation Action" \
				--menu \
				"What would you like to do now?" \
				15 78 6 \
				modules	"Install more modules." \
				cd		"Attempt to load the base system from the CD." \
				quit	"Give up on the CD and return to the main menu." \
				 2>$TEMP_FILE
			result=`cat $TEMP_FILE`
			rm -f $TEMP_FILE
			case $result in
			cd)
				break
				;;
			modules)
				continue
				;;
			quit|"")
				exit 1
				;;
			esac
		done
		;;

	2)
		msgBox \
"The base system did not extract correctly.
Perhaps you will need to go back through
some of the earlier steps." "$title"
		exit 1
		;;
esac
}

initialize
while true; do
	main
done
