#!/usr/bin/env bash
# Fake findmnt driven by $FAKE_MOUNTS: lines of "SOURCE TARGET" (raw-escaped).
[[ -z "${FAKE_FINDMNT_FAIL:-}" ]] || { echo "findmnt: simulated failure" >&2; exit 1; }
touch "${FAKE_MOUNTS:?}"
if [[ "$*" == *"--target"* ]]; then
    path="${@: -1}"; best="" src=""
    while read -r s t; do
        t="$(printf '%b' "$t")"
        if [[ "$path" == "$t" || "$path" == "$t"/* || "$t" == / ]]; then
            ((${#t} >= ${#best})) && { best="$t"; src="$s"; }
        fi
    done < "$FAKE_MOUNTS"
    [[ -n "$src" ]] && { echo "$src"; exit 0; }
    exit 1
fi
cat "$FAKE_MOUNTS"
