#!/bin/sh
# -----------------------------------------------------
# WriteDisk 1.4 - (C) 2000 Erich Roncarolo
# WriteDisk 1.3 - (C) 1999 Erich Roncarolo
# WriteDisk 1.2 - (C) 1999 Erich Roncarolo
# WriteDisk 1.1 - (C) 1999 FBW from Cosmic Chaos
# Original by Erich Boehm
# Package: BYLD 1.0.1 
# License: GPL2 (see LICENSE)
# -----------------------------------------------------

dircomm=$(dirname $0)
comm=$(basename $0)
if [ ! -x "./$comm" ] ; then
	echo "$comm should be executed in BYLD home: cd $dircomm"
	cd $dircomm
fi

. Environment

while [ "$1" != "" ] ; do
	eval $1
	shift
done

# check if the there is kernel 
if [ ! -f $RD_KERNEL ]; then
	echo "Please build the kernel first!"
	echo "Read the INSTALL file for some indications on how to do this."
	exit 1
fi

# check if the image was made...
if [ ! -f $RD_IMAGE ]; then
	echo "Please build the disk image using ./MakeImage first!"
	echo "Read the README file for instructions on how to do this."
	exit 1
fi

# here we make some calculations

PACKING=50
KERNELSIZE=$(ls -s $RD_KERNEL | (read KS DUMMY; echo $KS))
IMAGESIZE=$(ls -s $RD_IMAGE | (read IS DUMMY; echo $IS))
BOOTBSIZE=$(ls -s $LILO_BOOT | (read BS DUMMY; echo $BS))

# Look total size

TOTAL=$(du -c $RD_KERNEL $RD_IMAGE $LILO_BOOT |tail -1|cut -f1)
TOTAL=$[ $TOTAL + $PACKING ]

#BOOTSTART=$[$KERNELSIZE + 2]
#RD_FLAG=$[$BOOTSTART + 16384]

#########################################
#
# prg starts here 
#

echo
echo "----------------------------"
echo "Checking BYLD disk to write"
echo "----------------------------"
echo "Kernel size  : ${KERNELSIZE}k"
echo "boot.b size  : ${BOOTBSIZE}k"
echo "Image size   : ${IMAGESIZE}k"
echo "Packing size : ${PACKING}k"
echo "= Total      : ${TOTAL}k"
echo
if [ $TOTAL -gt $MAXSIZE ] ; then
	echo "*** THE SIZE SHOULD NOT EXCEED $MAXSIZE !!!"
	echo "*** You can continue, but only if you are absolutely sure that it's correct!"
	echo "*** If you are not sure type ctrl-c now and reconfigure root distribution"
	echo -n "*** and/or your kernel image; otherwise press [enter]..."
else
	echo "*** OK! It seems that size don't exceed ${MAXSIZE}."
	echo -n "*** If something is wrong type ctrl-c now; otherwise and press [enter]..."
fi
read DUMMY

echo
echo "-------------------------------"
echo "Writing BYLD image..."
echo "-------------------------------"
echo

echo "Make a file of ${MAXSIZE}k and use loop device on it"
dd if=/dev/zero of=$DEST_IMG bs=1k count=$MAXSIZE
if ! `losetup $LOOPDEV $DEST_IMG`; then
	echo "losetup did NOT run correctly."
	echo "Please make sure that you are root and that the losetup binary is in the path."
	echo "Also make sure that you have the loop device compiled in the kernel or the module loaded."
	rm -f $DEST_IMG
	exit 0
fi

mkdir $MNTDIR 2>/dev/null

echo "Make minix fs on loopdev and mount it"
echo "mkfs.minix $LOOPDEV > /dev/null"
mkfs.minix $LOOPDEV > /dev/null
if [ "$?" != "0" ] ; then
	echo "ERROR $? MAKING MINIX FS!"
	losetup -d $LOOPDEV
	exit $?
fi
mount $LOOPDEV $MNTDIR -t minix
if [ "$?" != "0" ] ; then
	echo "ERROR $? MOUNTING LOOPDEV!"
	losetup -d $LOOPDEV
	exit $?
fi

echo -n "Copying kernel image to image dir... "
rm -rf $MNTDIR/lost+found/ &> /dev/null
if ! cp -a $RD_KERNEL $MNTDIR ; then
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit $?
fi
echo "done."

echo -n "Copying initrd image to image dir... "
if ! cp -a $RD_IMAGE $MNTDIR ; then
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit $?
fi
echo "done."

echo -n "Copying LILO boot sector image to image dir... "
if ! cp -a $LILO_BOOT $MNTDIR ; then
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit $?
fi
echo "done."

echo -n "Building lilo.conf... "
LILOMAP=$(basename $LILO_MAP)
LILOBOOT=$(basename $LILO_BOOT)
RDKERNEL=$(basename $RD_KERNEL)
RDIMAGE=$(basename $RD_IMAGE)

eval DISK_GEOMETRY=\$DISK_GEOMETRY_$MAXSIZE
eval $DISK_GEOMETRY
eval DISK_BIOS=$BIOS
eval DISK_CYLINDERS=$CYLINDERS
eval DISK_SECTORS=$SECTORS
eval DISK_HEADS=$HEADS

echo > $LILO_CONF
echo "# This file is automagically generated by BYLD WriteDisk." >> $LILO_CONF
echo "# Edit now if you want change it (try also to read Environment file)." >> $LILO_CONF
echo >> $LILO_CONF
echo "# Global section" >> $LILO_CONF
echo "boot = $LOOPDEV" >> $LILO_CONF
echo "#compact       # faster, but won't work on all systems." >> $LILO_CONF
echo "map = $MNTDIR/$LILOMAP" >> $LILO_CONF
echo "install = $MNTDIR/$LILOBOOT" >> $LILO_CONF
echo >> $LILO_CONF
echo "# Disk geometry" >> $LILO_CONF
echo "disk = $LOOPDEV" >> $LILO_CONF
echo "  bios = $DISK_BIOS" >> $LILO_CONF
echo "  sectors = $DISK_SECTORS" >> $LILO_CONF
echo "  heads = $DISK_HEADS" >> $LILO_CONF
echo "  cylinders = $DISK_CYLINDERS" >> $LILO_CONF
echo >> $LILO_CONF
echo "# Image" >> $LILO_CONF
echo "image = $MNTDIR/$RDKERNEL" >> $LILO_CONF
echo "  label = BYLD" >> $LILO_CONF
echo "  root = /dev/ram0" >> $LILO_CONF
echo "  initrd = $MNTDIR/$RDIMAGE" >> $LILO_CONF
echo >> $LILO_CONF
echo "# End of file" >> $LILO_CONF
echo "done."

if [ "$CHECK_LILO" = "true" -o "$CHECK_LILO" = "true" ] ; then
	echo; echo -n "*** Press [enter] to check ${LILO_CONF}..."
	read DUMMY
	$LILO_EDITOR $LILO_CONF
fi

echo "Running LILO"
if ! lilo -C $LILO_CONF ; then
	umount $MNTDIR
	losetup -d $LOOPDEV
	exit $?
fi
echo "done."

echo "Umount and undo loop"
umount $MNTDIR
rmdir $MNTDIR 2>/dev/null
losetup -d $LOOPDEV

if [ "$DEST_DEV" ] ; then
	echo; echo -n "*** Insert floppy in $DEST_DEV and press [enter]..."
	read DUMMY
	echo
	MINOR=$(ls -l $DEST_DEV | (read x x x x maj min x ; echo $min) )
	if [ $MINOR -ne 0 -a "$FD_FORMAT" = "true" ] ; then
		echo "Formatting disk... "
		$FORMAT_UTIL $DEST_DEV
	fi
	echo "Writing image..."
	dd if=$DEST_IMG of=$DEST_DEV bs=1k conv=sync || WERR=true
	echo "Finish."
fi

echo -n "Waiting until everthing is written... "
sync

echo "done."

if [ "$DEST_DEV" -a "$WERR" = "true" ] ; then
	echo
	echo "*** Maybe an error occurred during writing process:"
	echo "*** try to format devide with  $FORMAT_UTIL  and then $0 again."
	echo
elif [ "$DEST_DEV" ] ; then
	echo
	echo "*** BYLD disk written. You can reboot the system with BYLD disk in floppy drive."
	echo
else
	echo
	echo "*** BYLD image written in file ${DEST_IMG}."
	echo "*** You can put it on a floppy disk using 'dd' or 'cat'."
	if [ $MINOR -ne 0 ] ; then
		echo "*** Probably you need also to execute  $FORMAT_UTIL $DEST_DEV  first."
	fi
	echo
fi
