#!/usr/bin/env bash
set -euo pipefail

script_dir="${BASH_SOURCE[0]%/*}"
if [[ "$script_dir" == "${BASH_SOURCE[0]}" ]]; then
  script_dir="."
fi
bin="$script_dir/.clew/bin/clew"

case "${OSTYPE:-}" in
  msys*|cygwin*|win32*) candidates=("$bin.exe" "$bin") ;;
  *) candidates=("$bin" "$bin.exe") ;;
esac

for candidate in "${candidates[@]}"; do
  if [[ -x "$candidate" ]]; then
    exec "$candidate" "$@"
  fi
done

echo "stable clew binary missing: ${candidates[*]}" >&2
echo "run: scripts/promote-clew" >&2
exit 1
