#!/bin/sh
# $0 is whatever shim or symlink `pnx` was launched through, so walk to the
# file itself before looking beside it. 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
# `${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 "pnx: could not resolve $0 to a regular file within 40 symlink hops." >&2
  exit 1
fi

pnpm=${self%/*}/pnpm
# The placeholder setup.js replaces with the native binary is not executable, so
# this reports the skipped install script rather than an EACCES from `exec`.
if [ ! -x "$pnpm" ]; then
  echo "pnx: pnpm's native binary was not installed next to this script." >&2
  echo "Reinstall @pnpm/exe with its install scripts allowed." >&2
  exit 1
fi

exec "$pnpm" dlx "$@"
