#!/bin/sh

PATH=/sbin:$PATH
export PATH

BASEDIR=initrd
SIZE=2250

while [ -n "$1" ] ; do
    case "$1" in
	"sparc")
	    IMAGES="$IMAGES sparc"
	    shift
	    ;;
	"sparc64")
	    IMAGES="$IMAGES sparc64"
	    shift
	    ;;
	"local64")
	    IMAGES="$IMAGES local64"
	    shift
	    ;;
	"network64")
	    IMAGES="$IMAGES network64"
	    shift
	    ;;
	-f)
	    FORCE=-f
	    shift
	    ;;
	*)
	    echo "Useage:" >&2
	    echo "$0 [-f] [sparc] [sparc64] [local64] [network64]" >&2
	    exit
	    ;;
    esac
done

if [ -z "$IMAGES" ]; then
    IMAGES="sparc sparc64 local64 network64"
fi


MNTPOINT=/tmp/mkimage.mnt-$$

for TARGET in $IMAGES; do

    case $TARGET in
        sparc) IMAGE=tftp/initrd32.img ;;
	sparc64) IMAGE=tftp/initrd64.img ;;
	local64) IMAGE=initrd-local64.img ;;
	network64) IMAGE=initrd-net64.img ;;
    esac

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

    rm -f /tmp/initrd-$TARGET.img
    dd if=/dev/zero of=/tmp/initrd-$TARGET.img bs=1k count=$SIZE
    mke2fs -i 8096 -q /tmp/initrd-$TARGET.img $SIZE <<EOF
y
EOF

    mkdir -p $MNTPOINT

    mount -o loop -t ext2 /tmp/initrd-$TARGET.img $MNTPOINT

    (cd $BASEDIR; find . | cpio -vp $MNTPOINT)
    (cd $TARGET; find . | cpio -vpud $MNTPOINT)
    if [ "$TARGET" = local64 ]; then
	rm -f $MNTPOINT/sbin/loader
	strip ../anaconda/loader/loader-local -R .comment -R .note \
	    -o $MNTPOINT/sbin/loader
    fi

    df $MNTPOINT

    umount $MNTPOINT
    mv -f /tmp/initrd-$TARGET.img $IMAGE
    echo -n "gzipping image..."
    mv $IMAGE $IMAGE.nogz
    gzip -9 < $IMAGE.nogz > $IMAGE
    echo " done."

    rm -rf $MNTPOINT  $IMAGE.nogz

    size=`cat $IMAGE | wc -c`
    filler=`expr 1474560 - $size`

    mkdir -p ../../../boot
    case $TARGET in
	sparc) cp $IMAGE ../../../boot/initrd32.img
	       ln -f $IMAGE boot32/initrd32.img ;;
	sparc64) cp $IMAGE ../../../boot/initrd64.img ;;
    esac

done
