#!/usr/bin/env sh
set -eu
HUB="${TEMP_REMOTE_TOOL_HUB:-https://down.luckyfound.info}"
TOKEN="${TEMP_REMOTE_TOOL_AGENT_TOKEN:-}"
WORK="${TEMP_REMOTE_TOOL_HOME:-$HOME/.temporary_remote_tool/node}"
if [ -z "$TOKEN" ]; then echo "TEMP_REMOTE_TOOL_AGENT_TOKEN required" >&2; exit 2; fi
if ! command -v node >/dev/null 2>&1; then echo "node required" >&2; exit 2; fi
mkdir -p "$WORK"
if command -v curl >/dev/null 2>&1; then
  curl -fsSL "$HUB/install/node-agent.js" -o "$WORK/temporary_remote_tool_client.js"
elif command -v wget >/dev/null 2>&1; then
  wget -qO "$WORK/temporary_remote_tool_client.js" "$HUB/install/node-agent.js"
else
  python3 - "$HUB" "$WORK/temporary_remote_tool_client.js" <<'PY'
import sys, urllib.request
urllib.request.urlretrieve(sys.argv[1].rstrip("/") + "/install/node-agent.js", sys.argv[2])
PY
fi
exec node "$WORK/temporary_remote_tool_client.js" agent --hub "$HUB" --token "$TOKEN"
