#!/bin/bash
# Lackadaisical squashfs tools - Unmount

DIR="$1"
if [[ -z "$DIR" ]]; then
  echo "Usage: $0 <directory>"
  exit 1
fi

DIR=$(readlink -f "$DIR")
DIR_SHORT=$(basename "$DIR")

OVERLAY_ROOT="$(dirname "$DIR")/.squashfs/$DIR_SHORT"
OVERLAY_LOWER="$OVERLAY_ROOT/lower"
OVERLAY_TARG="$DIR"

# Forcefully unmount both layers recursively
sudo umount -l -R "$OVERLAY_TARG" 2>/dev/null
sudo umount -l -R "$OVERLAY_LOWER" 2>/dev/null

if mountpoint -q "$OVERLAY_TARG" || mountpoint -q "$OVERLAY_LOWER"; then
    echo "Warning: Filesystem is still mounted. Check for open processes."
    exit 1
fi

echo "SquashFS filesystem has been unmounted."
exit 0
