# Makefile for mod_reqin_log
# Apache HTTPD module for logging HTTP requests as JSON to Unix socket

# APXS tool path (can be overridden)
APXS ?= apxs

# Compiler settings
CC ?= gcc
CFLAGS ?= -Wall -Wextra -O2 -std=gnu11 -x c -Wno-error=format-security

# Directories
SRC_DIR = src
BUILD_DIR = build
INSTALL_DIR = modules
DIST_DIR = dist

# Source files
SRCS = $(SRC_DIR)/mod_reqin_log.c

# Module name
MODULE_NAME = mod_reqin_log

# Package version
VERSION ?= 1.0.7

.PHONY: all clean install uninstall test package package-deb package-rpm

all: $(MODULE_NAME).so

# Build the module using apxs
# Note: Use -Wc to pass flags to the C compiler through apxs
$(MODULE_NAME).so: $(SRCS)
	@mkdir -p $(BUILD_DIR)
	$(APXS) -c -Wc,"$(CFLAGS)" -o $(BUILD_DIR)/$(MODULE_NAME).so $(SRCS)
	@mkdir -p $(INSTALL_DIR)
	@if [ -f $(BUILD_DIR)/.libs/$(MODULE_NAME).so ]; then \
		cp $(BUILD_DIR)/.libs/$(MODULE_NAME).so $(INSTALL_DIR)/; \
	elif [ -f $(BUILD_DIR)/$(MODULE_NAME).so ]; then \
		cp $(BUILD_DIR)/$(MODULE_NAME).so $(INSTALL_DIR)/; \
	fi

# Install the module
install: $(MODULE_NAME).so
	@echo "Installing $(MODULE_NAME).so..."
	@mkdir -p $(DESTDIR)/usr/lib/apache2/modules
	@if [ -f $(INSTALL_DIR)/$(MODULE_NAME).so ]; then \
		cp $(INSTALL_DIR)/$(MODULE_NAME).so $(DESTDIR)/usr/lib/apache2/modules/; \
	elif [ -f $(BUILD_DIR)/.libs/$(MODULE_NAME).so ]; then \
		cp $(BUILD_DIR)/.libs/$(MODULE_NAME).so $(DESTDIR)/usr/lib/apache2/modules/; \
	elif [ -f $(BUILD_DIR)/$(MODULE_NAME).so ]; then \
		cp $(BUILD_DIR)/$(MODULE_NAME).so $(DESTDIR)/usr/lib/apache2/modules/; \
	else \
		echo "Error: $(MODULE_NAME).so not found"; \
		exit 1; \
	fi
	@echo "Installation complete."
	@echo "Enable the module by adding to your httpd.conf:"
	@echo "  LoadModule reqin_log_module modules/mod_reqin_log.so"

# Uninstall the module
uninstall:
	rm -f $(DESTDIR)/usr/lib/apache2/modules/$(MODULE_NAME).so
	@echo "Uninstallation complete."

# Clean build artifacts
clean:
	rm -rf $(BUILD_DIR) $(INSTALL_DIR)
	rm -f .libs/*.o .libs/*.la .libs/*.so
	rm -f *.o *.la *.lo
	rm -rf .libs

# Run unit tests (requires cmocka)
test:
	@mkdir -p build/tests
	cd build/tests && cmake ../../ -DCMAKE_BUILD_TYPE=Debug
	$(MAKE) -C build/tests run_tests

# Build with debug symbols
debug: CFLAGS += -g -DDEBUG
debug: clean all

# =============================================================================
# Packaging (RPM with Docker + fpm)
# Dockerfile.package builds RPMs in a single multi-stage build:
#   - 3 RPM packages (el8, el9, el10 for RHEL/Rocky/AlmaLinux compatibility)
# =============================================================================

## package: Build all RPM packages (el8, el9, el10)
package:
	mkdir -p $(DIST_DIR)/rpm/el8 $(DIST_DIR)/rpm/el9 $(DIST_DIR)/rpm/el10
	docker build --target output -t mod_reqin_log:packager \
		--build-arg VERSION=$(VERSION) \
		-f Dockerfile.package .
	@echo "Extracting packages from Docker image..."
	docker run --rm -v $(PWD)/$(DIST_DIR)/rpm/el8:/output/el8 \
		-v $(PWD)/$(DIST_DIR)/rpm/el9:/output/el9 \
		-v $(PWD)/$(DIST_DIR)/rpm/el10:/output/el10 \
		mod_reqin_log:packager \
		sh -c 'cp /packages/rpm/el8/*.rpm /output/el8/ && cp /packages/rpm/el9/*.rpm /output/el9/ && cp /packages/rpm/el10/*.rpm /output/el10/'
	@echo "Packages created:"
	@echo "  RPM (el8, el9, el10):"
	@ls -la $(DIST_DIR)/rpm/el8/
	@ls -la $(DIST_DIR)/rpm/el9/
	@ls -la $(DIST_DIR)/rpm/el10/

## package-rpm: Build RPM packages (el8, el9, el10)
package-rpm: package
	@echo "RPM packages built in Dockerfile.package"

## test-package-rpm: Test RPM package installation in Docker (tests el9 by default)
test-package-rpm: package
	docker run --rm -v $(PWD)/$(DIST_DIR)/rpm/el9:/packages:ro rockylinux:9 \
		sh -c "dnf install -y /packages/*.el9.*.rpm && echo 'RPM el9 install OK'"

## test-package-rpm-el8: Test el8 RPM installation
test-package-rpm-el8: package
	docker run --rm -v $(PWD)/$(DIST_DIR)/rpm/el8:/packages:ro rockylinux:8 \
		sh -c "dnf install -y /packages/*.el8.*.rpm && echo 'RPM el8 install OK'"

## test-package-rpm-el9: Test el9 RPM installation
test-package-rpm-el9: package
	docker run --rm -v $(PWD)/$(DIST_DIR)/rpm/el9:/packages:ro rockylinux:9 \
		sh -c "dnf install -y /packages/*.el9.*.rpm && echo 'RPM el9 install OK'"

## test-package-rpm-el10: Test el10 RPM installation
test-package-rpm-el10: package
	docker run --rm -v $(PWD)/$(DIST_DIR)/rpm/el10:/packages:ro almalinux:10 \
		sh -c "dnf install -y /packages/*.el10.*.rpm && echo 'RPM el10 install OK'"

## test-package: Test all RPM packages installation
test-package: test-package-rpm-el8 test-package-rpm-el9 test-package-rpm-el10

# Help target
help:
	@echo "mod_reqin_log Makefile"
	@echo ""
	@echo "Targets:"
	@echo "  all       - Build the module (default)"
	@echo "  install   - Install the module to DESTDIR"
	@echo "  uninstall - Remove the module from DESTDIR"
	@echo "  clean     - Remove build artifacts"
	@echo "  test      - Run unit tests"
	@echo "  debug     - Build with debug symbols"
	@echo "  package   - Build all RPM packages (el8, el9, el10)"
	@echo "  package-rpm  - Build RPM packages"
	@echo "  test-package - Test RPM package installation"
	@echo ""
	@echo "Variables:"
	@echo "  APXS     - Path to apxs tool (default: apxs)"
	@echo "  CC       - C compiler (default: gcc)"
	@echo "  CFLAGS   - Compiler flags (default: -Wall -Wextra -O2)"
	@echo "  DESTDIR  - Installation destination (default: /)"
	@echo "  VERSION  - Package version (default: 1.0.4)"
