#!/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/app}"
if [ -z "$TOKEN" ]; then echo "TEMP_REMOTE_TOOL_AGENT_TOKEN required" >&2; exit 2; fi
mkdir -p "$WORK"
python3 - "$HUB" "$WORK" <<'PY'
import pathlib, shutil, sys, tempfile, urllib.request, zipfile
hub, work = sys.argv[1].rstrip("/"), pathlib.Path(sys.argv[2])
tmp = pathlib.Path(tempfile.mkdtemp())
zip_path = tmp / "temporary_remote_tool_python.zip"
urllib.request.urlretrieve(hub + "/install/archive/python.zip", zip_path)
if work.exists():
    shutil.rmtree(work)
work.mkdir(parents=True, exist_ok=True)
with zipfile.ZipFile(zip_path) as z:
    z.extractall(work)
PY
cd "$WORK"
exec python3 -m temporary_remote_tool.cli agent --hub "$HUB" --token "$TOKEN"
