#!/usr/bin/env sh
set -eu

BASE_URL="${DROPHERE_BASE_URL:-https://drophere.page}"
INSTALL_DIR="${DROPHERE_INSTALL_DIR:-$HOME/.drophere/bin}"
BIN="$INSTALL_DIR/drophere"
TMP_FILE="$(mktemp)"

cleanup() {
  rm -f "$TMP_FILE"
}
trap cleanup EXIT

if ! command -v node >/dev/null 2>&1; then
  echo "DropHere requires Node.js 18 or newer." >&2
  exit 1
fi

node -e 'const major = Number(process.versions.node.split(".")[0]); if (major < 18) process.exit(1)' || {
  echo "DropHere requires Node.js 18 or newer." >&2
  exit 1
}

mkdir -p "$INSTALL_DIR"

if command -v curl >/dev/null 2>&1; then
  curl -fsSL "$BASE_URL/cli/drophere.js" -o "$TMP_FILE"
elif command -v wget >/dev/null 2>&1; then
  wget -qO "$TMP_FILE" "$BASE_URL/cli/drophere.js"
else
  echo "Install needs curl or wget." >&2
  exit 1
fi

install -m 755 "$TMP_FILE" "$BIN"

echo "DropHere installed to $BIN"
case ":$PATH:" in
  *":$INSTALL_DIR:"*) ;;
  *)
    echo "Add this to your shell profile:"
    echo "  export PATH=\"$INSTALL_DIR:\$PATH\""
    ;;
esac

echo "Run: drophere --help"

