#!/usr/bin/make -f
# -*- makefile -*-

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

# This has to be exported to make some magic below work.
export DH_OPTIONS

SVN_ROOT ?= ../svnroot
DEST = debian
DEST_MAIN = debian/yandex-du-releaser
DEST_FRONTEND = debian/yandex-du-releaser-frontend
DEST_SCRIPTS = debian/yandex-du-releaser-scripts

build:
	echo nop

binary: install
	echo nop

clean:
	dh_testdir
	dh_testroot
	dh_clean
	rm -f build-arch-stamp build-indep-stamp #CONFIGURE-STAMP#

	# Add here commands to clean up after the build process.
	-$(MAKE) clean

	dh_clean

install:
	dh_testdir
	dh_testroot
	dh_clean -k -i
	dh_installdirs
	dh_installcron

	# main
	install -d $(DEST_MAIN)/var/www/
	rsync -a --delete --exclude="packages" --exclude=".svn" $(SVN_ROOT)/ $(DEST_MAIN)/var/www/releaser/
	python -c 'import compileall, sys, re; compileall.compile_dir(sys.argv[1], rx=re.compile("/[.]svn"), quiet=True)' $(DEST_MAIN)/var/www/releaser/
	chmod 0777 $(DEST_MAIN)/var/www/releaser/logs
	chmod 0777 $(DEST_MAIN)/var/www/releaser/releaser/static/upload
	chmod 0777 $(DEST_MAIN)/var/www/releaser/releaser/locks

	# frontend
	install -o www-data -g www-data -d \
		$(DEST_FRONTEND)/var/cache/lighttpd.tabula/compress \
		$(DEST_FRONTEND)/var/cache/lighttpd.tabula/uploads \
		$(DEST_FRONTEND)/var/log/lighttpd.tabula \
		$(DEST_FRONTEND)/var/run/lighttpd.tabula

	install -d \
		$(DEST_FRONTEND)/etc/init.d \
		$(DEST_FRONTEND)/etc/lighttpd

	install $(SVN_ROOT)/etc/frontend/lighttpd.conf $(DEST_FRONTEND)/etc/lighttpd/tabula.conf
	install -m 755 $(SVN_ROOT)/etc/frontend/lighttpd.init $(DEST_FRONTEND)/etc/init.d/lighttpd.tabula

	install -d $(DEST_FRONTEND)/etc/lighttpd/conf-enabled
	#quick'n'dirty поддержка "бет" Табулы: явное сравнение с нужными номерами, т.к. подставить в lighttpd качестве порта переменную из регвыр с ходу не получается
	tpage $(SVN_ROOT)/etc/frontend/lighttpd-betas.conf > $(DEST_FRONTEND)/etc/lighttpd/lighttpd-betas.conf

	install -d \
		$(DEST_FRONTEND)/var/log/releaser-direct-dev.yandex-team.ru/ \
		$(DEST_FRONTEND)/var/log/releaser-javadirect-dev.yandex-team.ru/ \
		$(DEST_FRONTEND)/var/log/releaser-directmod-dev.yandex-team.ru/ \
		$(DEST_FRONTEND)/var/log/releaser-geocontext-dev.yandex-team.ru/

	chown -R www-data:www-data \
		$(DEST_FRONTEND)/var/log/releaser-direct-dev.yandex-team.ru/ \
		$(DEST_FRONTEND)/var/log/releaser-javadirect-dev.yandex-team.ru/ \
		$(DEST_FRONTEND)/var/log/releaser-directmod-dev.yandex-team.ru/ \
		$(DEST_FRONTEND)/var/log/releaser-geocontext-dev.yandex-team.ru/

	install -d \
		$(DEST_FRONTEND)/var/www/releaser/tmp/direct/egg_cache \
		$(DEST_FRONTEND)/var/www/releaser/tmp/javadirect/egg_cache \
		$(DEST_FRONTEND)/var/www/releaser/tmp/directmod/egg_cache \
		$(DEST_FRONTEND)/var/www/releaser/tmp/geocontext/egg_cache

	chown -R www-data:www-data \
		$(DEST_FRONTEND)/var/www/releaser/tmp/direct/ \
		$(DEST_FRONTEND)/var/www/releaser/tmp/javadirect/ \
		$(DEST_FRONTEND)/var/www/releaser/tmp/directmod/ \
		$(DEST_FRONTEND)/var/www/releaser/tmp/geocontext/

	chmod -R go+w \
		$(DEST_FRONTEND)/var/www/releaser/tmp/direct/ \
		$(DEST_FRONTEND)/var/www/releaser/tmp/javadirect/ \
		$(DEST_FRONTEND)/var/www/releaser/tmp/directmod/ \
		$(DEST_FRONTEND)/var/www/releaser/tmp/geocontext/

	/usr/bin/django-admin create_package_files --settings=releaser.settings.direct --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_FRONTEND) --frontend
	/usr/bin/django-admin create_package_files --settings=releaser.settings.javadirect --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_FRONTEND) --frontend
	/usr/bin/django-admin create_package_files --settings=releaser.settings.directmod --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_FRONTEND) --frontend
	/usr/bin/django-admin create_package_files --settings=releaser.settings.geocontext --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_FRONTEND) --frontend

	# scripts
	install -d $(DEST_SCRIPTS)/etc/cron.d
	/usr/bin/django-admin create_package_files --settings=releaser.settings.direct --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_SCRIPTS) --scripts
	/usr/bin/django-admin create_package_files --settings=releaser.settings.javadirect --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_SCRIPTS) --scripts
	/usr/bin/django-admin create_package_files --settings=releaser.settings.directmod --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_SCRIPTS) --scripts
	/usr/bin/django-admin create_package_files --settings=releaser.settings.geocontext --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_SCRIPTS) --scripts
	install -d $(DEST_SCRIPTS)/etc/switchman
	install -m 0644 $(SVN_ROOT)/etc/tabula-switchman.conf $(DEST_FRONTEND)/etc/tabula-switchman.conf

	# frontend-scripts
	install -d $(DEST_FRONTEND)/etc/cron.d
	/usr/bin/django-admin create_package_files --settings=releaser.settings.direct --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_FRONTEND) --frontend-scripts
	/usr/bin/django-admin create_package_files --settings=releaser.settings.javadirect --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_FRONTEND) --frontend-scripts
	/usr/bin/django-admin create_package_files --settings=releaser.settings.directmod --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_FRONTEND) --frontend-scripts
	/usr/bin/django-admin create_package_files --settings=releaser.settings.geocontext --pythonpath=$(SVN_ROOT) --dest-dir=$(DEST_FRONTEND) --frontend-scripts

	# Табула
	mk-dpkg-monitor-files $(DEST_MAIN) yandex-du-releaser \
		-d email=direct-updates@yandex-team.ru \
		-d packages_prefixes=yandex-du-releaser \
		-d main_package=yandex-du-releaser \
		-d url='http://direct-dev.yandex-team.ru/versionica/update'
	# direct-utils
	mk-dpkg-monitor-files $(DEST_MAIN) yandex-du \
		-d email=direct-updates@yandex-team.ru \
		-d packages_prefixes=yandex-du- \
		-d packages_prefixes_ignore=yandex-du-releaser \
		-d url='http://direct-dev.yandex-team.ru/versionica/update' \
		-d title='direct-utils'
	# вообще все
	mk-dpkg-monitor-files $(DEST_MAIN) all \
		-d email=direct-updates@yandex-team.ru \
		-d packages_prefixes_ignore=yandex-du- \
		-d url='http://direct-dev.yandex-team.ru/versionica/update' \
		-d title='except Tabula'

	#install -T $(SVN_ROOT)/mk-packages-dir $(DEST_MAIN)/usr/local/bin/mk-packages-dir
	dh_perl
	#dh_makeshlibs
	dh_installdeb
	#dh_shlibdeps
	dh_gencontrol -- -v`cat debian/version`
	dh_md5sums
	dh_builddeb

.PHONY: build clean install
