.PHONY: all build release install clean deb rpm arch test

VERSION := $(shell grep '^version' Cargo.toml | head -1 | cut -d'"' -f2)
NAME := suspiro
CARGO_BIN := $(HOME)/.cargo/bin

all: release

# Development build
build:
	cargo build

# Release build
release:
	cargo build --release

# Run tests
test:
	cargo test

# Install locally
install: release
	install -Dm755 target/release/$(NAME) ~/.local/bin/$(NAME)
	install -Dm644 data/suspiro.service ~/.config/systemd/user/suspiro.service
	install -Dm644 data/io.suspiro.Daemon.service ~/.local/share/dbus-1/services/io.suspiro.Daemon.service
	systemctl --user daemon-reload
	@echo "Installed. Run: systemctl --user enable --now suspiro"

# Uninstall
uninstall:
	systemctl --user stop suspiro || true
	systemctl --user disable suspiro || true
	rm -f ~/.local/bin/$(NAME)
	rm -f ~/.config/systemd/user/suspiro.service
	rm -f ~/.local/share/dbus-1/services/io.suspiro.Daemon.service
	systemctl --user daemon-reload

# Build Debian package
deb:
	@(which cargo-deb > /dev/null 2>&1 || test -x $(CARGO_BIN)/cargo-deb) || (echo "Install cargo-deb: cargo install cargo-deb" && exit 1)
	$(CARGO_BIN)/cargo-deb
	@echo "Package created: target/debian/$(NAME)_$(VERSION)_*.deb"

# Build RPM package
rpm: release
	@(which cargo-generate-rpm > /dev/null 2>&1 || test -x $(CARGO_BIN)/cargo-generate-rpm) || (echo "Install cargo-generate-rpm: cargo install cargo-generate-rpm" && exit 1)
	$(CARGO_BIN)/cargo-generate-rpm
	@echo "Package created: target/generate-rpm/$(NAME)-$(VERSION)-*.rpm"

# Build Arch package (must be in packaging/arch directory)
arch:
	@echo "To build Arch package:"
	@echo "  cd packaging/arch && makepkg -si"

# Clean build artifacts
clean:
	cargo clean
	rm -rf target/debian target/generate-rpm

# Show package info
info:
	@echo "Name: $(NAME)"
	@echo "Version: $(VERSION)"
	@echo ""
	@echo "Build commands:"
	@echo "  make release  - Build release binary"
	@echo "  make install  - Install to ~/.local"
	@echo "  make deb      - Build .deb package"
	@echo "  make rpm      - Build .rpm package"
	@echo "  make arch     - Instructions for Arch"
