#!/bin/bash -e

TARGET_ROOTFS_DIR=./binary
ROOTFSIMAGE=linaro-rootfs.img


echo Making rootfs!

if [ -e ${ROOTFSIMAGE} ]; then
	sudo rm ${ROOTFSIMAGE}
fi

for script in ./post-build.sh ../device/rockchip/common/post-build.sh; do
	[ -x $script ] || continue
	sudo $script "$(realpath "$TARGET_ROOTFS_DIR")"
done

# Apparent size + maxium alignment(file_count * block_size) + maxium journal size
IMAGE_SIZE_MB=$(( $(sudo du --apparent-size -sm ${TARGET_ROOTFS_DIR} | cut -f1) + \
	$(sudo find ${TARGET_ROOTFS_DIR} | wc -l) * 4 / 1024 + 64 ))

# Extra 10%
IMAGE_SIZE_MB=$(( $IMAGE_SIZE_MB * 110 / 100 ))

sudo mkfs.ext4 -d ${TARGET_ROOTFS_DIR} ${ROOTFSIMAGE} ${IMAGE_SIZE_MB}M

echo Rootfs Image: ${ROOTFSIMAGE}
