#!/usr/bin/make -f

include /usr/share/openstack-pkg-tools/pkgos.make

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

override_dh_install:
	echo "Do nothing..."

override_dh_auto_clean:
	python3 setup.py clean
	find . -iname '*.mo' -delete
	rm -rf build .coverage .pytest_cache
	rm -rf $(CURDIR)/static
	rm -rf $(CURDIR)/horizon/build
	rm -rf $(CURDIR)/horizon.egg-info
	rm -rf $(CURDIR)/openstack_dashboard/openstack_dashboard.egg-info
	rm -rf doc/source/sourcecode
	rm -rf $(CURDIR)/doc/build
	rm -f horizon/openstack_dashboard/test/.secret_key_store
	find $(CURDIR)/openstack_dashboard -iname '*.pyc' -delete
	rm -rf .eggs
	rm -f AUTHORS
	rm -f ChangeLog
	find . -iname '*.pyc' -delete
	for i in $$(find . -type d -iname __pycache__) ; do rm -rf $$i ; done
	rm -f openstack_dashboard/locale/djangojs.pot openstack_dashboard/locale/django.pot openstack_dashboard/local/_build_horizon* .test_secret_key_store.lock horizon/locale/django.pot horizon/locale/djangojs.pot
	rm -f openstack_auth/locale/django.pot openstack_auth/locale/djangojs.pot
	rm -f openstack_dashboard/local/.secret_key_store

override_dh_auto_build:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	python3 -m sphinx -b html doc/source debian/horizon-doc/usr/share/doc/horizon-doc/html
endif

override_dh_auto_install:

	# Compile language files
	chmod +x debian/compile-messages
	$(CURDIR)/debian/compile-messages

	set -e && for pyvers in $(PYTHON3S); do \
		python$$pyvers setup.py install \
			--install-layout=deb \
			--root $(CURDIR)/debian/tmp \
			--no-compile -O0; \
	done

	# Prepare openstack-dashboard folder and copy manage.py
	install -d -m 755 $(CURDIR)/debian/tmp/usr/share/openstack-dashboard
	cp $(CURDIR)/manage.py $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/

	# Move themes folder to standard location
	mv -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/themes \
		$(CURDIR)/debian/tmp/usr/share/openstack-dashboard/

	# Create /etc/openstack-dashboard/ and move policy files into
	install -d -m 755 $(CURDIR)/debian/tmp/etc/openstack-dashboard/
	mv -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/conf \
		$(CURDIR)/debian/tmp/etc/openstack-dashboard/policy

	# Move conf to standard location
	mv -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/local/enabled \
		$(CURDIR)/debian/tmp/etc/openstack-dashboard/
	rm -f $(CURDIR)/debian/tmp/etc/openstack-dashboard/enabled/__init__.py
	rm -f $(CURDIR)/debian/tmp/etc/openstack-dashboard/enabled/*.example

	# Move wsgi file to standard location
	mv $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/wsgi.py \
		$(CURDIR)/debian/tmp/usr/share/openstack-dashboard/

	# Move local_settings.d to /etc and modify local_settings.py
	mv -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/local/local_settings.d \
		$(CURDIR)/debian/tmp/etc/openstack-dashboard/
	rm -f $(CURDIR)/debian/tmp/etc/openstack-dashboard/local_settings.d/*.example

	install -d -m 755 $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/examples
	mv -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/local/local_settings.py.example \
		$(CURDIR)/debian/tmp/usr/share/openstack-dashboard/examples/local_settings.py

	# Secret key has to be replaced here
	sed -i -e 's/os.path.join(LOCAL_PATH,/os.path.join("\/","var","lib","openstack-dashboard","secret-key",/' $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/examples/local_settings.py

	# Set debug to false in local.settings.d not working as expected
	# So, it needs to be replaced to false directly in local_settings.py
	sed -i -e 's/DEBUG = True/DEBUG = False/g' $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/examples/local_settings.py

	# Move osprofiler conf files to be enabled to standard location
	# and remove /usr/lib/python3/dist-packages/openstack_dashboard/contrib/developer/enabled
	mv -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/contrib/developer/enabled/_[0-9]*.py \
		$(CURDIR)/debian/tmp/etc/openstack-dashboard/enabled/
	rm -rf $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/contrib/developer/enabled

	# Remove secret_key_store as we will use it in /var/lib/
	rm -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/local/.secret_key_store

	## Delete __pycache__
	find . -name __pycache__ -prune -exec rm -rf {} +;

	## Delete not needed files
	rm -f $(CURDIR)/debian/tmp/usr/lib/python3/dist-packages/openstack_dashboard/local/_build*.lock

	dh_install
	dh_missing --fail-missing
	find $(CURDIR)/debian -iname .eslintrc -delete

	# Redering this file looks like the cause of #1069097.
	# Not a lot of people would be using the serial console
	# thingy, but more likely the VNC or Spice console.
	# Threfore, as a workaround, let's just delete it.
#	rm $(CURDIR)/debian/python3-django-horizon/usr/lib/python3/dist-packages/openstack_dashboard/templates/serial_console.html

override_dh_auto_test:
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# Blacklist:
	# test_site_branding_tag: https://bugs.launchpad.net/horizon/+bug/1980214
	# We should add --compilemessages when the .mo are removed.
	# Ignore openstack_dashboard/dashboards/project/backups/tests.py,
	# see https://bugs.debian.org/1026610 and https://github.com/python/cpython/commit/92a6abf72e7a8274f96edbb5297119d4ff055be7
	set -e ; set -x ; for i in $(PYTHON3S) ; do \
		http_proxy=127.0.0.1:9 https_proxy=127.0.0.9: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 coverage run -a -m pytest horizon/test/ -n $$(nproc --all) -v --ds=horizon.test.settings -m "not selenium and not integration" ; \
		PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages PYTHON=python$$i python$$i -m coverage run -a -m pytest --ignore=openstack_dashboard/test/unit/api/rest/test_policy.py openstack_dashboard -v --ds=openstack_dashboard.test.settings --ignore openstack_dashboard/dashboards/project/backups/tests.py --ignore openstack_dashboard/test/integration_tests --ignore openstack_dashboard/test/selenium -k "not selenium and not integration and not test_instance_details_metadata" ; \
		PYTHONPATH=$(CURDIR)/debian/tmp/usr/lib/python3/dist-packages PYTHON=python$$i python$$i -m coverage run -a -m pytest openstack_auth/tests -n $$(nproc --all) -v --ds=openstack_auth.tests.settings -m "not selenium and not integration" ; \
	done
	## Delete __pycache__
	find . -name __pycache__ -prune -exec rm -rf {} +;
endif