summaryrefslogtreecommitdiff
path: root/nextbin
blob: d688fa98fce3cac4479a40fe9353dd52c2757016 (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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env sh

set -e

usage() {
    # shellcheck disable=SC2016
    echo 'usage: nextbin [-v] "$0" "$@"' >&2
}

case "$1" in
    -v) action="echo"; shift;;
    -*|'') usage; exit 1;;
    *) action="exec"
esac

orig_exe=$1
shift

die() {
    fmt=$1
    shift
    printf "nextbin %s failed: $fmt\n" "$orig_exe" "$@" >&2
    exit 1
}

[ -n "$PATH" ] || die 'PATH is empty'

# shellcheck disable=SC3013
if [ / -ef / ] && ! [ / -ef /dev/null ]; then
    same_file() {
        # shellcheck disable=SC3013
        [ "$1" -ef "$2" ]
    }
else
    if command -v bash >/dev/null 2>&1; then
        exec bash "$0" "$orig_exe" "$@"
    fi
    ino() {
        ls=$(ls -i "$1")
        printf '%s\n' "${ls%% *}"
    }
    dev() {
        df -P "$1" | awk 'END{print $1}'
    }
    same_file() {
        [ "$(ino "$1")" = "$(ino "$2")" ] && [ "$(dev "$1")" = "$(dev "$2")" ]
    }
fi

case $orig_exe in
    /*) ;;
    *) die 'non-absolute $0'
esac

exe_name=${orig_exe##*/}
path=$PATH
phase=1
found=
while :; do
    exe=${path%%:*}/$exe_name
    if [ -f "$exe" ] && [ -x "$exe" ]; then
        if [ "$phase" = 1 ]; then
            if same_file "$exe" "$orig_exe"; then
                phase=2
            fi
        elif same_file "$exe" "$orig_exe"; then
            [ -z "$found" ] || die 'duplicate PATH entries found'
        elif [ -z "$found" ]; then
            found=$exe
        fi
    fi
    case $path in
        *:*) path=${path#*:} ;;
        *) break ;;
    esac
done

if [ "$phase" = 1 ]; then
    found=$(command -v "$exe_name") || die '%s is not in PATH' "$exe_name"
elif [ -z "$found" ]; then
    die '%s is last instance' "$orig_exe"
fi
"$action" "$found" "$@"