GO ?= go
DIST ?= dist
SERVICE_ENV ?= service/.env
REMOTE_ARCH ?= amd64
HOST ?=

DOMAIN ?=

.PHONY: help check-go test vet run build-amd64 build-arm64 build-linux build-host checksums clean install-service start-service stop-service service-status service-logs install-service-remote setup-vhost

help:
	@echo 'Live Update Go daemon targets:'
	@echo '  make check-go            Check the Go compiler version'
	@echo '  make test                Run tests & test Drupal-to-Go daemon connectivity with diagnostics'
	@echo '  make vet                 Run go vet'
	@echo '  make run                 Run the daemon in this terminal (uses service/.env)'
	@echo '  make build               Build Linux amd64 and arm64 binaries'
	@echo '  make checksums           Create SHA256SUMS for built binaries'
	@echo '  make install-service     Install and start a systemd service on this Linux host'
	@echo '  make setup-vhost         Setup Apache or Nginx VHost WebSocket proxy (auto-detect & backup)'
	@echo '  make start-service | stop-service | service-status | service-logs'
	@echo '  make install-service-remote HOST=user@server [REMOTE_ARCH=amd64|arm64]'

check-go:
	@$(GO) version

test:
	@./scripts/test-connection.sh

test-unit:
	cd service && $(GO) test ./...

vet:
	cd service && $(GO) vet ./...

run:
	@test -f $(SERVICE_ENV) || { echo 'Missing $(SERVICE_ENV). Copy service/.env.example to service/.env and edit it first.'; exit 1; }
	set -a; . ./$(SERVICE_ENV); set +a; cd service && $(GO) run .

build: build-amd64 build-arm64

build-host:
	mkdir -p $(DIST)
	cd service && GOOS=linux GOARCH=$$($(GO) env GOARCH) CGO_ENABLED=0 $(GO) build -trimpath -buildvcs=false -ldflags='-s -w' -o ../$(DIST)/liveupdate-go-linux-$$($(GO) env GOARCH) .

build-amd64:
	mkdir -p $(DIST)
	cd service && GOOS=linux GOARCH=amd64 CGO_ENABLED=0 $(GO) build -trimpath -buildvcs=false -ldflags='-s -w' -o ../$(DIST)/liveupdate-go-linux-amd64 .

build-arm64:
	mkdir -p $(DIST)
	cd service && GOOS=linux GOARCH=arm64 CGO_ENABLED=0 $(GO) build -trimpath -buildvcs=false -ldflags='-s -w' -o ../$(DIST)/liveupdate-go-linux-arm64 .

checksums:
	cd $(DIST) && sha256sum liveupdate-go-linux-* > SHA256SUMS

install-service: build-host
	@test "$$(uname -s)" = Linux || { echo 'Run this target on the Linux server where the service should run.'; exit 1; }
	@test -f $(SERVICE_ENV) || { echo 'Missing $(SERVICE_ENV). Copy service/.env.example to service/.env and edit it first.'; exit 1; }
	sudo ./scripts/install-service.sh $(DIST)/liveupdate-go-linux-$$($(GO) env GOARCH) $(SERVICE_ENV) service/systemd/liveupdate-go.service

start-service:
	sudo systemctl start liveupdate-go

stop-service:
	sudo systemctl stop liveupdate-go

service-status:
	systemctl status liveupdate-go --no-pager

service-logs:
	sudo journalctl -u liveupdate-go -f

install-service-remote:
	@test -n "$(HOST)" || { echo 'Usage: make install-service-remote HOST=user@server [REMOTE_ARCH=amd64|arm64]'; exit 1; }
	@test -f $(SERVICE_ENV) || { echo 'Missing $(SERVICE_ENV). Copy service/.env.example to service/.env and edit it first.'; exit 1; }
	$(MAKE) build-$(REMOTE_ARCH)
	ssh $(HOST) 'mkdir -p "$$HOME/.cache/liveupdate-go-deploy" && chmod 700 "$$HOME/.cache/liveupdate-go-deploy"'
	scp $(DIST)/liveupdate-go-linux-$(REMOTE_ARCH) $(SERVICE_ENV) service/systemd/liveupdate-go.service scripts/install-service.sh $(HOST):.cache/liveupdate-go-deploy/
	ssh $(HOST) 'chmod 600 "$$HOME/.cache/liveupdate-go-deploy/"*; sudo "$$HOME/.cache/liveupdate-go-deploy/install-service.sh" "$$HOME/.cache/liveupdate-go-deploy/liveupdate-go-linux-$(REMOTE_ARCH)" "$$HOME/.cache/liveupdate-go-deploy/.env" "$$HOME/.cache/liveupdate-go-deploy/liveupdate-go.service"; result=$$?; rm -rf "$$HOME/.cache/liveupdate-go-deploy"; exit $$result'

clean:
	rm -rf $(DIST)

setup-vhost:
	sudo ./scripts/setup-vhost.sh $(DOMAIN)
