#!/bin/bash
DIR="$@"
DIR=$(readlink -f "$DIR")
DIR_SHORT=$(basename $DIR)

if [[ -f "$DIR/.needs_mount" ]];
then
  echo "Not a SquashFS image, exiting."
  exit 0
fi

echo "Destroying the existing SquashFS image."
echo "Copying data from the original directory."

echo "Using rsync... in 2 seconds."
sleep 2
rsync -avz --progress "$DIR" "/tmp/$DIR_SHORT.tmp"

umount-squash-image "$DIR"

rm -rf "$DIR"
mv "/tmp/$DIR_SHORT.tmp" "$DIR"

rm -rf "$DIR/../.squashfs/$DIR_SHORT.img"
rm -rf "$DIR/../.squashfs/$DIR_SHORT"

echo "Your SquashFS image was deleted."
