#!/usr/bin/make -f

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

%:
	dh $@  --with python3 --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
	dh_auto_clean
	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
	# install vendorfied xstatic assets
	cp -r xstatic $(CURDIR)/debian/tmp/usr/lib/$(PY3VER)/dist-packages/horizon
	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

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# Skip tests
	:
endif

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