#!/usr/bin/env bash
#
# This script is invoked after the createimg script copied the root partition
# to the mounted image.
#
# The first argument is the folder of the mounted root partition.
# The second argument is the folder of the source root file system before it
# was copied to the mounted partition.  Some folders like /boot, /media, /root,
# and /tmp are not copied to the mounted partition.
#
# The first argument is the folder of the mounted boot partition.
# The second is the boot partition UUID
# This third is the root partition device name
#

if [ "$#" -ne 4 ]; then
	echo "Usage: $0 mnt boot_dir boot_uuid root_uuid"
	exit 1
fi

set -e

mnt=$1
boot_dir=$2
buuid=$3
ruuid=$4

if [ -e ${mnt}etc/ld.so.preload.disabled ]; then
	mv ${mnt}etc/ld.so.preload.disabled ${mnt}etc/ld.so.preload
fi

sed -e "s;\${BOOT_DIR};$boot_dir;" -e "s/\${BOOT_UUID}/$buuid/" -e "s/\${ROOT_UUID}/$ruuid/" fstab.template > ${mnt}etc/fstab
