.PHONY: all backend agent agent-linux updater-freebsd updater-linux plugin clean certs release deploy-backend deploy-frontend

VERSION ?= 0.0.0

BACKEND_BIN = build/rmm-backend
AGENT_BIN = build/rmm-agent
AGENT_LINUX_BIN = build/rmm-agent-linux
UPDATER_FREEBSD_BIN = build/rmm-updater-freebsd
UPDATER_LINUX_BIN = build/rmm-updater-linux

BACKEND_HOST = 192.168.85.13
SSH_USER = root

all: backend agent agent-linux updater-freebsd updater-linux

backend:
	@echo "==> Building Backend (linux/amd64)..."
	cd backend && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ../$(BACKEND_BIN) .
	@echo "==> $(BACKEND_BIN) erstellt"

agent:
	@echo "==> Building Agent (freebsd/amd64)..."
	cd agent-bsd && GOOS=freebsd GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-X main.Version=$(VERSION)" -o ../$(AGENT_BIN) .
	@echo "==> $(AGENT_BIN) erstellt"

agent-linux:
	@echo "==> Building Agent Linux (linux/amd64)..."
	cd agent-linux && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags "-X main.Version=$(VERSION)" -o ../$(AGENT_LINUX_BIN) .
	@echo "==> $(AGENT_LINUX_BIN) erstellt"

updater-freebsd:
	@echo "==> Building Updater (freebsd/amd64)..."
	cd updater && GOOS=freebsd GOARCH=amd64 CGO_ENABLED=0 go build -o ../$(UPDATER_FREEBSD_BIN) .
	@echo "==> $(UPDATER_FREEBSD_BIN) erstellt"

updater-linux:
	@echo "==> Building Updater (linux/amd64)..."
	cd updater && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o ../$(UPDATER_LINUX_BIN) .
	@echo "==> $(UPDATER_LINUX_BIN) erstellt"

# Release: Baut alles und erzeugt ZIP-Bundles pro Plattform
release: agent agent-linux updater-freebsd updater-linux
	@echo "==> Creating release bundles v$(VERSION)..."
	@mkdir -p build/release
	@# FreeBSD Bundle (Agent + Updater + install.sh)
	@cp opnsense-plugin/install.sh build/install-freebsd.sh
	@cd build && zip -j release/rmm-freebsd-$(VERSION).zip rmm-agent rmm-updater-freebsd install-freebsd.sh
	@echo "==> build/release/rmm-freebsd-$(VERSION).zip erstellt"
	@# Linux Bundle (Agent + Updater + install.sh)
	@cp agent-linux/install.sh build/install-linux.sh
	@cd build && zip -j release/rmm-linux-$(VERSION).zip rmm-agent-linux rmm-updater-linux install-linux.sh
	@echo "==> build/release/rmm-linux-$(VERSION).zip erstellt"
	@echo ""
	@echo "Release v$(VERSION) fertig:"
	@ls -lh build/release/rmm-*-$(VERSION).zip

plugin:
	@echo "==> Building OPNsense Plugin Package..."
	cd opnsense-plugin && ./build.sh $(VERSION)
	@echo "==> Plugin Package erstellt in opnsense-plugin/"

clean:
	rm -rf build/ opnsense-plugin/stage/

certs:
	@echo "==> Generiere self-signed TLS-Zertifikate..."
	mkdir -p certs
	openssl req -x509 -newkey ec -pkeyopt ec_paramgen_curve:P-256 \
		-keyout certs/server.key -out certs/server.crt \
		-days 3650 -nodes \
		-subj "/CN=rmm-backend/O=RMM" \
		-addext "subjectAltName=IP:192.168.85.13,IP:127.0.0.1,DNS:localhost"
	@echo "==> Zertifikate in certs/ erstellt"

deploy-frontend:
	cd frontend && npm run build
	ssh root@192.168.85.20 "rm -rf /var/www/html/assets/*"
	scp -r frontend/dist/assets frontend/dist/index.html root@192.168.85.20:/var/www/html/
	ssh root@192.168.85.20 "chown -R www-data:www-data /var/www/html/ && chmod -R 755 /var/www/html/"
	@echo "==> Frontend deployed"

deploy-backend: backend
	@echo "==> Deploying Backend to $(BACKEND_HOST)..."
	ssh $(SSH_USER)@$(BACKEND_HOST) "systemctl stop rmm-backend || true"
	scp $(BACKEND_BIN) $(SSH_USER)@$(BACKEND_HOST):/home/cynfo/rmm/rmm-backend
	ssh $(SSH_USER)@$(BACKEND_HOST) "systemctl start rmm-backend"
	@echo "==> Backend deployed und gestartet"
