summaryrefslogtreecommitdiff
path: root/nextbin
blob: 2e1d9dd5e44c0aebee6d5b539a4a5d762151280e (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
27
28
29
30
31
#!/bin/sh

orig_exe=$(realpath -s "$1")
exe_name=${orig_exe##*/}
case "$orig_exe" in
    */*) ;;
    *)
        printf 'error: cannot find next executable: argv[0] is %s, missing /\n' "$orig_exe"
        exit 1
esac
paths=
passed_paths=
set -f
_IFS=$IFS
IFS=:
for path in $PATH; do
    file=$path/$exe_name
    if [ "$file" = "$orig_exe" ]; then
        if [ -n "$passed_paths" ] && [ -n "$paths" ]; then
            printf 'error: ambiguous next executable for %s past %s, due to multiple non-consecutive instances of %s in PATH' "$exe_name" "$orig_exe" "$(dirname "$orig_exe")"
            exit 1
        fi
        passed_paths=${paths+$paths:}
        paths=
    else
        paths="${paths+$paths:}$path"
    fi
done
IFS=$_IFS
PATH="$passed_paths$paths" exec "$exe_name"
exit 1