.PHONY: replay replay-down 2rtk 2rtk-down 2rtk-setup down logs status local capture

# === Simple replay caster (no auth, instant start) ===
replay:
	docker compose --profile replay up -d --build

replay-down:
	docker compose --profile replay down

# === 2RTK production caster (web UI + feeder) ===
2rtk:
	docker compose --profile 2rtk up -d --build

2rtk-down:
	docker compose --profile 2rtk down -v

# Setup 2RTK mount points and users after first start
2rtk-setup:
	@echo "Waiting for 2RTK to be healthy..."
	@sleep 5
	@echo "Logging in..."
	@curl -s -c /tmp/2rtk_cookie -X POST http://localhost:5757/api/login \
		-H "Content-Type: application/json" \
		-d '{"username":"admin","password":"admin123"}' > /dev/null
	@echo "Creating mount: kunaidaho"
	@curl -s -b /tmp/2rtk_cookie -X POST http://localhost:5757/api/mounts \
		-H "Content-Type: application/json" \
		-d '{"mount":"kunaidaho","password":"rtcm2024"}' || true
	@echo ""
	@echo "Creating mount: nampaidaho"
	@curl -s -b /tmp/2rtk_cookie -X POST http://localhost:5757/api/mounts \
		-H "Content-Type: application/json" \
		-d '{"mount":"nampaidaho","password":"WEEK2400a"}' || true
	@echo ""
	@echo "Creating rover user"
	@curl -s -b /tmp/2rtk_cookie -X POST http://localhost:5757/api/users \
		-H "Content-Type: application/json" \
		-d '{"username":"rover","password":"rover123"}' || true
	@echo ""
	@echo "Done! Connect with: --user rover --password rover123"

# === Common ===
down:
	docker compose --profile replay --profile 2rtk down

logs:
	docker compose logs -f

status:
	@echo "=== Container Status ==="
	@docker compose ps 2>/dev/null || echo "No containers running"
	@echo ""
	@echo "=== RTCM Data Files ==="
	@ls -lh data/*.bin 2>/dev/null || echo "No RTCM data files"
	@echo ""
	@echo "=== Quick NTRIP Test ==="
	@python3 -c "import socket; s=socket.socket(); s.settimeout(3); s.connect(('localhost',2101)); \
		s.send(b'GET / HTTP/1.1\r\nHost: localhost\r\n\r\n'); print(s.recv(1024).decode('latin-1')[:300]); s.close()" 2>/dev/null \
		|| echo "Caster not responding on port 2101"

# Run simple caster locally without Docker
local:
	python caster.py --data-dir data -v

# Capture RTCM from a remote caster
# Usage: make capture MOUNT=kunaidaho
capture:
	python capture.py \
		--host $(HOST) \
		--port $(PORT) \
		--mount $(MOUNT) \
		--user "$(USER)" \
		--password "$(PASS)" \
		--duration $(DURATION) \
		--output data/$(MOUNT).bin

HOST ?= rtk2go.com
PORT ?= 2101
MOUNT ?= kunaidaho
USER ?= ryan@malloys.us
PASS ?= none
DURATION ?= 60
