#!/bin/sh

source ../Common/util.inc

# Size in megabytes that you want to be FREE on filesystem.
# Please, re-read above line!
SIZE=10

# Name of the file that you want fs to be put into
OUTFILE=../BusyBox.img


# NO NEED TO MODIFY ANYTHING BELOW THIS LINE


echo "Build filesystem in ${OUTFILE}..."
umount initrd 2>/dev/null
mkdir -p initrd
mount initrd.fs initrd -oloop
rm -rf TMP
mkdir -p TMP
cp -a initrd/* TMP/
umount initrd
rmdir initrd

mkdir -p TMP/lib/modules/
cp -a ../.common/MODULES/* TMP/lib/modules/
cp -a over/* TMP/
cp ../.common/rc.dinouml TMP/etc/rc.d/rc.local
chmod a+x TMP/etc/rc.d/rc.local
#echo "sc1:12345:respawn:/sbin/agetty -L ttyS0 9600 vt100" >> TMP/etc/inittab
echo "ttyS0" >> TMP/etc/securetty
echo "DinoUML" > TMP/etc/HOSTNAME
sed -e 's/-r now/-h now/' TMP/etc/inittab > TMP/etc/inittab.new
mv TMP/etc/inittab.new TMP/etc/inittab 
cp -a /sbin/ip TMP/sbin

i=0
for dev in a b c d e f g h; do
	echo "Make ubd${dev} (mknod /dev/ubd${dev} b 98 ${i})..."
	rm -f TMP/dev/ubd${dev}
	mknod TMP/dev/ubd${dev} b 98 ${i}
	i=$[${i}+16]
done

# rights
if [ -r "TMP/etc/rc.d/rc.nfsd" ]; then
	chmod a-x TMP/etc/rc.d/rc.nfsd
fi

echo "Put everything in ${OUTFILE}..."
DU=`du -ms TMP | cut -f1`
echo "DU=${DU}"
TOTAL=$[${SIZE}+${DU}]
echo "TOTAL=${TOTAL}"

echo "Make fsfile..."
rm -f "${OUTFILE}"
dd if=/dev/zero of="${OUTFILE}" bs=1M seek=${TOTAL} count=0

echo "Make fs..."
mke2fs -F -m 0 "${OUTFILE}"

echo "Mount file..."
mkdir -p TMP.loop
mount "${OUTFILE}" TMP.loop -t ext2 -oloop

echo "Copy files..."
cp -a TMP/* TMP.loop

echo "Umount file..."
umount TMP.loop
rm -rf TMP.loop

rm -rf TMP

make_default "BusyBox.img"
