#!/usr/bin/make -f
# vim: set noexpandtab shiftwidth=8: #

%:
	dh $@ --with apache2

override_dh_fixperms:
	dh_fixperms
	# Fix executable-not-elf-or-script lintian warnings
	find $(CURDIR)/debian/spotweb/usr/share/spotweb/templates/ -type f -exec chmod a-x '{}' \;
	find $(CURDIR)/debian/spotweb/usr/share/spotweb/lib/ -type f -exec chmod a-x '{}' \;
	find $(CURDIR)/debian/spotweb/usr/share/spotweb/www/ -type f -exec chmod a-x '{}' \;
	find $(CURDIR)/debian/spotweb/usr/share/spotweb/js/ -type f -exec chmod a-x '{}' \;
	find $(CURDIR)/debian/spotweb/usr/share/spotweb/Math/ -type f -exec chmod a-x '{}' \;
	find $(CURDIR)/debian/spotweb/usr/share/spotweb/Crypt/ -type f -exec chmod a-x '{}' \;
	chmod a-x $(CURDIR)/debian/spotweb/etc/spotweb/*
	chmod a-x $(CURDIR)/debian/spotweb/usr/share/spotweb/scripts/*.php

override_dh_link:
	dh_link
	# Use Debian-packaged jquery
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/jquery/jquery.min.js
	dh_link usr/share/javascript/jquery/jquery.min.js usr/share/spotweb/js/jquery/jquery.min.js        
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/jquery/jquery-ui.custom.min.js
	dh_link usr/share/javascript/jquery-ui/jquery-ui.min.js usr/share/spotweb/js/jquery/jquery-ui.custom.min.js
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/templates/we1rdo/css/jquery-ui-1.8.13.custom.css
	dh_link usr/share/javascript/jquery-ui/css/smoothness/jquery-ui.css usr/share/spotweb/templates/we1rdo/css/jquery-ui-1.8.13.custom.css
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/jquery/jquery.cookie.js
	dh_link usr/share/javascript/jquery-cookie/jquery.cookie.min.js usr/share/spotweb/js/jquery/jquery.cookie.js
	# Fancybox: use Debian version in /usr/share/javascript/jquery-fancybox
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/fancybox/jquery.easing-1.3.pack.js
	dh_link usr/share/javascript/jquery-easing/jquery.easing.min.js usr/share/spotweb/js/fancybox/jquery.easing-1.3.pack.js
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/jquery/jquery.form.js
	dh_link usr/share/javascript/jquery-form/jquery.form.min.js usr/share/spotweb/js/jquery/jquery.form.js
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/fancybox/jquery.mousewheel-3.0.4.pack.js
	dh_link usr/share/javascript/jquery-mousewheel/jquery.mousewheel.min.js usr/share/spotweb/js/fancybox/jquery.mousewheel-3.0.4.pack.js
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/fancybox/jquery.fancybox-1.3.4.pack.js
	dh_link usr/share/javascript/jquery-fancybox/jquery.fancybox.min.js usr/share/spotweb/js/fancybox/jquery.fancybox-1.3.4.pack.js
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/fancybox/jquery.fancybox-1.3.4.css
	dh_link usr/share/javascript/jquery-fancybox/jquery.fancybox.css usr/share/spotweb/js/fancybox/jquery.fancybox-1.3.4.css
	for f in $(CURDIR)/debian/spotweb/usr/share/spotweb/js/fancybox/*.gif \
	         $(CURDIR)/debian/spotweb/usr/share/spotweb/js/fancybox/*.png; do \
		rm "$$f"; \
		dh_link usr/share/javascript/jquery-fancybox/$$( basename "$$f" ) \
			usr/share/spotweb/js/fancybox/$$( basename "$$f" ); \
	done
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/www/images/ttf/Arialbd.TTF
	# Use debian-supplied alternative free font instead of font bundled with
	# upstream
	dh_link usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf usr/share/spotweb/www/images/ttf/Arialbd.TTF
	# Remove licenses recorded in debian/copyright
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/lib/gettext/LICENSE
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/dynatree/GPL-LICENSE.txt
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/js/dynatree/MIT-License.txt
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/lib/notifications/boxcar/LICENSE.txt
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/lib/notifications/nma/LICENSE.txt
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/lib/notifications/prowl/LICENSE.txt
	rm -f $(CURDIR)/debian/spotweb/usr/share/spotweb/lib/notifications/twitter/LICENSE.txt
	rm -f $(CURDIR)/debian/spotweb/usr/share/doc/spotweb/INSTALL

override_dh_install: debian/spotweb.conf
	# Create packed css and js files where spotweb expects them
	yui-compressor -o js/jquery.mobile-1.0a3/jquery.mobile-1.0a3.min.js js/jquery.mobile-1.0a3/jquery.mobile-1.0a3.js
	yui-compressor -o js/jquery.mobile-1.0a3/jquery.mobile-1.0a3.min.css js/jquery.mobile-1.0a3/jquery.mobile-1.0a3.css
	yui-compressor -o templates/we1rdo/js/jquery.address-1.4.min.js debian/extra_sources/jquery.address-1.4.js
	yui-compressor -o js/dynatree/jquery.dynatree.min.js debian/extra_sources/jquery.dynatree.js
	yui-compressor -o js/jquery/jquery.preload-min.js debian/extra_sources/jquery.preload.js
	# Install dbconfig-common database initialisation scripts
	install --directory $(CURDIR)/debian/spotweb/usr/share/dbconfig-common/scripts/spotweb/install
	install debian/upgrade-db.sh $(CURDIR)/debian/spotweb/usr/share/dbconfig-common/scripts/spotweb/install/mysql
	# Install dbconfig-common database upgrade scripts
	install --directory $(CURDIR)/debian/spotweb/usr/share/dbconfig-common/scripts/spotweb/upgrade/mysql
	install debian/db-update/v20111210.sh $(CURDIR)/debian/spotweb/usr/share/dbconfig-common/scripts/spotweb/upgrade/mysql/20111210
	install debian/upgrade-db.sh          $(CURDIR)/debian/spotweb/usr/share/dbconfig-common/scripts/spotweb/upgrade/mysql/20130826
	# Standard dh installation, but exclude font files already in Debian
	dh_install --exclude liberation-sans

override_dh_apache2:
	dh_apache2 --noscripts

debian/spotweb.conf: debian/apache.conf.in
	rm -f debian/spotweb.conf; \
	for template in templates/*; do \
	  for subdir in img js css smileys; do \
            if [ -d $$template/$$subdir ]; then \
              echo "Alias /spotweb/$$template/$$subdir \"/usr/share/spotweb/$$template/$$subdir\"" >> debian/spotweb.conf; \
            fi; \
          done; \
        done; \
	cat debian/apache.conf.in >> debian/spotweb.conf; 

override_dh_installchangelogs: debian/notifications.html
	dh_installchangelogs debian/notifications.html

debian/notifications.html: notifications.xml
	xsltproc debian/rss.xsl notifications.xml > debian/notifications.html

PACKAGE = spotweb
SRC_VERSION := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(\([0-9]\+\):\)\?\(.*\)-.*/\3/p')
TARBALL = $(CURDIR)/$(PACKAGE)_$(SRC_VERSION).orig.tar.gz
.PHONY: get-orig-source

get-orig-source:
	rm -f $(TARBALL)
	debian/get-orig-source.sh $(SRC_VERSION) $(TARBALL)
