aboutsummaryrefslogtreecommitdiff
path: root/tools/find-root
blob: d2c71ab986911b979f394992b8d95113d48173ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/sh

set -eu

if [ ! "${1:-}" ]; then
    exit 1
fi

BASEDIR="$(dirname "$1")"

while read -r glob; do
    cd "$BASEDIR"

    while [ "$PWD" != "/" ]; do
        set -- $glob

        if [ -e "$1" ]; then
            echo "$PWD"
            exit 0
        fi

        cd ..
    done
done

echo "$BASEDIR"