#!/usr/bin/make -f

export PYBUILD_NAME = pagure

DESTDIR = debian/pagure
export PYBUILD_DESTDIR = $(DESTDIR)

DESTDIR_EV_SERVER = debian/pagure-ev-server

# The user we create when the package is installed.
PAGURE_USER = _pagure

%:
	dh $@ --with python3,sphinxdoc,apache2 --buildsystem=pybuild

override_dh_auto_build:
	dh_auto_build
	PYTHONPATH=$(CURDIR) http_proxy='127.0.0.1:9' make -C doc/ \
	    html SPHINXBUILD=sphinx-build
# For the manpage, we don't need to include the changelog.  This also
# prevents an annoying lintian warning.
	PYTHONPATH=$(CURDIR) http_proxy='127.0.0.1:9' make -C doc/ \
            man SPHINXBUILD=sphinx-build \
	    SPHINXOPTS='-D exclude_patterns=changelog.rst'

# Unfortunately, there are a lot of tweaks we need to do after
# auto_install.  For each Python 3 version available, we need to:
#
# 1) Iterate over the *.js files (but _not_ *.min.js), and minify it
# using yui-compressor.
#
# 2) Likewise, but for *.css files.
#
# 3) Iterate over the symlinks under pagure/hooks/files/, check if it
# points to a non-existent file, and remove it if it does.
#
# 4) Iterate over the font files and remove the executable bits from
# them.
override_dh_auto_install:
	dh_auto_install
	for py in $(shell py3versions -rv); do \
	    paguredir=$(DESTDIR)/usr/lib/python$${py}/dist-packages/pagure; \
	    themesdir=$${paguredir}/themes; \
	    vendordir=$${paguredir}/static/vendor; \
	    find $${vendordir} $${themesdir} -name "*.js" ! -name "*.min.js" \
	      | sed 's/\.js$$//' | while read file ; do \
	        yui-compressor -o $${file}.min.js $${file}.js; \
	    done; \
	    find $${vendordir} $${themesdir} -name "*.css" ! -name "*.min.css" \
	      | sed 's/\.css$$//' | while read file ; do \
	        yui-compressor -o $${file}.min.css $${file}.css; \
	    done; \
	    find $${paguredir}/hooks/files/ -type l \
	      | while read file; do \
	        if stat $${file} | grep -q '\/does\/not\/exist'; then \
	            rm -fv $${file}; \
	        fi; \
	    done; \
	done

override_dh_install:
	dh_install
# We install the directories below under /usr/share/pagure, so we have
# to make sure to delete them from $(DESTDIR).
	rm -rfv $(DESTDIR)/usr/lib/python*/dist-packages/pagure/static/
	rm -rfv $(DESTDIR)/usr/lib/python*/dist-packages/pagure/templates/
	rm -rfv $(DESTDIR)/usr/lib/python*/dist-packages/pagure/themes/

# These are helper scripts, but they need to be marked executable.
override_dh_fixperms:
	dh_fixperms
	chmod -v +x \
	    $(DESTDIR)/usr/share/pagure/aclchecker.py \
	    $(DESTDIR)/usr/share/pagure/keyhelper.py \
	    $(DESTDIR)/usr/share/pagure/mirror_project_in.py \
	    $(DESTDIR_EV_SERVER)/usr/share/pagure-ev-server/bin/pagure_stream_server.py

override_dh_installsystemd-indep:
	dh_installsystemd --no-enable --no-start --package pagure --name pagure-worker
	dh_installsystemd --no-enable --no-start --package pagure --name pagure-gitolite-worker
	dh_installsystemd --no-enable --no-start --package pagure --name pagure-api-key-expire-mail
	dh_installsystemd --no-enable --no-start --package pagure --name pagure-mirror-project-in
	dh_installsystemd --no-enable --no-start --package pagure --name pagure-authorized-keys-worker
	dh_installsystemd --no-enable --no-start --remaining-packages
# The service files come with user/group "git" (or "mirror", in the
# case of pagure-mirror) by default.  We have to replace it with our
# own user/group $(PAGURE_USER).
	find debian/ -type f -name "*.service" \
	  | xargs sed -i -e "s/User=\(git\|mirror\)/User=$(PAGURE_USER)/" \
		      -e "s/Group=\(git\|mirror\)/Group=$(PAGURE_USER)/"

# The Apache2 configuration file that is installed must be edited and
# adjusted by the user before it can be enabled.
override_dh_apache2:
	dh_apache2 --noenable

# - Disable test_dev_data until dev-data.py is shipped upstream:
#   https://pagure.io/pagure/issue/4741
#
# - Disable test_upload_issue, due to non-determinism.
#
# - Disable test_upload_issue_virus because it requires a running
#   clamav-server instance.
#
# - Disable black compliance check, and test_code_with_flake8.  They
#   require +rw access on /dev/shm for POSIX semaphores, which isn't
#   always possible on chroot.
#
# - Disable test_view_issue_custom_field_user_ticket, due to
#   non-determinism.  See <https://pagure.io/pagure/issue/4798>.
export PYBUILD_TEST_ARGS = \
        --exclude=test_dev_data \
        --exclude=test_upload_issue \
        --exclude=test_upload_issue_virus \
        --exclude=test_view_issue_raw_file \
        --exclude=test_code_with_black \
        --exclude=test_code_with_flake8 \
	--exclude=test_view_issue_custom_field_user_ticket

# Necessary for the tests.
export PAGURE_CONFIG = ../tests/test_config

# The testsuite does not pass completely when run offline.  I still
# think it is useful to have it run.
override_dh_auto_test:
	dh_auto_test || true
