#!/bin/sh

if [ $(stat $0 | awk '/Device/ { print $2 }') = "0,0" ] ; then
    echo "Don't run $0 over nfs!"
    exit 1
fi

PATH=/sbin:$PATH
export PATH

SIZE=1440

MNTPOINT=/tmp/mkimage.mnt-$$

#LODEV=/dev/loop$devnum
LODEV=/dev/ram

mkdir -p ../../../images

for IMAGE in boot32.img boot64.img boot-net64.img; do

    if [ "$1" != "-f" ]; then
	if [ -f $IMAGE ]; then
	    echo "$IMAGE already exists." >&2
	    exit 1
	fi
    fi

    FINALIMAGE=../../../images/$IMAGE
    case "$IMAGE" in
	boot32.img) DIR=boot32 ;;
	boot64.img) DIR=boot64; ln -f initrd-local64.img $DIR/initrd64.img ;;
	boot-net64.img) DIR=boot64; ln -f initrd-net64.img $DIR/initrd64.img ;;
    esac

    mkdir $MNTPOINT

    genromfs -V "Red Hat Linux Install" -d $DIR/ -f $LODEV -a 512 -A 2048,/..

    mount -t romfs $LODEV $MNTPOINT

    ./silo -r $MNTPOINT -i /fd.b -b /second.b -C /silo.conf -F

    umount $MNTPOINT
    dd if=$LODEV of=$IMAGE bs=1k count=$SIZE
    #losetup -d $LODEV

    rm -rf $MNTPOINT

    #size=`cat $IMAGE | wc -c`
    #filler=`expr 1474560 - $size`
    cat $IMAGE > $FINALIMAGE
    #dd if=/dev/zero bs=$filler count=1 >> $FINALIMAGE

    rm -f boot64/initrd64.img

done

ln -f tftp/initrd32.img ../../../boot/initrd32.img
ln -f tftp/initrd64.img ../../../boot/initrd64.img
