MAKEFLAGS += --no-print-directory

#
# Setup Makefile to match your environment
#
PYTHON=/usr/bin/python3

# check for executables

BLACK = $(shell which black)
HTTPD = $(shell which httpd)
APACHE2 = $(shell which apache2)

# Debian / Ubuntu have /bin/sh -> dash
SHELL = /bin/bash

NAME=$(shell basename $(CURDIR))
VERSION=$(shell python3 -m setuptools_scm | grep -Po '^[0-9]+\.[0-9]+\.[0-9]+')
TOP_DIR:=$(shell pwd)
DESTDIR=/
SYSTESTS="basic_test or import_test or settings_cli_test or svc_test"

prefix=devinstall

# Taken from: https://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
help:
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

all: clean build ## Executes the clean target and afterwards the build target.

clean: ## Cleans Python bytecode, build artifacts and the temp files.
	@echo "cleaning: python bytecode"
	@rm -f *.pyc
	@rm -f cobbler/*.pyc
	@rm -f cobbler/modules/*.pyc
	@rm -f cobbler/web/*.pyc
	@rm -f cobbler/web/templatetags/*.pyc
	@rm -rf cobbler/__pycache__
	@rm -rf cobbler/**/__pycache__
	@echo "cleaning: build artifacts"
	@rm -f cobbler/_version.py
	@rm -rf cobbler/data/man
	@rm -rf build release dist cobbler.egg-info
	@rm -rf rpm-build/*
	@rm -rf deb-build/*
	@rm -f MANIFEST AUTHORS
	@rm -f docs/*.1.gz
	@rm -rf docs/_build
	@echo "cleaning: temp files"
	@rm -f *~
	@rm -rf buildiso
	@rm -f *.tmp
	@rm -f *.log
	@rm -f supervisord.pid
	@rm -rf .pytest_cache

cleandoc: ## Cleans the docs directory.
	@echo "cleaning: documentation"
	@cd docs; make clean > /dev/null 2>&1

doc: ## Creates the documentation with sphinx in html form.
	@echo "creating: documentation"
	@cd docs; make html > /dev/null 2>&1

apidoc: ## Regenerates the docs/code-autodoc folder from the current source tree using sphinx-apidoc.
	@echo "regenerating: code-autodoc"
	@rm -f docs/code-autodoc/*
	@cd docs; sphinx-apidoc --no-toc -o code-autodoc ../cobbler

man: ## Creates documentation and man pages using Sphinx
	@if python3 -c "from importlib.metadata import version; from sys import exit; exit(0 if tuple(int(p) for p in version('setuptools_scm').split('.')[:2]) >= (8, 2) else 1)" 2>/dev/null; then \
		python3 -m setuptools_scm --force-write-version-files > /dev/null; \
	else \
		python3 setup.py --version > /dev/null 2>&1; \
	fi
	@cd docs; make man > /dev/null 2>&1
	@mkdir -p cobbler/data/man/man1 cobbler/data/man/man5 cobbler/data/man/man8
	@find docs/_build/man -name "*.1" -exec cp -f {} cobbler/data/man/man1/ \;
	@find docs/_build/man -name "*.5" -exec cp -f {} cobbler/data/man/man5/ \;
	@find docs/_build/man -name "*.8" -exec cp -f {} cobbler/data/man/man8/ \;

qa: ## If black is found then it is run.
ifeq ($(strip $(BLACK)),)
	@echo "No black found"
else
	@echo "checking: black ${BLACK}"
	@${BLACK} .
endif

authors: ## Creates the AUTHORS file.
	@echo "creating: AUTHORS"
	@cp AUTHORS.in AUTHORS
	@git config --global --add safe.directory /code
	@git log --format='%aN <%aE>' | grep -v 'root' | sort -u >> AUTHORS

release: clean qa authors ## Creates the full release.
	@echo "creating: release artifacts"
	@bash -c 'name=${NAME}; cd ..; tar --transform="s/${NAME}/${NAME}-${VERSION}/" --exclude ".idea" --exclude ".mypy_cache" --exclude="venv" --exclude "dist" --exclude "build" --exclude "rpm-build" --exclude "deb-build" -czf "$$name.tar.gz" "$$name"'
	@mkdir release
	@mv ../${NAME}.tar.gz ./release/${NAME}-${VERSION}.tar.gz

test-rocky10: ## Executes the testscript for testing cobbler in a docker container on Rocky Linux 10.
	./docker/rpms/build-and-install-rpms.sh rl10 docker/rpms/Rocky_Linux_10/Rocky_Linux_10.dockerfile

test-fedora41: ## Executes the testscript for testing cobbler in a docker container on Fedora 41.
	./docker/rpms/build-and-install-rpms.sh fc41 docker/rpms/Fedora_43/Fedora43.dockerfile

test-opensuse-leap: ## Executes the testscript for testing cobbler in a docker container on openSUSE Leap.
	./docker/rpms/build-and-install-rpms.sh opensuse-leap docker/rpms/opensuse_leap/openSUSE_Leap16.dockerfile

test-opensuse-tumbleweed: ## Executes the testscript for testing cobbler in a docker container on openSUSE Tumbleweed.
	./docker/rpms/build-and-install-rpms.sh opensuse-tumbleweed docker/rpms/opensuse_tumbleweed/openSUSE_TW.dockerfile

test-debian13: ## Executes the testscript for testing cobbler in a docker container on Debian 13.
	./docker/debs/build-and-install-debs.sh deb13 docker/debs/Debian_13/Debian13.dockerfile

system-test: ## Runs the system tests
	${PYTHON} -m pytest -m integration -k ${SYSTESTS}

build: authors man ## Runs the Python Build.
	@echo "building: Python package"
	@git config --global --add safe.directory /code; \
	${PYTHON} -m pip wheel --verbose --wheel-dir ./build .

install: build ## Runs the build target and then installs via setup.py
	@git config --global --add safe.directory /code; \
	${PYTHON} -m pip install --verbose --no-compile --no-deps --no-index --root $(DESTDIR) ./build/cobbler*.whl

rpms: release ## Runs the target release and then creates via rpmbuild the rpms in a directory called rpm-build.
	mkdir -p rpm-build/SOURCES
	cp release/*.gz rpm-build/SOURCES
	rpmbuild --define "_topdir %(pwd)/rpm-build" \
	--define '_rpmfilename %%{NAME}-%%{VERSION}-%%{RELEASE}.%%{ARCH}.rpm' \
	-ba cobbler.spec

# Only build a binary package
debs: authors ## Creates native debs in a directory called deb-build. The release target is called during the build process.
	@debuild -us -uc
	@mkdir -p deb-build; \
    cp ../cobbler_* deb-build/; \
    cp ../cobbler-tests* deb-build/; \
    cp ../cobbler-apache2_* deb-build/; \
    cp ../cobbler-nginx_* deb-build/

eraseconfig: ## Deletes the cobbler data jsons which are created when using the file provider.
	-rm /var/lib/cobbler/cobbler_collections/distros/*
	-rm /var/lib/cobbler/cobbler_collections/images/*
	-rm /var/lib/cobbler/cobbler_collections/profiles/*
	-rm /var/lib/cobbler/cobbler_collections/systems/*
	-rm /var/lib/cobbler/cobbler_collections/repos/*
	-rm /var/lib/cobbler/cobbler_collections/mgmtclasses/*
	-rm /var/lib/cobbler/cobbler_collections/files/*
	-rm /var/lib/cobbler/cobbler_collections/packages/*
	-rm /var/lib/cobbler/cobbler_collections/menus/*

.PHONY: system-test
