#!/usr/bin/make -f
INST_BASE = $(CURDIR)/debian/collabtive
WEB_BASE = $(INST_BASE)/usr/share/collabtive/www
SMARTY_BASE = $(INST_BASE)/usr/share/php/smarty/libs/

%:
	dh $@

override_dh_install:
	dh_install
	# Prepare the symlinks to be used for our components
	ln -s /var/cache/collabtive/templates_c $(WEB_BASE)/templates_c
	ln -s /var/lib/collabtive/files $(WEB_BASE)/files
	ln -s /etc/collabtive/ $(WEB_BASE)/config
	ln -s /etc/collabtive/language $(WEB_BASE)/language
	# Upstream-provided templates should be provided immutable;
	# local admin can add other templates in /etc/collabtive/templates
	ln -s /etc/collabtive/templates $(WEB_BASE)/templates
	for TMPL in `ls templates`; do \
		ln -s /usr/share/collabtive/templates/$$TMPL \
			$(INST_BASE)/etc/collabtive/templates/$$TMPL; \
	done
	find $(INST_BASE) -name Thumbs.db -exec rm \{\} +
	find $(INST_BASE) -name WS_FTP.LOG -exec rm \{\} +
	# Convenience copy of several libraries are basically thrown
	# in together. Weed them out by finding identically named
	# files in all instances we are aware of them... And fix
	# afterwards whatever breaks the pattern :-P
	#
	# smarty
	SMARTYFILES=`mktemp`; dpkg -L smarty|grep '\.php'$$ > $$SMARTYFILES; \
	for ITEM in `find $(WEB_BASE)/include/ -type f`; do \
		BASE=`basename $$ITEM`; \
		if FOUND=`grep $$BASE $$SMARTYFILES`; then \
			rm $$ITEM; ln -s $$FOUND $$ITEM; \
		fi; done; rm $$SMARTYFILES
	rm $(WEB_BASE)/include/class.Smarty.php
	ln -s /usr/share/php/smarty/libs/Smarty.class.php $(WEB_BASE)/include/class.Smarty.php
	# Smarty paginate - Move and link
	if [ ! -d $(SMARTY_BASE)/plugins ]; then mkdir -p $(SMARTY_BASE)/plugins; fi
	for FUNC in first last middle next prev; do \
		mv $(WEB_BASE)/include/plugins/function.paginate_$$FUNC.php $(SMARTY_BASE)/plugins/; \
		ln -s /usr/share/php/smarty/libs/plugins/function.paginate_$$FUNC.php $(WEB_BASE)/include/plugins/; \
		done
	# libphp-phpmailer: The versions are quite different... But
	# the API seems to be stable. Fingers crossed.
	rm $(WEB_BASE)/include/class.smtp.php $(WEB_BASE)/include/class.PHPmailer.php
	ln -s /usr/share/php/libphp-phpmailer/class.smtp.php $(WEB_BASE)/include/class.smtp.php
	ln -s /usr/share/php/libphp-phpmailer/class.phpmailer.php $(WEB_BASE)/include/class.PHPmailer.php
	# libphp-pclzip - Newer version, API seems stable (some
	# functions added, only private functions' signatures changed)
	rm $(WEB_BASE)/include/class.PclZip.php
	ln -s /usr/share/php/libphp-pclzip/pclzip.lib.php $(WEB_BASE)/include/class.PclZip.php
	# Javascript files: libjs-prototype, tinymce
	rm -fr $(WEB_BASE)/include/js/tiny_mce $(WEB_BASE)/include/js/prototype.js
	ln -s /usr/share/tinymce/www/ $(WEB_BASE)/include/js/tiny_mce
	ln -s /usr/share/javascript/prototype/prototype.js $(WEB_BASE)/include/js/prototype.js
