#!/bin/sh
#----
IFS=":"
#----
for dir in $PATH
do
    if [ -z "$dir" ]
    then
        continue
    fi
    #----
    nodejs="$dir/node"
    #----
    #- Check if binary exists in current path and is executable
    if [ -x "$nodejs" ]
    then
        #- Check if binary matches this script using inode numbers
        if ! [ "$nodejs" -ef "$0" ] 2>/dev/null
        then
            #- If -ef isn't supported by this shell, fallback to path string comparison
            #- NOTE: path must not be a symlink
            if [ "$nodejs" = "$0" ]
            then
                continue
            fi
        else
            #- If -ef evaluated successfully and they match, skip it
            if [ "$nodejs" -ef "$0" ]
            then
                continue
            fi
        fi
        #- Run found nodejs binary with all incoming arguments
        exec "$nodejs" ${1+"$@"}
        exit 0
    fi
done
echo "Error: nodejs binary not found in PATH." >&2
exit 1
