#!/usr/bin/make -f

export DH_VERBOSE=1

export WLDIR=/usr/share/weblate
export WLDATADIR=/var/lib/weblate
export WLETCDIR=/etc/weblate

%:
	dh $@ --with python2,sphinxdoc,linktree

override_dh_auto_build:
	(cd docs && make html)
	dh_auto_build

override_dh_clean:
	find . -name '*.pyc' -exec rm {} \;
	rm -rf docs/_build
	rm -f weblate/settings.py
	dh_clean

override_dh_install:
	cp weblate/settings_example.py weblate/settings.py;
	sed -i 's@^BASE_DIR = .*@BASE_DIR = "${WLDIR}/weblate"@g' weblate/settings.py
	sed -i 's@^DATA_DIR = .*@DATA_DIR = "${WLDATADIR}"@g' weblate/settings.py
	sed -i "s@'NAME': 'weblate.db'@'NAME': '${WLDATADIR}/weblate.db'@" weblate/settings.py
	sed -i "s@/usr/share/weblate/data@${WLDATADIR}@" examples/apache.conf
	dh_install
	install --no-target-directory manage.py $(CURDIR)/debian/weblate/usr/bin/weblate-manage

	# Create Dirs
	install -d $(CURDIR)/debian/weblate/${WLDIR}
	install -d $(CURDIR)/debian/weblate/${WLETCDIR}

	# Remove test data
	rm -rf $(CURDIR)/debian/weblate/${WLDIR}/data-test

	# Move configuration to etc
	mv $(CURDIR)/debian/weblate/${WLDIR}/weblate/settings.py $(CURDIR)/debian/weblate/${WLETCDIR}/
	ln -s ${WLETCDIR}/settings.py $(CURDIR)/debian/weblate/${WLDIR}/weblate/settings.py

	# Apache config
	install -d $(CURDIR)/debian/weblate/${WLETCDIR}/../apache2/sites-available/
	install -m 644 examples/apache.conf $(CURDIR)/debian/weblate/${WLETCDIR}/../apache2/sites-available/weblate.conf

	# Whoosh index dir
	install -d $(CURDIR)/debian/weblate/${WLDATADIR}

	# Remove not needed link
	rm -rf $(CURDIR)/debian/weblate/${WLDIR}/weblate/ttf/

override_dh_link:
	rm -rf $(CURDIR)/debian/weblate/${WLDIR}/weblate/ttf/
	dh_link
