#!/bin/sh
# `pnpx` is `pnpm dlx`. The pnpm it hands over to is the one installed alongside
# it, found relative to this file: a `PATH` lookup would run whatever other pnpm
# comes first there, and would find nothing at all when the directory holding
# these bins is not on `PATH`.
#
# $0 is whatever shim or symlink `pnpx` was launched through, so walk to the file
# itself first. The hop cap matches the kernel's ELOOP limit, so a cycle cannot
# hang the script. Directories come from `${self%/*}` and `readlink` runs through
# `command -p`, so the caller's `PATH` decides nothing here.
self=$0
# MSYS and Cygwin can launch this with a native Windows path, which has no slash
# for `${self%/*}` to strip. Only a drive letter or a UNC prefix marks one; a
# backslash anywhere else is an ordinary character in a Unix file name, so the
# path is left alone. The separators are swapped in the shell rather than through
# `echo`, which mangles a `\t` or `\b` in a path under dash.
case $self in
  [A-Za-z]:\\*|\\\\*)
    while :; do
      case $self in
        *\\*) self=${self%%\\*}/${self#*\\} ;;
        *) break ;;
      esac
    done
    ;;
esac
# `${self%/*}` needs a slash to strip. A bare name came from a `PATH` lookup and
# stands for a file in the current directory.
case $self in
  */*) ;;
  *) self=./$self ;;
esac
hops=0
while [ -L "$self" ] && [ "$hops" -lt 40 ]; do
  hops=$((hops + 1))
  link=$(command -p readlink "$self")
  case $link in
    /*) self=$link ;;
    *) self=${self%/*}/$link ;;
  esac
done
# The walk has to end at a regular file. Running out of hops leaves $self a
# symlink; a chain that changed under us can leave it dangling or a directory, and
# a failed readlink leaves a trailing slash. Each case would take `pnpm` from the
# wrong directory — the substitution this script exists to prevent.
if [ -L "$self" ] || [ ! -f "$self" ]; then
  echo "pnpx: could not resolve $0 to a regular file within 40 symlink hops." >&2
  exit 1
fi

exec "${self%/*}/pnpm" dlx "$@"
