#!/usr/bin/sh
BLOCKDEV=${BLOCKDEV:=/dev/mmcblk0}
PART=${BLOCKDEV}p1
ROOTFS=$1

if [[ $BLOCKDEV == /dev/sd* ]]; then
	PART=${BLOCKDEV}1
fi


echo "Using BLOCKDEV=$BLOCKDEV"
echo "Using PART=$PART"

echo "Erasing boot sector.."
#dd if=/dev/zero of=$BLOCKDEV bs=1M count=8

echo "Writing first level bootloader..."
dd if=./MLO of=$BLOCKDEV count=1 seek=1 conv=notrunc bs=128k status=progress

echo "Writing U-Boot..."
dd if=./u-boot.img of=$BLOCKDEV count=2 seek=1 conv=notrunc bs=384k

echo "Writing rootfs.."
xzcat $ROOTFS | dd of=$PART bs=512 status=progress

echo "Checking rootfs.."
fsck -f $PART

echo "Resizing rootfs.."
resize2fs $PART

