#!/bin/ash
# /etc/rc/probe
# scan the machine hard-disk, searching for muLinux images 
# (C) M. Andreoli 

#set -x
IAM=`cat /etc/version`

# local

abort()
{
echo -e $BRIGHT$RED
echo "abort: $@"
echo -e $NORMAL

read -p "s)hell r)eboot " ans

case "$ans"  in
s*)  ash;;
*|r*) ;;
esac
echo "bye!"
reboot -f

}

msg()
{
echo -e "$BRIGHT$GREEN---------------------------"
echo $@
echo -e "---------------------------$NORMAL"
sleep 1
}


register()
{
       echo $DEVICE > /var/tmp/bootdevice
       echo $DIR > /var/tmp/bootdir

echo -e $BRIGHT$GREEN
echo "Images are on device $DEVICE, directory $DIR."
echo -e $NORMAL
}

ask()
{
cd /
umount $MNT 2>/dev/null
setlevel 1
PAR=`fdisk -l 2>/dev/null | rgrep /dev | rgrep -v Disk`
setlevel 5
clear

echo -e $BRIGHT$GREEN
echo "Search failed. So, you want run Linux, well. I need your help."
echo -e $NORMAL


echo "This is the partition table of your system:"
echo -e "$BRIGHT$GREEN"
echo "   Device Boot   Begin    Start      End   Blocks   Id  System"
echo -e "$PAR"
echo -e "$NORMAL"

cat << END

NOTE: hda is for first IDE HD, hdb for second IDE HD, and so on ...
      hda2 means: partition two on primary IDE controller.
      Sorry: DOS Installer can't access SCSI disks.
END

echo
while [ 1 ] ; do
read -p "Which partition contains muLinux downloaded images? \
[/dev/hda1] " DEVICE
[ -z "$DEVICE" ] && DEVICE=/dev/hda1

mount $DEVICE $MNT || abort "mounting $DEVICE."

cd $MNT
echo
msg "Disk contents:"
echo
/bin/ls -F 
echo


read -p "Which directory (starting with a /)? \
[/mulinux] " DIR
[ -z "$DIR" ] && DIR=/mulinux

# check

cd $MNT$DIR && break

cd /; umount $MNT 2>/dev/null
echo "retry."
done
cd /; umount $MNT 2>/dev/null
}

probe_mount()
{
DEVICE=$1
# already mounted?
set -- $(mount | rgrep $DEVICE)
if [ "$3" ] ; then
	MNT=$3
else
	MNT=/mnt
        echo  "Mounting $DEVICE ... "
        mount $DEVICE $MNT 2>/dev/null || return 1
fi


	set -- `mount | rgrep $MNT`; type=$5
	# skip non-DOS disk
	case "$type" in
	ext2) umount $MNT ; return 1;;
	esac
	# seach directories
        echo  "Scanning $DEVICE ... "
	cd $MNT
	paths=`/bin/ls -1d */* | rgrep /mu.cnf` 	
	[ $? -ne 0 ] && return 1
	
	# there exists more installation path?
	# Ok, scan for matching release number

	for p in $paths
	do 
	DIR="/`dirname $p`"
	YOUARE=`( . $MNT$DIR/mu.cnf 2>/dev/null ; echo $VERSION)`
	echo -n "Found muLinux v$YOUARE in $DIR ... " 
	[ "$IAM" = "$YOUARE" ] && echo "release number match." && return 0
	echo "wrong release number."
	done
	return 1
}

# put the 'lilo' command in the RAM

misc()
{
[ "`which lilo`" ] && return 0
lilo=$MNT$DIR/bin/lilo
[ -f "$lilo" ] || return 1
cp $lilo /sbin 
}


# try to mount in turn a list of HDs 
clear
echo -e $BRIGHT$GREEN
echo "Scanning directories  ... "
echo -e $NORMAL
sleep 3

	setlevel 1
	found=
	list="`/bin/scan ide-hd` `/bin/scan scsi-hd`" 
	for D in $list; do
	probe_mount $D && found=yes && break
	cd /; umount $MNT 2>/dev/null
	done
	setlevel 5

# register DEVICE

if [ -z "$found" ] ; then
	sleep 2
	ask
fi

register
misc
cd /; umount $MNT 2>/dev/null
sleep 3

# End
