#!/usr/bin/make -f

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

%:
	dh $@  --with python2,sphinxdoc

PYVER=$(shell pyversions -d)

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:
	#dh_auto_build

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

	# install horizon and dashboard
	python setup.py install --root=$(CURDIR)/debian/tmp --no-compile -O0 --install-layout=deb
	# install vendorfied xstatic assets
	#cp -r xstatic $(CURDIR)/debian/tmp/usr/lib/$(PYVER)/dist-packages/horizon
	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

	cp -a $(CURDIR)/openstack_dashboard/ $(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

	# Fix path of WEBROOT to be /horizon and not just /
	sed -i "s#^WEBROOT[ \t]*=.*#WEBROOT = '/horizon'#" $(CURDIR)/debian/tmp/etc/openstack-dashboard/local_settings.py
	# Fix path of $webroot to be /horizon and not just / or /dashboard for scss
	sed -i 's#^$$webroot: ".*"#$$webroot: "/horizon"#' \
	    $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/openstack_dashboard/static/themes/webroot/_variables.scss
	# Fix path of secret-key (ie: in /var, as per Debian policy)
	sed -i -e 's/os.path.join(LOCAL_PATH,/os.path.join("\/","var","lib","openstack-dashboard","secret-key",/' \
		$(CURDIR)/debian/tmp/etc/openstack-dashboard/local_settings.py
	# Activate off-line compress
	echo "COMPRESS_OFFLINE=True" >> $(CURDIR)/debian/tmp/etc/openstack-dashboard/local_settings.py

	# Copy the missing angular folder
	cp -auxf horizon/static/angular $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/openstack_dashboard/static

	cp $(CURDIR)/manage.py \
		 $(CURDIR)/debian/tmp/usr/share/openstack-dashboard/manage.py
	ln -fs /etc/openstack-dashboard/local_settings.py \
		$(CURDIR)/debian/tmp/usr/share/openstack-dashboard/openstack_dashboard/local/local_settings.py
	ln -fs /usr/lib/$(PYVER)/dist-packages/horizon/static/horizon \
		$(CURDIR)/debian/tmp/usr/share/openstack-dashboard/openstack_dashboard/static/horizon

        # Link to awesome fonts
	install -d -m 755 $(CURDIR)/debian/tmp/usr/lib/$(PYVER)/dist-packages/horizon/static/horizon/lib/font-awesome
	ln -fs /usr/share/fonts-font-awesome/fonts $(CURDIR)/debian/tmp/usr/lib/$(PYVER)/dist-packages/horizon/static/horizon/lib/font-awesome/fonts

override_dh_link:
	rm -rf $(CURDIR)/debian/openstack-dashboard/usr/share/openstack-dashboard/openstack_dashboard/static/dashboard/js
	rm -rf $(CURDIR)/debian/openstack-dashboard/usr/share/openstack-dashboard/openstack_dashboard/static/dashboard/css
	rm -rf $(CURDIR)/debian/openstack-dashboard/usr/share/pyshared/horizon/static/horizon/lib/font-awesome/fonts
	dh_link

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

override_dh_sphinxdoc:
ifeq (,$(findstring nodocs, $(DEB_BUILD_OPTIONS)))
	python setup.py build_sphinx
	cp -rf doc/build/html $(CURDIR)/debian/horizon-doc/usr/share/doc/horizon-doc
	dh_sphinxdoc -O--buildsystem=python_distutils
endif
