#!/bin/sh
# SlashTable CLI wrapper. Installed at /usr/local/bin/slashtable as a symlink
# to the bundled wrapper inside the app. Resolves symlinks to locate the real
# GUI binary regardless of where the app is installed.
#
# macOS: Contents/Resources/bin/slashtable -> Contents/MacOS/SlashTable
# Linux: resource dir /usr/lib/SlashTable/bin/slashtable -> /usr/bin/SlashTable
#        (or a sibling/parent SlashTable next to the resources tree)

TARGET="$0"
while [ -L "$TARGET" ]; do
  LINK="$(readlink "$TARGET")"
  case "$LINK" in
    /*) TARGET="$LINK" ;;
    *)  TARGET="$(dirname "$TARGET")/$LINK" ;;
  esac
done
DIR="$(cd "$(dirname "$TARGET")" && pwd -P)"

MAC_BIN="$DIR/../../MacOS/SlashTable"
if [ -x "$MAC_BIN" ]; then
  exec "$MAC_BIN" "$@"
fi

for candidate in \
  "$DIR/../SlashTable" \
  "$DIR/../../SlashTable" \
  "$DIR/SlashTable" \
  "/usr/bin/SlashTable"
do
  if [ -x "$candidate" ]; then
    exec "$candidate" "$@"
  fi
done

echo "slashtable: could not locate the SlashTable binary" >&2
exit 1
