#!/usr/bin/make -f
INST_BASE = $(CURDIR)/debian/collabtive
WEB_BASE = $(INST_BASE)/usr/share/collabtive/www
INCLUDES = $(WEB_BASE)/include
SMARTY_PLUGINS = $(INST_BASE)/usr/share/php/smarty3/plugins

%:
	dh $@ --with apache2

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/language $(WEB_BASE)/language
	# The config file itself is in a subdirectory of config - Link
	# the "standard" configuration to the top level
	ln -s /etc/collabtive/ $(WEB_BASE)/config
	ln -s ../config.php $(INST_BASE)/etc/collabtive/standard/config.php

	# 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 smarty3|grep '\.php'$$ > $$SMARTYFILES; \
	for ITEM in `find $(INCLUDES)/ -type f`; do \
		BASE=`basename $$ITEM`; \
		if FOUND=`grep $$BASE $$SMARTYFILES`; then \
			rm $$ITEM; ln -s $$FOUND $$ITEM; \
		fi; done; rm $$SMARTYFILES
	rm -f $(INCLUDES)/class.Smarty.php $(INCLUDES)/Smarty_Compiler.class.php $(INCLUDES)/internals/core.*php
	ln -s ../../../php/smarty3/Smarty.class.php $(INCLUDES)/class.Smarty.php
	# Smarty paginate - Move and link
	for FUNC in first last middle next prev; do \
		mv $(INCLUDES)/plugins/function.paginate_$$FUNC.php $(SMARTY_PLUGINS)/; \
		ln -s ../../../../php/smarty3/libs/plugins/function.paginate_$$FUNC.php $(INCLUDES)/plugins/; \
		done
	# libphp-phpmailer: The versions are quite different... But
	# the API seems to be stable. Fingers crossed.
	rm $(INCLUDES)/class.smtp.php $(INCLUDES)/class.PHPmailer.php
	ln -s ../../../php/libphp-phpmailer/class.smtp.php $(INCLUDES)/class.smtp.php
	ln -s ../../../php/libphp-phpmailer/class.phpmailer.php $(INCLUDES)/class.PHPmailer.php
	# libphp-pclzip - Newer version, API seems stable (some
	# functions added, only private functions' signatures changed)
	rm $(INCLUDES)/class.PclZip.php
	ln -s ../../../php/libphp-pclzip/pclzip.lib.php $(INCLUDES)/class.PclZip.php
	# Javascript files: libjs-prototype, tinymce
	rm -fr $(INCLUDES)/js/tiny_mce $(INCLUDES)/js/prototype.js
	ln -s ../../../../tinymce/www/ $(INCLUDES)/js/tiny_mce
	ln -s ../../../../javascript/prototype/prototype.js $(INCLUDES)/js/prototype.js
	# php-tcpdf
	rm $(INCLUDES)/include/tcpdf*.php $(INCLUDES)/include/barcodes/*
	for i in fonts filters font_data colors static images; do \
		ln -s ../../../../php/tcpdf/include/tcpdf_$$i.php $(INCLUDES)/include/; \
	done
	for i in qrcode.php datamatrix.php pdf417.php; do \
		ln -s ../../../../php/tcpdf/include/barcodes/$$i $(INCLUDES)/include/barcodes/$$i; \
	done
	ln -s ../../../../php/tcpdf/include/sRGB.icc $(INCLUDES)/include/sRGB.icc
