
CC = gcc
OUT_DIR  = ./build
TARGET   = ./bin/proxmon
SRC_DIRS = ./src

# Collect all C source files in the current directory
SRCS := $(shell find $(SRC_DIRS) -name '*.c')

# Generate object files in the build directory
OBJS := $(patsubst $(SRC_DIRS)/%.c, $(OUT_DIR)/%.o, $(SRCS))

INC_DIRS := $(shell find $(SRC_DIRS) -type d) $(INC_DIRS)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))

CCFLAGS := $(INC_FLAGS)

.PHONY: build clean install install-services uninstall-services create-services reinstall-services

install: build create-services install-services  ## Full installation. Builds the proxmon executable, generates service files, installs and starts services
 
build: $(TARGET) create-services  ## Build the proxmon executable, and generates service files for editing

clean:  ## Remove all build artifacts including generated service files (does not uninstall service files)
	@rm -rf bin/ $(OUT_DIR) $(SERVICE_DIR)

./bin: 
	@mkdir -p ./bin
	@cp ./dist/* ./bin

$(OUT_DIR)/%.o: $(SRC_DIRS)/%.c
	@mkdir -p $(dir $@)
	$(CC) $(CCFLAGS) -c $< -o $@

$(TARGET): $(OBJS) | ./bin
	$(CC) $(CCFLAGS) $(OBJS) -o $@


TEMPLATE_DIR ?= ./template
SERVICE_DIR ?= ./systemd
INSTALL_DIR ?= /etc/systemd/system
WORKDIR := $(shell pwd)

# Collect all template service files
TEMPLATES := $(shell find $(TEMPLATE_DIR) -name '*.service.in')

# Generate names of service files that will have paths substituted
SERVICES := $(patsubst $(TEMPLATE_DIR)/%.service.in, $(SERVICE_DIR)/%.service, $(TEMPLATES))

# Generate names of installed service files
INSTALLED := $(patsubst $(SERVICE_DIR)/%, $(INSTALL_DIR)/%, $(SERVICES))

# Create services directory
$(SERVICE_DIR):
	@mkdir -p $@

# Convert template service into actual service file
$(SERVICE_DIR)/%.service: $(TEMPLATE_DIR)/%.service.in | $(SERVICE_DIR)
	sed 's|@WORKDIR@|$(WORKDIR)|g' $< > $@

create-services: $(SERVICES)	## Generate service files from templates into the systemd/ directory

# install a service into systemd
$(INSTALL_DIR)/%.service: $(SERVICE_DIR)/%.service
	install -m 644 $< $(INSTALL_DIR)/
	

install-services: create-services $(INSTALLED)  ## Installs all service files in the systemd/ directory into /etc/systemd/system
	systemctl daemon-reload
	@for srv in $(SERVICES); do \
		name=$$(basename $$srv); \
		systemctl enable $$name; \
		systemctl start $$name; \
	done

uninstall-services: ## Stops and uninstalls all services associated with ProxMon
	@for srv in $(INSTALLED); do \
		name=$$(basename $$srv); \
		echo "Stopping and uninstalling $$name..."; \
		systemctl stop $$name || true; \
		systemctl disable $$name || true; \
		if [ -f $$srv ]; then rm -f $$srv; fi; \
	done
	systemctl daemon-reload

reinstall-services: uninstall-services install-services  ## Uninstalls and reinstalls ProxMon services

help: ## Show help message
	@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n  make \033[36m\033[0m\n"} /^[$$()% a-zA-Z_-]+:.*?##/ { printf "  \033[36m%-18s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
