#!/usr/bin/make -f

PYTHONS:=$(shell pyversions -vr)

include /usr/share/openstack-pkg-tools/pkgos.make
export OSLO_PACKAGE_VERSION=$(shell dpkg-parsechangelog | grep Version: | cut -d' ' -f2 | sed -e 's/^[[:digit:]]*://' -e 's/[-].*//' -e 's/~/.0/' | head -n 1)

%:
	dh $@ --buildsystem=python_distutils --with python2,sphinxdoc

override_dh_auto_install:
	# Note: upstream advises to use "python setup.py compile_catalog"
	# maybe we should do that next time.
	chmod +x debian/compile-messages
	$(CURDIR)/debian/compile-messages

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

	install -d -m 755 $(CURDIR)/debian/tmp/usr/share/openstack-dashboard

	cp -a $(CURDIR)/openstack_dashboard/ $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/

	# Sets memcached by default in local_settings.py
	cp $(CURDIR)/openstack_dashboard/local/local_settings.py.example $(CURDIR)/debian/openstack-dashboard/usr/share/openstack-dashboard/local_settings.py
	sed -i -e 's/os.path.join(LOCAL_PATH,/os.path.join("\/","var","lib","openstack-dashboard","secret-key",/' $(CURDIR)/debian/openstack-dashboard/usr/share/openstack-dashboard/local_settings.py
	echo "COMPRESS_OFFLINE=True" >> $(CURDIR)/debian/openstack-dashboard/usr/share/openstack-dashboard/local_settings.py
	install -d -m 755 $(CURDIR)/debian/openstack-dashboard/var/lib/openstack-dashboard/static


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

override_dh_auto_clean:
	dh_auto_clean
	python setup.py clean
	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

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	sphinx-build -b html doc/source debian/horizon-doc/usr/share/doc/horizon-doc/html
	dh_sphinxdoc -O--buildsystem=python_distutils
endif

ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
override_dh_auto_test:
	# We should add --compilemessages when the .mo are removed.
	./run_tests.sh --no-virtual-env --no-pep8 --verbosity=2
endif

