#!/usr/bin/make -f

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

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

PY3VER=python3
PYTHON3S:=$(shell py3versions -vr)

DEB_VERSION := $(shell dpkg-parsechangelog | egrep '^Version:' | cut -f 2 -d ' ')
DEB_NOEPOCH_VERSION := $(shell echo $(DEB_VERSION) | cut -d: -f2-)
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_NOEPOCH_VERSION) | sed 's/-[^-]*$$//')

override_dh_auto_clean:
	find . -name "django*.mo" -delete || :
	rm -f horizon/xstatic
	rm -rf build .pybuild
	rm -f horizon/openstack_dashboard/local/.secret_key_store

override_dh_auto_build:
	# NOTE(jamespage) this is a little horrible but works around
	# the switch to /var/lib/openstack once installed for lockfiles
	ln -sf ../xstatic horizon/xstatic
	patch -p1 -R <debian/patches/ubuntu_settings.patch
	python3 ./manage.py compilemessages
	patch -p1 <debian/patches/ubuntu_settings.patch
	dh_auto_build

override_dh_auto_install:
	# NOTE(jamespage):
	# This looks odd but it forces a re-generation of SOURCES.txt
	# to include the mo files generated during compilemessages
	# in override_dh_auto_build to ensure that they get installed
	# into binary packages.
	rm -f horizon.egg-info/SOURCES.txt
	# install horizon and dashboard
	set -e ; for pyvers in $(PYTHON3S); do \
		python$$pyvers setup.py install --no-compile -O0 --install-layout=deb \
			--root $(CURDIR)/debian/tmp; \
	done

	# Replace the build-time symlink of horizon/xstatic from the staging area and
	# source with real xstatic directory.
	rm -rf $(CURDIR)/debian/tmp/usr/lib/$(PY3VER)/dist-packages/horizon/xstatic
	rm -f horizon/xstatic

	# Install vendorfied xstatic assets
	cp -r xstatic $(CURDIR)/debian/tmp/usr/lib/$(PY3VER)/dist-packages/horizon

	# Install the ubuntu theme into its own subdirectory
	install -d $(CURDIR)/debian/tmp/usr/lib/$(PY3VER)/dist-packages/openstack_dashboard/themes
	cp -r debian/theme/ubuntu $(CURDIR)/debian/tmp/usr/lib/$(PY3VER)/dist-packages/openstack_dashboard/themes/
	install -d -m 755 $(CURDIR)/debian/tmp/usr/share/openstack-dashboard
	install -d -m 755 $(CURDIR)/debian/tmp/etc/openstack-dashboard
	install -d -m 755 $(CURDIR)/debian/tmp/etc/apache2/conf-available

	install -d -m 755 $(CURDIR)/debian/tmp/etc/openstack-dashboard
	cp -a $(CURDIR)/openstack_dashboard/settings.py $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/

	cp $(CURDIR)/openstack_dashboard/local/local_settings.py.example \
		$(CURDIR)/debian/tmp/etc/openstack-dashboard/local_settings.py
	cp $(CURDIR)/manage.py \
		 $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/manage.py

	# NOTE(jamespage): Link local_settings.py to both py version locations
	ln -fs /etc/openstack-dashboard/local_settings.py \
		$(CURDIR)/debian/tmp/usr/lib/$(PY3VER)/dist-packages/openstack_dashboard/local/local_settings.py

	# Delete .eslintrc files and bootswatch demo pages
	find $(CURDIR)/debian -iname .eslintrc -delete
	find $(CURDIR)/debian/tmp -path '*/bootswatch/data/*/index.html' -delete
	find $(CURDIR)/debian/tmp -path '*/bootswatch/data/*/thumbnail.png' -delete

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	set -e ; set -x ; for i in $(PYTHON3S) ; do \
		http_proxy=127.0.0.1:9 https_proxy=127.0.0.1:9 \
		HTTP_PROXY=127.0.0.1:9 HTTPS_PROXY=127.0.0.1:9 \
		PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages PYTHON=python$$i python$$i -m pytest horizon/test/ -v --ds=horizon.test.settings -m "not selenium and not integration" --ignore horizon/test/unit/hacking/test_checks.py ; \
		PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages PYTHON=python$$i python$$i -m pytest openstack_dashboard -v --ds=openstack_dashboard.test.settings --ignore openstack_dashboard/test/integration_tests --ignore openstack_dashboard/test/selenium --ignore openstack_dashboard/dashboards/identity/projects/tests.py --ignore openstack_dashboard/dashboards/identity/users/tests.py --ignore openstack_dashboard/dashboards/project/images/tests.py -k "not selenium and not integration" ; \
		PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages PYTHON=python$$i python$$i -m pytest openstack_auth/tests -v --ds=openstack_auth.tests.settings -m "not selenium and not integration" ; \
	done
	## Delete __pycache__
	find . -name __pycache__ -prune -exec rm -rf {} +;
endif

refresh-xstatic:
	bash debian/bundle-xstatic.sh $(DEB_UPSTREAM_VERSION)
