#!/bin/sh
# --------------------------------------------
# MakeImage 1.4 - (C) 2000 Erich Roncarolo
# MakeImage 1.3 - (C) 1999 Erich Roncarolo
# MakeImage 1.2 - (C) 1999 Erich Roncarolo
# MakeImage 1.1 - (C) 1999 FBW from Cosmic Chaos 
# Original by Erich Boehm
# 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

mkdir $MNTDIR 2>/dev/null
MY_DEST_DIR=$(cd $RD_ROOT ; pwd)

echo 
echo "---------------------------------------------------"
echo "Creating ramdisk image..."
echo "                    --> this takes up to 2 minutes"
echo "---------------------------------------------------"

#cat >$RD_LINUX/tag.c <<EOF 
##include <stdio.h> 
##include "linux/version.h" 
#int main() { 
#	printf("%s", UTS_RELEASE); 
#	return 0; 
#}
#EOF 
#VER_TAG=$(cd $RD_LINUX; cc -o tag tag.c; ./tag) 

VER_TAG=$(uname -r)

if [ -d $RD_MODULES -a "$(ls $RD_MODULES 2> /dev/null)" ] ; then
	echo "Copying modules to root dir... "
	MY_DEST_DIR=$(cd $RD_ROOT ; pwd)
	rm -R $MY_DEST_DIR/lib/modules 2> /dev/null
	( cd $RD_LINUX ; INSTALL_MOD_PATH=$MY_DEST_DIR make modules_install )
	find $RD_LINUX/lib/modules -name "*.o" -exec strip {} \; &> /dev/null
	depmod -A $VER_TAG -a -F $RD_LINUX/System.map -b $MY_DEST_DIR
	chown -R 0.0 $MY_DEST_DIR/lib/modules
	echo "done."
fi

if [ "$ROOT_TEST" = "true" ] ; then
	echo
	echo "*** Now testing BYLD root dir with following command:"
	echo "*** ( cd $MY_DEST_DIR ; chroot . /bin/bash -login )"
	echo "*** This is not a realistic test, /proc is empty and your kernel is not"
	echo "*** the BYLD kernel, but can be useful to test configuration and binaries."
	echo -n "*** Press [enter]..."
	read dummy
	echo
	echo " ( cd $MY_DEST_DIR ; chroot . /bin/bash -login )"
	sleep 1
	echo 
	echo "*** Now you are chrooted to $MY_DEST_DIR type exit to stop"
	( cd $MY_DEST_DIR ; chroot . /bin/bash -login )
	echo
	echo "*** If during your tour in BYLD root you found something wrong, then"
	echo -n "*** press ctrl-c now and stop making image, else press [enter] to continue..."
	read dummy
fi

echo ; echo "Make a file of ${RD_SIZE}k and use loop device on it"
dd if=/dev/zero of=$TMPFILE bs=1k count=$RD_SIZE
# > /dev/null
if ! `losetup $LOOPDEV $TMPFILE`; 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 $TMPFILE
	exit 0
fi

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

echo -n "Copying ramdisk contents to loopdev... "
cp -a $RD_SOURCE $MNTDIR
echo "done."

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

echo "Create rd image compressed with ${ZIP}"
dd if=$TMPFILE | ${ZIP} -9 > $RD_IMAGE
rm -f $TMPFILE

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

echo "done."
echo
echo "*** Type ./WriteDisk to create the floppy image and write it on a device."
echo
