# Makefile for mod_hostinglimits
# Generated based on mod_hostinglimits.spec
#
# Targets:
#   sync-sources    - Build module and sync to system paths (no rpmbuild)
#   build-package   - Build RPM with unique timestamp (allows upgrades without version bump)
#   install-package - Install built RPM via yum/dnf
#   run-unit-tests         - Run unit tests (spec has no %check)
#   run-integration-tests  - Run QA pytest suite (requires QA + cl-venv; modifies host)

# Package info — NAME is hardcoded because the spec uses a conditional macro
# (%{?eig:eig_}mod_hostinglimits) that cannot be parsed via grep.
SPEC_FILE := mod_hostinglimits.spec
NAME := mod_hostinglimits
VERSION := $(shell grep -m1 '^Version:' $(SPEC_FILE) | awk '{print $$2}')

# RPM build paths (local directory, not ~/rpmbuild)
RPMBUILD_DIR := $(CURDIR)/.rpmbuild

# Out-of-tree cmake build dir (avoids clobbering this Makefile)
BUILD_DIR := $(CURDIR)/.build

# System install paths (system httpd on CloudLinux)
HTTPD_MODULES_DIR := /usr/lib64/httpd/modules
HTTPD_CONF_DIR := /etc/httpd/conf.d

# Source items included in the tarball (spec does `%setup -q` + cmake build)
SOURCE_ITEMS := src CMakeLists.txt cmake apr install modhostinglimits.conf LICENSE.TXT suexec.patch

# Integration tests: QA pytest suite invoked via its canonical wrapper.
QA_ROOT   := /root/ai-workspace/QA
QA_SUITE  := $(QA_ROOT)/c-projects/mod_hostinglimits
CL_PYTHON := /opt/cloudlinux/venv/bin/python3

.PHONY: help sync-sources build-package install-package run-unit-tests run-integration-tests _integration-preflight clean

help:
	@echo "Available targets:"
	@echo "  sync-sources    - Build module and sync to system paths (fast, no rpmbuild)"
	@echo "  build-package   - Build RPM package (unique timestamp, allows upgrades)"
	@echo "  install-package - Install built RPM via yum/dnf"
	@echo "  run-unit-tests         - Run unit tests (spec has no %check)"
	@echo "  run-integration-tests  - Run QA pytest suite (requires QA + cl-venv; modifies host)"
	@echo "  clean                  - Remove build artifacts"
	@echo ""
	@echo "Package: $(NAME)-$(VERSION)"

# =============================================================================
# sync-sources: Build .so via cmake and copy to system paths (no rpmbuild)
# Based on spec %build + %install (system httpd path only)
# =============================================================================
sync-sources:
	@echo "Building mod_hostinglimits.so (out-of-tree at $(BUILD_DIR))..."
	@mkdir -p $(BUILD_DIR)
	cd $(BUILD_DIR) && cmake -DBUILD:BOOL=TRUE $(CURDIR)
	$(MAKE) -C $(BUILD_DIR)
	@echo "Installing module to $(HTTPD_MODULES_DIR)/"
	install -D -m 755 $(CURDIR)/lib2.2/mod_hostinglimits.so $(HTTPD_MODULES_DIR)/mod_hostinglimits.so
	@echo "Installing config to $(HTTPD_CONF_DIR)/"
	install -D -m 644 modhostinglimits.conf $(HTTPD_CONF_DIR)/modhostinglimits.conf
	@echo "Sync complete. Restart httpd to pick up changes: systemctl restart httpd"

# =============================================================================
# build-package: Build RPM using rpmbuild with local _topdir
# Uses timestamp in dist for unique, upgradeable builds
# Source0 in spec is tar.bz2, so we produce .tar.bz2 (not .tar.gz).
# =============================================================================
build-package:
	@echo "Building RPM package..."
	@mkdir -p $(RPMBUILD_DIR)/BUILD $(RPMBUILD_DIR)/RPMS $(RPMBUILD_DIR)/SOURCES $(RPMBUILD_DIR)/SPECS $(RPMBUILD_DIR)/SRPMS
	@echo "-> Creating source tarball ($(NAME)-$(VERSION).tar.bz2)"
	@TMPDIR=$$(mktemp -d) && \
	mkdir -p $$TMPDIR/$(NAME)-$(VERSION) && \
	cp -r $(SOURCE_ITEMS) $$TMPDIR/$(NAME)-$(VERSION)/ && \
	tar -cjf $(RPMBUILD_DIR)/SOURCES/$(NAME)-$(VERSION).tar.bz2 -C $$TMPDIR $(NAME)-$(VERSION) && \
	rm -rf $$TMPDIR
	@cp eig-mod-hostinglimits.pl $(RPMBUILD_DIR)/SOURCES/
	@cp eig-mod-hostinglimits.sh $(RPMBUILD_DIR)/SOURCES/
	@cp $(SPEC_FILE) $(RPMBUILD_DIR)/SPECS/
	rpmbuild -ba \
		--define "_topdir $(RPMBUILD_DIR)" \
		--define "dist $$(rpm --eval '%{dist}').$$(date +%s)" \
		$(RPMBUILD_DIR)/SPECS/$(SPEC_FILE)
	@echo "Build complete! RPMs at: $(RPMBUILD_DIR)/RPMS/"

# =============================================================================
# install-package: Install built RPM package
# =============================================================================
install-package:
	@RPM_FILE=$$(ls -t $(RPMBUILD_DIR)/RPMS/*/$(NAME)-$(VERSION)*.rpm 2>/dev/null | head -1); \
	if [ -z "$$RPM_FILE" ]; then \
		echo "Error: No RPM found. Run 'make build-package' first."; \
		exit 1; \
	fi; \
	echo "Installing $$RPM_FILE"; \
	yum install -y $$RPM_FILE

# =============================================================================
# run-unit-tests: spec has no %check section and there are no unit tests
# =============================================================================
run-unit-tests:
	@echo "No unit tests configured (spec has no %check section)"

# =============================================================================
# run-integration-tests: runs QA/c-projects/mod_hostinglimits/ pytest suite.
#
# Invokes pytest directly (not QA's ./run wrapper) so failing tests produce a
# non-zero exit — ./run's run.py discards pytest.main()'s return, which would
# make the autofix skill treat failures as passes.
#
# Markers hardcoded to the no-panel set ("Common or WP") since workspace VMs
# don't have a control panel installed; use `cd $(QA_SUITE) && ./run` directly
# if you need the cPanel or Endurance marker set.
#
# Prerequisite: the mod_hostinglimits RPM must already be installed on the
# host (run `make install-package` first). The suite's configure_environment
# fixture is NOT autouse — it won't self-bootstrap.
# =============================================================================
run-integration-tests: _integration-preflight
	@rpm -q mod_hostinglimits >/dev/null 2>&1 || { \
		echo "Error: mod_hostinglimits RPM is not installed on this host."; \
		echo "Run: make build-package && make install-package"; \
		exit 1; }
	@cd $(QA_SUITE) && \
		PYTHONPATH=$(QA_ROOT)/c-projects:$$PYTHONPATH \
		$(CL_PYTHON) -m pytest -m "Common or WP" .

_integration-preflight:
	@[ -d $(QA_SUITE) ] || { \
		echo "Error: $(QA_SUITE) not found."; \
		echo "Run: cd /root/ai-workspace/cl-aiworkspaces && make workspace-projects-add PROJECT=QA"; \
		exit 1; }
	@[ -x $(CL_PYTHON) ] || { \
		echo "Error: $(CL_PYTHON) missing (install lvemanager / CloudLinux venv)"; \
		exit 1; }

# =============================================================================
# clean: Remove build artifacts
# =============================================================================
clean:
	rm -rf $(RPMBUILD_DIR) $(BUILD_DIR)
	rm -rf $(CURDIR)/lib2.2 $(CURDIR)/lib2.2h $(CURDIR)/lib1.3
	rm -f CMakeCache.txt cmake_install.cmake
	rm -rf CMakeFiles
