HOST      = 172.16.0.101
SSH_USER  = root
SSH       = ssh $(SSH_USER)@$(HOST)
SCP       = scp
JAIL_NAME = cyclingbot
JAIL_ROOT = /jails/$(JAIL_NAME)
BINARY    = cycling-bot

# Postgres jail config
PG_JAIL   = postgres
PG_DATA   = /var/db/postgres/data16
PG_IFACE  = igb0
PG_IPS    = 172.16.0.215/24 172.16.0.216/24

# Build info (injected via ldflags)
VERSION   = $(shell git describe --tags --always --dirty 2>/dev/null || echo dev)
LDFLAGS   = -ldflags "-X main.version=$(VERSION)"

.PHONY: all build build-native test vet deploy setup deploy-env boot aliases
.PHONY: pg-start pg-stop pg-status start stop restart logs status clean dns-fix

all: test build deploy start

# ── Networking ────────────────────────────────────────────────────────────────
aliases:
	$(SSH) "$(foreach ip,$(PG_IPS),ifconfig $(PG_IFACE) alias $(ip) ;) true"

# ── Postgres ──────────────────────────────────────────────────────────────────
pg-start:
	$(SSH) "service jail start $(PG_JAIL) 2>/dev/null || true"
	$(SSH) "jexec $(PG_JAIL) su -l postgres -c 'pg_ctl status -D $(PG_DATA)' 2>&1 | grep -q 'server is running' \
		&& echo 'postgres already running' \
		|| { rm -f /jails/$(PG_JAIL)$(PG_DATA)/postmaster.pid ; \
		     jexec $(PG_JAIL) su -l postgres -c 'pg_ctl start -D $(PG_DATA)'; }"

pg-stop:
	$(SSH) "jexec $(PG_JAIL) su -l postgres -c 'pg_ctl stop -D $(PG_DATA) -m fast' 2>/dev/null || true"

pg-status:
	$(SSH) "jexec $(PG_JAIL) su -l postgres -c 'pg_ctl status -D $(PG_DATA)'"

# ── Full server boot (run this after every reboot) ────────────────────────────

boot: aliases
	$(SSH) "service jail stop $(JAIL_NAME) 2>/dev/null || true"
	$(SSH) "service jail stop $(PG_JAIL) 2>/dev/null || true"
	@$(MAKE) pg-start
	$(SSH) "service jail start $(JAIL_NAME) 2>/dev/null || true"
	$(SSH) "jexec $(JAIL_NAME) route add default 172.16.0.1 2>/dev/null || true"
	@$(MAKE) start
	@echo "Boot sequence complete."

# ── Build ─────────────────────────────────────────────────────────────────────

# Cross-compile for FreeBSD amd64
build:
	GOOS=freebsd GOARCH=amd64 go build $(LDFLAGS) -o $(BINARY) .

# Build for the local (Linux) platform — useful for testing
build-native:
	go build $(LDFLAGS) -o $(BINARY) .

# Run tests
test:
	go test ./...

# Run go vet
vet:
	go vet ./...

# Create required directories inside the jail (safe to run multiple times)
setup:
	$(SSH) "mkdir -p $(JAIL_ROOT)/usr/local/bin \
	                  $(JAIL_ROOT)/usr/local/etc/rc.d \
	                  $(JAIL_ROOT)/var/db/$(JAIL_NAME) \
	                  $(JAIL_ROOT)/var/log \
	                  $(JAIL_ROOT)/var/run"

# Copy binary and rc.d script into the jail
deploy: test vet build setup
	$(SCP) $(BINARY) $(SSH_USER)@$(HOST):/tmp/$(BINARY)
	$(SCP) rc.d/$(JAIL_NAME) $(SSH_USER)@$(HOST):/tmp/$(JAIL_NAME)-rcd
	$(SSH) "jexec $(JAIL_NAME) service $(JAIL_NAME) stop 2>/dev/null; \
	        rm -f $(JAIL_ROOT)/usr/local/bin/$(BINARY) && \
	        cp /tmp/$(BINARY) $(JAIL_ROOT)/usr/local/bin/$(BINARY) && \
	        chmod +x $(JAIL_ROOT)/usr/local/bin/$(BINARY) && \
	        rm /tmp/$(BINARY) && \
	        rm -f $(JAIL_ROOT)/usr/local/etc/rc.d/$(JAIL_NAME) && \
	        cp /tmp/$(JAIL_NAME)-rcd $(JAIL_ROOT)/usr/local/etc/rc.d/$(JAIL_NAME) && \
	        chmod +x $(JAIL_ROOT)/usr/local/etc/rc.d/$(JAIL_NAME) && \
	        rm /tmp/$(JAIL_NAME)-rcd"

# Copy .env separately (run once — avoid overwriting production config)
deploy-env:
	$(SCP) .env \
	       $(SSH_USER)@$(HOST):$(JAIL_ROOT)/var/db/$(JAIL_NAME)/.env
	$(SSH) "chmod 600 $(JAIL_ROOT)/var/db/$(JAIL_NAME)/.env && \
	        chown 1001:1001 $(JAIL_ROOT)/var/db/$(JAIL_NAME)/.env"

start:
	$(SSH) "rm -f $(JAIL_ROOT)/var/run/$(JAIL_NAME).pid && \
	        jexec $(JAIL_NAME) service $(JAIL_NAME) start"

restart:
	$(SSH) "jexec $(JAIL_NAME) service $(JAIL_NAME) stop 2>/dev/null; \
	        rm -f $(JAIL_ROOT)/var/run/$(JAIL_NAME).pid && \
	        jexec $(JAIL_NAME) service $(JAIL_NAME) start"

stop:
	$(SSH) "jexec $(JAIL_NAME) service $(JAIL_NAME) stop"

logs:
	$(SSH) "jexec $(JAIL_NAME) tail -f /var/log/$(JAIL_NAME).log"

status:
	$(SSH) "jls && jexec $(JAIL_NAME) service $(JAIL_NAME) status"
	@$(MAKE) pg-status

# ── Jail DNS ─────────────────────────────────────────────────────────────────
dns-fix:
	@echo "=== Current jail DNS ==="
	$(SSH) "cat /jails/$(JAIL_NAME)/etc/resolv.conf"
	@echo ""
	@echo "=== Fixing jail DNS ==="
	$(SSH) "printf 'nameserver 1.1.1.1\nnameserver 9.9.9.9\nnameserver 172.16.0.1\n' > /jails/$(JAIL_NAME)/etc/resolv.conf"
	@echo "=== New jail DNS ==="
	$(SSH) "cat /jails/$(JAIL_NAME)/etc/resolv.conf"

clean:
	rm -f $(BINARY)
	go clean
