#!/usr/bin/make -f

PKD      = $(word 1,$(abspath $(dir $(MAKEFILE_LIST))))
PKG      = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
UVER     = $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\-\d+.*)};')
DTYPE    = +reloaded2
VER     ?= $(subst $(DTYPE),,$(UVER))
PLUGINS  = $(shell ls debian/watch.* | cut -d "." -f2 | sed -e 's/gosa-//')

NULL     =

%:
	dh $@

override_dh_compress:
	dh_compress
	gzip -d debian/gosa/usr/share/doc/gosa/gosa.conf.gz

override_dh_install:

	# Run dh_install first...
	dh_install

	# silence several lintian warnings (package-contains-empty-directory).
	touch debian/gosa/usr/share/gosa/html/plugins/references/.placeholder
	test -f debian/gosa/usr/share/gosa/locale/core/en/LC_MESSAGES/messages.po || \
	    touch debian/gosa/usr/share/gosa/locale/core/en/LC_MESSAGES/.placeholder
	find debian/gosa-plugin-*/usr/share/gosa/locale/plugins/*/locale/*/LC_MESSAGES/ -type d | while read dir; do \
	    test -f $$dir/messages.po || touch $$dir/.placeholder; \
	done

	# don't use embedded fonts, use font package files from Debian
	mkdir -p debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationSerif-Italic.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationSerif-Regular.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationSans-Italic.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationMono-Italic.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationSerif-BoldItalic.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationMono-Bold.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationSans-BoldItalic.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationMono-BoldItalic.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts
	ln -sf /usr/share/fonts/truetype/liberation/LiberationSerif-Bold.ttf debian/gosa/usr/share/gosa/html/themes/default/fonts

	# symlink to Debian's libjs-scriptaculous package
	mkdir -p debian/gosa/usr/share/gosa/html/include
	ln -sf /usr/share/javascript/scriptaculous/effects.js debian/gosa/usr/share/gosa/html/include/
	ln -sf /usr/share/javascript/scriptaculous/controls.js debian/gosa/usr/share/gosa/html/include/
	ln -sf /usr/share/javascript/scriptaculous/dragdrop.js debian/gosa/usr/share/gosa/html/include/
	ln -sf /usr/share/javascript/scriptaculous/unittest.js debian/gosa/usr/share/gosa/html/include/
	ln -sf /usr/share/javascript/scriptaculous/slider.js debian/gosa/usr/share/gosa/html/include/
	ln -sf /usr/share/javascript/scriptaculous/scriptaculous.js debian/gosa/usr/share/gosa/html/include/
	ln -sf /usr/share/javascript/scriptaculous/sound.js debian/gosa/usr/share/gosa/html/include/
	ln -sf /usr/share/javascript/scriptaculous/builder.js debian/gosa/usr/share/gosa/html/include/
	ln -sf /usr/share/javascript/scriptaculous/prototype.js debian/gosa/usr/share/gosa/html/include/

	# symlink the smarty-gettext plugin
	ln -sf /usr/share/php/smarty3/plugins/block.t.php debian/gosa/usr/share/gosa/include/smartyAddons/

override_dh_installchangelogs:
	dh_installchangelogs -k gosa-core/Changelog

get-orig-source: $(patsubst %,$(PKG)-%_$(VER)$(DTYPE).orig.tar.gz,$(PLUGINS))
	cd $(PKD) && XZ_OPT=-6v tar -caf "../$(PKG)_$(VER)$(DTYPE).orig.tar.gz" --owner=root --group=root --mode=a+rX README.multi-orig-tarball-package
	cd $(PKD)/.. && mv *.tar.gz ..
	@echo "# Repacking gosa-core..."
	cd $(PKD)/.. && rm -rf gosa-core-$(VER) && tar xzf ../$(PKG)_$(VER)$(DTYPE).orig-gosa-core.tar.gz
	cd $(PKD)/../gosa-core-$(VER) && \
	    rm -rfv debian/ \
	    rm -rfv redhat/ \
	    rm -rfv include/smarty \
	    rm -fv  include/smartyAddons/block.t.php \
	    rm -fv  html/include/prototype.js \
	    rm -fv  html/include/scriptaculous.js \
	    rm -fv  html/include/slider.js \
	    rm -fv  html/include/sound.js \
	    rm -fv  html/include/unittest.js \
	    rm -fv  html/themes/default/fonts/*.ttf \
	    rm -fv  include/pChart/*.ttf \
	    $(NULL)
	rm ../$(PKG)_$(VER)$(DTYPE).orig-gosa-core.tar.gz
	cd $(PKD)/../ && tar czf ../$(PKG)_$(VER)$(DTYPE).orig-gosa-core.tar.gz gosa-core-$(VER) && rm -rf gosa-core-$(VER)


$(PKG)-%_$(VER)$(DTYPE).orig.tar.gz:
	@echo "# Downloading..."
	if [ ! -f $(PKD)/../../$(PKG)_$(VER)$(DTYPE).orig-$(shell echo $* | sed -e 's/plugin-//').tar.gz ]; then \
	    cd $(PKD)/.. && uscan --noconf --download --force-download --package $(PKG)-$* --watchfile debian/watch.gosa-$* --upstream-version $(UVER) --download-version $(VER) --rename --destdir=../; \
	fi
	@echo "# Renaming..."
	if [ -f $(PKD)/../../$(PKG)-$*_$(VER).orig.tar.gz ]; then \
	    cd $(PKD)/.. && mv -v ../$(PKG)-$*_$(VER).orig.tar.gz ../$(PKG)_$(VER)$(DTYPE).orig-$(shell echo $* | sed -e 's/plugin-//').tar.gz; \
	fi
