# OPLabs Mixer Daemon — build, install, manage
#
# Targets:
#   build          — Build the daemon binary
#   test           — Run core tests
#   install        — Install systemd service
#   uninstall      — Remove systemd service
#   restart        — Restart the systemd service
#   status         — Show service status
#   start          — Start the service
#   stop           — Stop the service
#   logs           — Tail log output
#   clean          — Clean build artifacts

BUILD_DIR := $(dir $(realpath $(lastword $(MAKEFILE_LIST))))build
SERVICE := oplabs-mixer-daemon.service
SERVICE_PATH := /etc/systemd/system/$(SERVICE)

.PHONY: build test install uninstall restart status start stop logs clean

# Build the daemon
build:
	@mkdir -p $(BUILD_DIR)
	@cd $(BUILD_DIR) && cmake .. && make -j$$(nproc)
	@echo "=== Build complete: $(BUILD_DIR)/mixer-daemon ==="

# Run core tests
test:
	@cd $(BUILD_DIR) && ctest --output-on-failure

# Install systemd service and start it
install: $(BUILD_DIR)/mixer-daemon
	sudo cp $(SERVICE) $(SERVICE_PATH)
	sudo systemctl daemon-reload
	sudo systemctl enable $(SERVICE)
	sudo systemctl restart $(SERVICE)
	@echo "=== Service status ==="
	sudo systemctl status --no-pager $(SERVICE)

# Remove systemd service
uninstall:
	sudo systemctl stop $(SERVICE) || true
	sudo systemctl disable $(SERVICE) || true
	sudo rm -f $(SERVICE_PATH)
	sudo systemctl daemon-reload
	@echo "=== Uninstalled ==="

restart:
	sudo systemctl restart $(SERVICE)
	sudo systemctl status --no-pager $(SERVICE)

status:
	sudo systemctl status --no-pager $(SERVICE)

start:
	sudo systemctl start $(SERVICE)

stop:
	sudo systemctl stop $(SERVICE)

logs:
	sudo journalctl -u $(SERVICE) -n 100 -f

clean:
	rm -rf $(BUILD_DIR)
	@echo "=== Cleaned ==="
