#! /bin/sh

set -e

cleanup() {
    umount $mnt 2>/dev/null || true
    losetup -vd $loop || true
    rm -r $dir
}

trap cleanup EXIT

dir=`mktemp -d`

image=$dir/image
mnt=$dir/mnt
record=$dir/dumpdates

echo "# creating sparse image file $image"
dd of=$image count=0 bs=1M seek=128

echo "# creating the mountpoint"
mkdir $dir/mnt

x=$(losetup -fv $image)
loop=${x##* }
echo "# image connected to $loop"

mkfs -t ext4 $loop

# installing sample data
mount $loop $mnt
mkdir $mnt/abc.d
touch $mnt/abc.d/foo
sync
umount $mnt

dump -0 -u -D $record -f $dir/dump-0 $loop

# remove the dir
mount $loop $mnt
mv $mnt/abc.d/foo $mnt/
rmdir $mnt/abc.d
sync
umount $mnt

dump -1 -u -D $record -f $dir/dump-1 $loop

# now starting a shell
cd $dir
$SHELL
