crdhcpc 0.1.1

Standalone DHCP Client for Linux with DHCPv4, DHCPv6, PXE, and Dynamic DNS support
Documentation
# Makefile for crdhcpc - Standalone DHCP Client
#
# Usage:
#   make              - Build release binary
#   make debug        - Build debug binary
#   make install      - Install binary and systemd services
#   make uninstall    - Remove installed files
#   make clean        - Clean build artifacts

# Configuration
PREFIX ?= /usr/local
BINDIR ?= $(PREFIX)/bin
SYSCONFDIR ?= /etc
SYSTEMDDIR ?= /etc/systemd/system
RUNDIR ?= /var/run
STATEDIR ?= /var/lib/dhcp-client

# Binary name
BINARY = crdhcpc
CONFIG_FILE = dhcp-client.toml

# Build configuration
CARGO ?= cargo
CARGO_FLAGS ?=

# Default target
.PHONY: all
all: build

# Build release binary
.PHONY: build
build:
	$(CARGO) build --release $(CARGO_FLAGS)

# Build debug binary
.PHONY: debug
debug:
	$(CARGO) build $(CARGO_FLAGS)

# Run tests
.PHONY: test
test:
	$(CARGO) test $(CARGO_FLAGS)

# Clean build artifacts
.PHONY: clean
clean:
	$(CARGO) clean

# Install everything
.PHONY: install
install: install-binary install-systemd install-config
	@echo ""
	@echo "Installation complete!"
	@echo ""
	@echo "Next steps:"
	@echo "  1. Edit configuration: sudo nano $(SYSCONFDIR)/$(CONFIG_FILE)"
	@echo "  2. Enable service:     sudo systemctl enable $(BINARY).service"
	@echo "  3. Start service:      sudo systemctl start $(BINARY).service"
	@echo "  4. Check status:       sudo $(BINARY) status"

# Install only the binary
.PHONY: install-binary
install-binary: build
	@echo "Installing binary to $(BINDIR)/$(BINARY)..."
	install -d $(BINDIR)
	install -m 755 target/release/$(BINARY) $(BINDIR)/$(BINARY)

# Install systemd service files
.PHONY: install-systemd
install-systemd:
	@echo "Installing systemd services to $(SYSTEMDDIR)..."
	install -d $(SYSTEMDDIR)
	install -m 644 systemd/dhcp-client-standalone.service $(SYSTEMDDIR)/$(BINARY).service
	install -m 644 systemd/dhcp-client-standalone@.service $(SYSTEMDDIR)/$(BINARY)@.service
	@echo "Reloading systemd daemon..."
	systemctl daemon-reload || true

# Install systemd services for crrouter-web integration
.PHONY: install-systemd-crrouter
install-systemd-crrouter:
	@echo "Installing crrouter-web systemd services..."
	install -d $(SYSTEMDDIR)
	install -m 644 systemd/dhcp-client.service $(SYSTEMDDIR)/dhcp-client.service
	install -m 644 systemd/dhcp-client@.service $(SYSTEMDDIR)/dhcp-client@.service
	install -m 644 systemd/dhcp-client.timer $(SYSTEMDDIR)/dhcp-client.timer
	@echo "Installing helper scripts..."
	install -d $(BINDIR)
	install -m 755 systemd/scripts/dhcp-client-start.sh $(BINDIR)/
	install -m 755 systemd/scripts/dhcp-client-stop.sh $(BINDIR)/
	install -m 755 systemd/scripts/dhcp-client-reload.sh $(BINDIR)/
	install -m 755 systemd/scripts/dhcp-client-healthcheck.sh $(BINDIR)/
	systemctl daemon-reload || true

# Install configuration file (only if not exists)
.PHONY: install-config
install-config:
	@echo "Setting up configuration..."
	@if [ ! -f $(SYSCONFDIR)/$(CONFIG_FILE) ]; then \
		echo "Creating default configuration at $(SYSCONFDIR)/$(CONFIG_FILE)..."; \
		$(BINDIR)/$(BINARY) check --generate-example > $(SYSCONFDIR)/$(CONFIG_FILE); \
		chmod 640 $(SYSCONFDIR)/$(CONFIG_FILE); \
	else \
		echo "Configuration already exists at $(SYSCONFDIR)/$(CONFIG_FILE), skipping."; \
	fi

# Create required directories
.PHONY: install-dirs
install-dirs:
	@echo "Creating directories..."
	install -d $(STATEDIR)
	install -d $(RUNDIR)/$(BINARY)

# Uninstall everything
.PHONY: uninstall
uninstall: uninstall-systemd uninstall-binary
	@echo ""
	@echo "Uninstallation complete!"
	@echo "Note: Configuration file $(SYSCONFDIR)/$(CONFIG_FILE) was preserved."
	@echo "      Remove manually if no longer needed."

# Uninstall binary
.PHONY: uninstall-binary
uninstall-binary:
	@echo "Removing binary..."
	rm -f $(BINDIR)/$(BINARY)

# Uninstall systemd services
.PHONY: uninstall-systemd
uninstall-systemd:
	@echo "Stopping and disabling services..."
	-systemctl stop $(BINARY).service 2>/dev/null || true
	-systemctl stop $(BINARY)@*.service 2>/dev/null || true
	-systemctl disable $(BINARY).service 2>/dev/null || true
	@echo "Removing systemd service files..."
	rm -f $(SYSTEMDDIR)/$(BINARY).service
	rm -f $(SYSTEMDDIR)/$(BINARY)@.service
	rm -f $(SYSTEMDDIR)/dhcp-client.service
	rm -f $(SYSTEMDDIR)/dhcp-client@.service
	rm -f $(SYSTEMDDIR)/dhcp-client.timer
	@echo "Removing helper scripts..."
	rm -f $(BINDIR)/dhcp-client-start.sh
	rm -f $(BINDIR)/dhcp-client-stop.sh
	rm -f $(BINDIR)/dhcp-client-reload.sh
	rm -f $(BINDIR)/dhcp-client-healthcheck.sh
	systemctl daemon-reload || true

# Enable and start the service
.PHONY: enable
enable:
	systemctl enable $(BINARY).service
	systemctl start $(BINARY).service
	@echo "Service enabled and started."
	@echo "Check status: systemctl status $(BINARY).service"

# Disable and stop the service
.PHONY: disable
disable:
	systemctl stop $(BINARY).service
	systemctl disable $(BINARY).service
	@echo "Service stopped and disabled."

# Show service status
.PHONY: status
status:
	@systemctl status $(BINARY).service || true
	@echo ""
	@$(BINDIR)/$(BINARY) status 2>/dev/null || echo "Daemon not running or not accessible."

# Show logs
.PHONY: logs
logs:
	journalctl -u $(BINARY).service -f

# Validate configuration
.PHONY: check
check:
	$(BINDIR)/$(BINARY) check

# Generate example configuration
.PHONY: config-example
config-example:
	@$(BINDIR)/$(BINARY) check --generate-example

# Development helpers
.PHONY: run
run: debug
	RUST_LOG=debug ./target/debug/$(BINARY) daemon --foreground

.PHONY: fmt
fmt:
	$(CARGO) fmt

.PHONY: clippy
clippy:
	$(CARGO) clippy -- -D warnings

# Help
.PHONY: help
help:
	@echo "crdhcpc Makefile targets:"
	@echo ""
	@echo "  Build:"
	@echo "    make              - Build release binary"
	@echo "    make debug        - Build debug binary"
	@echo "    make clean        - Clean build artifacts"
	@echo "    make test         - Run tests"
	@echo ""
	@echo "  Install:"
	@echo "    make install      - Install binary, systemd services, and config"
	@echo "    make install-binary   - Install only the binary"
	@echo "    make install-systemd  - Install only systemd services"
	@echo "    make install-config   - Install default configuration"
	@echo "    make install-systemd-crrouter - Install crrouter-web services"
	@echo ""
	@echo "  Uninstall:"
	@echo "    make uninstall    - Remove all installed files"
	@echo ""
	@echo "  Service management:"
	@echo "    make enable       - Enable and start the service"
	@echo "    make disable      - Stop and disable the service"
	@echo "    make status       - Show service status"
	@echo "    make logs         - Follow service logs"
	@echo ""
	@echo "  Configuration:"
	@echo "    make check        - Validate configuration file"
	@echo "    make config-example - Print example configuration"
	@echo ""
	@echo "  Development:"
	@echo "    make run          - Build and run in foreground (debug mode)"
	@echo "    make fmt          - Format code"
	@echo "    make clippy       - Run clippy lints"
	@echo ""
	@echo "  Variables:"
	@echo "    PREFIX=$(PREFIX)"
	@echo "    BINDIR=$(BINDIR)"
	@echo "    SYSCONFDIR=$(SYSCONFDIR)"
	@echo "    SYSTEMDDIR=$(SYSTEMDDIR)"