#!/usr/bin/make -f
#export DH_VERBOSE = 1
export HOME = $(shell pwd)
export PYBUILD_NAME=libpysal
export PYBUILD_TEST_CUSTOM=1

SRC_DIR="usr/lib/python3/dist-packages/libpysal/examples/"
DEST_DIR="usr/share/doc/python3-libpysal/examples"


%:
	dh $@ --sphinxdoc  --buildsystem=pybuild

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	 pytest-3 -v --ignore=libpysal/graph/tests/ \
        --ignore=libpysal/weights/tests/test_contiguity.py \
        --ignore=libpysal/cg/tests/test_geoJSON.py \
        --ignore=libpysal/weights/tests/test_util.py \
        --ignore=libpysal/examples/tests/test_available.py \
        --ignore=libpysal/io/iohandlers/tests/test_db.py
	dh_auto_test
endif

# Build sphinx HTML documentation
override_dh_auto_build:
	dh_auto_build
	PYTHONPATH=. https_proxy='https://127.0.0.1:9/' no_proxy=localhost python3 -m sphinx -N -d debian/doctrees  -bhtml docs/ debian/html


override_dh_link:
	dh_link
# Fixe privacy-breach-generic
	find debian/python-libpysal-doc/  -name '*.html' -exec sed -i 's/.*https:.*//g' {} \;

#fixe package-contains-documentation-outside-usr-share-doc
	rm -rf debian/python3-libpysal/usr/lib/python3/dist-packages/lattice.dbf
	rm -rf debian/python3-libpysal/usr/lib/python3/dist-packages/libpysal/.gitignore
	rm -rf debian/python3-libpysal/usr/share/doc/python3-libpysal/examples/__pycache__
	rm -rf debian/python3-libpysal/usr/lib/python3/dist-packages/core
	rm -rf debian/python-libpysal-doc/usr/share/doc/python-libpysal-doc/html/_static/bootstrap-3.4.1/fonts/
	rm -rf debian/python-libpysal-doc/usr/share/doc/python-libpysal-doc/html/_static/bootswatch-3.4.1/fonts/
	find debian/python-libpysal-doc/usr/share/doc -name '*.js' -delete
	find debian/python-libpysal-doc/usr/share/doc -type d -empty -delete
#Fixe duplicate-files
	jdupes -rl debian/python-libpysal-doc/usr
	jdupes -rl debian/python3-libpysal/usr

# Moved libpysal/examples directory to usr/lib/ for testing "esda" package
	# Create the destination directory
	mkdir -p debian/python3-libpysal/$(DEST_DIR)

# maintain the "examples" subdirectory necessary to run the "esda" package tests
	for dir in debian/python3-libpysal/$(SRC_DIR)*/; do \
		dir=$$(basename "$$dir"); \
		if [ "$$dir" != "tests" ] && \
		[ "$$dir" != "Point" ] && \
		[ "$$dir" != "Polygon_Holes" ] && \
		[ "$$dir" != "baltim" ] && \
		[ "$$dir" != "berlin" ] && \
		[ "$$dir" != "calemp" ] && \
		[ "$$dir" != "columbus" ] && \
		[ "$$dir" != "geodanet" ] && \
		[ "$$dir" != "juvenile" ] && \
		[ "$$dir" != "networks" ] && \
		[ "$$dir" != "sids2" ] && \
		[ "$$dir" != "stl" ] && \
		[ "$$dir" != "us_income" ] && \
		[ "$$dir" != "wmat" ] && \
		[ "$$dir" != "Line" ] && \
		[ "$$dir" != "Polygon" ] && \
		[ "$$dir" != "arcgis" ] && \
		[ "$$dir" != "base.py" ] && \
		[ "$$dir" != "book" ] && \
		[ "$$dir" != "burkitt" ] && \
		[ "$$dir" != "chicago" ] && \
		[ "$$dir" != "desmith" ] && \
		[ "$$dir" != "georgia" ] && \
		[ "$$dir" != "mexico" ] && \
		[ "$$dir" != "remotes.py" ] && \
		[ "$$dir" != "snow_maps" ] && \
		[ "$$dir" != "street_net_pts" ] && \
		[ "$$dir" != "tokyo" ] && \
		[ "$$dir" != "virginia" ] && \
		[ -d "debian/python3-libpysal/$(SRC_DIR)$$dir" ]; then \
		mv "debian/python3-libpysal/$(SRC_DIR)$$dir" "debian/python3-libpysal/$(DEST_DIR)"; \
		fi; \
	done
