.PHONY: default
default: build
default: opt=--inplace

package = slepc4py
MODULE  = SLEPc

PYTHON  = python3$(py)
MPIEXEC = mpiexec
LOC    ?= ${SLEPC_DIR}/${PETSC_ARCH}/doc/html

# ----

.PHONY: config build test
config:
	${PYTHON} setup.py config ${opt}
build:
	${PYTHON} setup.py build ${opt}
test:
	${VALGRIND} ${PYTHON} ${PWD}/test/runtests.py ${opt}
test-%:
	${MPIEXEC} -n $* ${VALGRIND} ${PYTHON} ${PWD}/test/runtests.py ${opt}

.PHONY: srcbuild srcclean
srcbuild:
	${PYTHON} setup.py build_src ${opt}
srcclean:
	-${RM} src/${package}/${MODULE}.c
	-${RM} src/${package}/${MODULE}.h
	-${RM} src/${package}/${MODULE}_api.h

.PHONY: clean distclean fullclean
clean: srcclean
	${PYTHON} setup.py clean --all
distclean: clean
	-${RM} -r build  _configtest.* *.py[co]
	-${RM} -r MANIFEST dist ${package}.egg-info
	-${RM} -r `find . -name '__pycache__'`
	-${RM} `find . -name '*.py[co]'`
fullclean: distclean srcclean docsclean
	-find . -name '*~' -exec rm -f {} ';'

# ----

.PHONY: install uninstall
install: build
	${PYTHON} setup.py install --prefix='' --user ${opt}
uninstall:
	-${RM} -r $(shell ${PYTHON} -m site --user-site)/${package}
	-${RM} -r $(shell ${PYTHON} -m site --user-site)/${package}-*-py*.egg-info

# ----

.PHONY: lint ruff-lint cython-lint
CYTHONLINT_SRC = src demo
CYTHONLINT_OPTS =
RUFF_SRC = .
RUFF_OPTS = check

cython-lint:
	conf/cythonize.sh -Wextra -Werror
	cython-lint ${CYTHONLINT_OPTS} ${CYTHONLINT_SRC}


ruff-lint:
	ruff ${RUFF_OPTS} ${RUFF_SRC}

lint:
#	${RM} -r slepc4py-lint-env
	${PYTHON} -m venv ../../../doc/.venv-sphinx
	. ../../../doc/.venv-sphinx/bin/activate && python3 -m pip install -r ../../../doc/requirements.txt
	. ../../../doc/.venv-sphinx/bin/activate && ${MAKE} cython-lint
#	. ../../../doc/.venv-sphinx/bin/activate && ${MAKE} ruff-lint

# ----

.PHONY: docs docs-html docs-pdf docs-misc
docs: docs-html docs-pdf docs-misc
docs-html: rst2html sphinx-html
docs-pdf:  sphinx-pdf
docs-misc: sphinx-man sphinx-info

RST2HTML = $(shell command -v rst2html || command -v rst2html.py || false)
RST2HTMLOPTS  = --input-encoding=utf-8
RST2HTMLOPTS += --no-compact-lists
RST2HTMLOPTS += --cloak-email-addresses
.PHONY: rst2html
rst2html:
	${RST2HTML} ${RST2HTMLOPTS} ./LICENSE.rst  > docs/LICENSE.html
	${RST2HTML} ${RST2HTMLOPTS} ./CHANGES.rst  > docs/CHANGES.html
	${RST2HTML} ${RST2HTMLOPTS} docs/index.rst > docs/index.html

SPHINXBUILD = sphinx-build
SPHINXOPTS  =
.PHONY: sphinx sphinx-html sphinx-pdf sphinx-man sphinx-info
sphinx: sphinx-html sphinx-pdf sphinx-man sphinx-info
sphinx-html:
	mkdir -p build/doctrees docs/html
	${SPHINXBUILD} -a -b html -d build/doctrees ${SPHINXOPTS} \
	docs/source docs/html
	${RM} docs/html/.buildinfo
sphinx-pdf:
	mkdir -p build/doctrees build/latex
	${SPHINXBUILD} -b latex -d build/doctrees ${SPHINXOPTS} \
	docs/source build/latex
	${MAKE} -C build/latex all-pdf > /dev/null
	mv build/latex/*.pdf docs/
sphinx-man:
	mkdir -p build/doctrees build/man
	${SPHINXBUILD} -b man -d build/doctrees ${SPHINXOPTS} \
	docs/source build/man
	mv build/man/*.[137] docs/
sphinx-info:
	mkdir -p build/doctrees build/texinfo
	${SPHINXBUILD} -b texinfo -d build/doctrees ${SPHINXOPTS} \
	docs/source build/texinfo
	${MAKE} -C build/texinfo info > /dev/null
	mv build/texinfo/*.info docs/

.PHONY: docsclean
docsclean:
	-@${RM} docs/*.info docs/*.[137]
	-@${RM} docs/*.html docs/*.pdf
	-@${RM} -r docs/html
	-@${RM} -r docs/source/demo
	-@${RM} -r docs/source/reference
	-@${RM} docs/source/*.inv
	-@${RM} -r docs/source/_static

.PHONY: webstatic
webstatic:
	@if [ ! -d ../../../doc/source/_static ]; then \
		git clone -b main --depth=1 https://gitlab.com/slepc/webstatic.git ../../../doc/source/_static; \
	else \
		cd ../../../doc/source/_static; \
		git pull; \
		if [ $$? -ne 0 ]; then \
			cd - ; \
			rm -rf ../../../doc/source/_static; \
			git clone -b main --depth=1 https://gitlab.com/slepc/webstatic.git ../../../doc/source/_static; \
		fi ;\
		cd - ;\
	fi ;\
	if [ ! -d docs/source/_static/css ]; then mkdir -p docs/source/_static/css ; fi;\
	if [ ! -e docs/source/_static/css/slepc.css ]; then  ln -s ../../../../../../../doc/source/_static/css/slepc.css docs/source/_static/css/slepc.css ; fi

.PHONY: website
website: webstatic
#	CFLAGS=-O0 python -m pip install .
	@python3 -m venv ../../../doc/.venv-sphinx && \
	. ../../../doc/.venv-sphinx/bin/activate && \
	python3 -m pip install --quiet -r ../../../doc/requirements.txt &&\
	${MAKE} sphinx-html SPHINXOPTS="-j4 -E -T -W" && \
	rm -rf ${LOC}/slepc4py && \
	mkdir -p ${LOC}/slepc4py && \
	mv docs/html/* ${LOC}/slepc4py && \
	if [ -e docs/source/_static/css/slepc.css ]; then rm docs/source/_static/css/slepc.css; fi

# ----

.PHONY: sdist
sdist:
	${PYTHON} setup.py sdist ${opt}

# ----
