#!/usr/bin/make -f

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

# List all Files and directories here that should not be copied to
# /usr/share/horde3
COPY_EXCLUDE=^(\{arch\}|COPYING|README|po|scripts|config|debian|docs|build-stamp|configure-stamp|patch-stamp)$

srcpkg = $(shell dpkg-parsechangelog | sed -ne 's/Source: *//p')
srcver = $(shell dpkg-parsechangelog | sed -ne 's/Version: *\(.*:\)\?\(.*\)-.*$$/\2/p')

#{{{ generic rules

../$(srcpkg)_$(srcver).orig.tar.gz:
	@if git rev-parse --git-dir >/dev/null 2>&1; then                 \
            pristine-tar checkout $@;                                     \
        fi

check-tarball: ../$(srcpkg)_$(srcver).orig.tar.gz

.PHONY: check-tarball

#}}}
#{{{ maintainer stuff

refresh-patches: check-tarball
	@dh_testdir
	@echo 'refreshing debian/patches:'
	@rm -rf '$(CURDIR)'/debian/patches
	@mkdir -p '$(CURDIR)'/debian/patches
	@cd '$(CURDIR)'/debian/patches && git format-patch upstream..upstream+patches
	@echo .

#}}}

patch: patch-stamp check-tarball
patch-stamp:
	dh_testdir
	set -e; test -e patch-stamp || \
	    for i in `ls -1 debian/patches/*.patch || :`; do patch -p1 <$$i > /dev/null; done
	touch $@

unpatch:
	dh_testdir
	set -e; ! test -e patch-stamp || \
	    for i in `ls -1r debian/patches/*.patch || :`; do patch -p1 -R <$$i > /dev/null; done
	rm -f patch-stamp

configure: configure-stamp patch
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.

	touch configure-stamp

build-arch: build
build-indep: build

build: check-external-libs build-stamp

build-stamp: configure
	dh_testdir
	touch build-stamp

clean:  unpatch
	dh_testdir
	dh_testroot
	dh_clean build-stamp configure-stamp

install: build
	dh_testdir
	dh_testroot
	dh_clean -k 
	dh_installdirs

	# Add here commands to install the package into debian/horde3.
	#### copy stuff that belongs in /usr/share/horde3 ###
	for i in $$(ls -1 | egrep -v "$(COPY_EXCLUDE)"); do \
		echo install $$i ; \
		cp -a $$i debian/horde3/usr/share/horde3 ; \
	done

	#### copy configuration files ####
	for i in config/*.dist; do \
		NAME=etc/horde/horde3/$$(basename $$i ".dist");	\
		cp -a $$i debian/horde3/$$NAME; \
	done
	cp -r config/registry.d debian/horde3/etc/horde/horde3/

	# Use '/horde3' for default cookie path (See #391493)
	cat config/conf.xml | \
		sed -e "109,116s@/horde@/horde3@" > debian/horde3/etc/horde/horde3/conf.xml

	#### horde expects configuration in config subdir, an update-alternatives
	#### symlink is created in maintainer's script to comply with FHS

	#### copy scripts to run by cron ####
	mkdir -p debian/horde3/usr/share/horde3/scripts
	cp scripts/temp-cleanup.cron debian/horde3/usr/share/horde3/scripts
	chmod +x debian/horde3/usr/share/horde3/scripts/temp-cleanup.cron
	cp scripts/alarms.php debian/horde3/usr/share/horde3/scripts

	cp debian/horde3.default debian/horde3/etc/default/horde3

	#### create htaccess  to restrict acess by web ####
	echo "Deny from all" > debian/horde3/usr/share/horde3/scripts/.htaccess
	echo "Deny from all" > debian/horde3/etc/horde/horde3/.htaccess

	#### log file ####
	printf "\n\$$conf['log']['name'] = '/var/log/horde/horde3.log';" >> \
		debian/horde3/etc/horde/horde3/conf.php
	chown root:www-data debian/horde3/var/log/horde
	chmod 750 debian/horde3/var/log/horde

	#### add default configuration to avoid broken pages
	printf "\n\$$conf['datatree']['driver'] = 'null';" >> \
		debian/horde3/etc/horde/horde3/conf.php
	printf "\n\$$conf['share']['no_sharing'] = true;" >> \
		debian/horde3/etc/horde/horde3/conf.php
	printf "\n\$$conf['cache']['driver'] = 'none';" >> \
		debian/horde3/etc/horde/horde3/conf.php

	#### change made for program run with no problems ###
	cat config/registry.php.dist | \
		sed -e "s/'fileroot' *=> *dirname(__FILE__)/'fileroot' => '\/usr\/share\/horde3\/lib'/"| \
		sed -e "s/'templates' *=> *dirname(__FILE__)/'templates' => '\/usr\/share\/horde3\/lib'/"| \
		sed -e "s/webroot *= * '\/horde';/webroot = '\/horde3';/" > \
		debian/horde3/etc/horde/horde3/registry.php

	# change HORDE_BASE  in php files
	# change /usr/local/bin/php to /usr/bin/php
	for i in $$(find debian/horde3 -name "*.php"); do \
		cat $$i | \
		sed -e "s/define('HORDE_BASE'.*;/define\('HORDE_BASE', '\/usr\/share\/horde3\'\);/" |\
		sed -e "s/\/usr\/local\/bin/\/usr\/bin/" > $$i.rew; \
		mv $$i.rew $$i; \
	done

	#### change permissions of scripts  ####
	for i in $$(find debian/horde3/usr/share/horde3 -type f | \
		xargs grep '^#!\/' |cut -d: -f1); do \
		chmod u+x $$i; \
	done
 
	#### create link to access horde ####
	#dh_link usr/share/horde3 var/www/horde3
	#### end ####

	#### no exec right for .xml/png/gif/css/js/jpg/html/htm files (lintian warnings) ####
	find debian/horde3/usr/share/horde3 \( \
		    -name "*.xml" \
		 -o -name "*.png" \
		 -o -name "*.gif" \
		 -o -name "*.css" \
		 -o -name "*.js" \
		 -o -name "*.jpg" \
		 -o -name "*.html" \
		 -o -name "*.htm" \) \
		 -type f -exec chmod -x '{}' \;

	#### CREDITS file accessible to help viewer (DEPRECATED ??) ####
	#mkdir -m 755 debian/horde3/usr/share/horde3/docs
	#cp -a docs/CREDITS debian/horde3/usr/share/horde3/docs/
	
	#### Remove empty directories (lintian warnings) (DEPRECATED) ####
	#rmdir debian/horde3/usr/share/horde3/lib/Horde/tests
	#rmdir debian/horde3/usr/share/horde3/lib/Text/docs/examples
	#rmdir debian/horde3/usr/share/horde3/lib/Text/docs
	#rmdir debian/horde3/usr/share/horde3/lib/VFS/tests
	#rmdir debian/horde3/usr/share/horde3/lib/Horde/docs/examples
	#rmdir debian/horde3/usr/share/horde3/lib/Horde/docs
	#rmdir debian/horde3/usr/share/horde3/lib/File/tests
	#rmdir debian/horde3/usr/share/horde3/lib/XML/tests
	#rmdir debian/horde3/usr/share/horde3/lib/data
	#rmdir debian/horde3/usr/share/horde3/lib/Text/tests
	#rmdir debian/horde3/usr/share/horde3/lib/XML/docs/examples
	#rmdir debian/horde3/usr/share/horde3/lib/XML/docs
	#rmdir debian/horde3/usr/share/horde3/lib/Horde/scripts
	#rmdir debian/horde3/usr/share/horde3/lib/Horde/Text/tests
	#rmdir debian/horde3/usr/share/horde3/lib/docs

	#### Remove extra license file (lintian warnings) ####
	#### (This hack is temporary because waiting xinha package) ####
	rm debian/horde3/usr/share/horde3/services/editor/xinha/license.txt

	#### Remove javascript source files
	rm -rf  debian/horde3/usr/share/horde3/js/src/

	dh_install -i -ppear-horde-channel

# Build architecture-dependent files here.
binary-arch: build install
# We have nothing to do by default.

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs 
	dh_installdocs -XINSTALL
	mv debian/horde3/usr/share/doc/horde3/CHANGES \
		debian/horde3/usr/share/doc/horde3/changelog
	dh_installexamples
	-find debian/horde3 -depth -name ".arch-ids" -exec rm -Rf {} \;
#	dh_installmenu
#	dh_installdebconf	
	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip
	dh_compress
	dh_fixperms -Xdebian/horde3/var/log/horde
#	dh_perl
#	dh_python
#	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
check-external-libs:
	if [ -e services/editor/fckeditor ] || \
	   [ -e services/editor/tinymce ] || \
	   [ -e js/controls.js ] || [ -e js/src/controls.js ] || \
	   [ -e js/dragdrop.js ] || [ -e js/src/dragdrop.js ] || \
	   [ -e js/effects.js ] || [ -e js/src/effects.js ] || \
	   [ -e js/prototype.js ] || [ -e js/src/prototype.js ] || \
	   [ -e js/scriptaculous.js ] || [ -e js/src/scriptaculous.js ] || \
	   [ -e js/slider.js ] || [ -e js/src/slider.js ]; then \
               echo ""; \
	      echo " **                                  **"; \
	      echo " **  tar contains external files!    **"; \
	      echo " **                                  **"; \
	      echo ""; \
	      exit 1; \
        fi

binary: binary-indep binary-arch
.PHONY: patch unpatch build clean binary-indep binary-arch binary install configure
