#!/bin/sh

set -e

ISO_IMG=/tmp/test.iso
MNT_PNT=/tmp/test_iso

echo '~~~ Create an iso image from the source tree'
# Add a symlink to test fix of https://bugs.debian.org/1083039
ln -snf debian/rules symlink_to_rules
genisoimage -jcharset ascii -r -o $ISO_IMG . 2>&1

echo '~~~ Mount the iso image'
fuseiso -p $ISO_IMG $MNT_PNT

echo '~~~ Check the iso image against the source tree'
# Compare output of 'tree' calls ignoring directories,
# their sizes differs no matter what
tree -DFs . | grep -v '/$' >/tmp/source_tree.tree
cd $MNT_PNT
tree -DFs . | grep -v '/$' >/tmp/mounted_iso.tree
cd $OLDPWD
diff /tmp/source_tree.tree /tmp/mounted_iso.tree
# Compare files as to content
find . -type f -exec cmp {} $MNT_PNT/{} \;

echo '~~~ Unmount and remove the iso image'
fusermount -u $MNT_PNT
rm $ISO_IMG
