#!/usr/bin/make -f
# debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess (sample file)
# Copyright 2000 to 2002 by Roland Mas and Christian Bayle for the Sourceforge package
# Copyright 2002 to 2008 by Roland Mas and Christian Bayle for the Gforge package
# Copyright 2008 to 2009 by Roland Mas and Christian Bayle for the FusionForge package

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

PACKAGE=gforge
include /usr/share/dpatch/dpatch.make

CRONFILESLIST=$(patsubst cron.d/%,debian/$(PACKAGE)-%.cron.d,$(wildcard cron.d/[a-z]*))
DIRSFILESLIST=$(patsubst dirs/%,debian/$(PACKAGE)-%.dirs,$(wildcard dirs/[a-z]*))
INSTALLFILESLIST=$(patsubst install/%,debian/$(PACKAGE)-%.install,$(wildcard install/[a-z]*))

listecho: 
	@echo $(CRONFILESLIST)
	@echo $(DIRSFILESLIST)
	@echo $(INSTALLFILESLIST)

remove-binary-files:
	sh $(CURDIR)/deb-specific/manage-uufiles.sh clean

cronfiles: $(CRONFILESLIST)

dirsfiles: $(DIRSFILESLIST)

installfiles: $(INSTALLFILESLIST)

conffiles: cronfiles dirsfiles installfiles

debian/$(PACKAGE)-%.cron.d:
	(cat $(CURDIR)/cron.d/00phpcron ; sed -e 's/\$$FFUSER/gforge/g' $(CURDIR)/cron.d/$*) > $@

debian/$(PACKAGE)-%.dirs:
	@echo "Creating $@"
	cp $(CURDIR)/dirs/$* $@ 

debian/$(PACKAGE)-%.install:
	@echo "Creating $@"
	cp $(CURDIR)/install/$* $@ 

configure: configure-stamp debian/po/templates.pot
configure-stamp: remove-binary-files
	dh_testdir

	touch configure-stamp

debian/po/templates.pot: $(wildcard debian/*.templates) $(wildcard debian/*.templates.dsfh-in) $(wildcard debian/dsf-helper/*.templates)
	@debconf-updatepo

build: configure-stamp build-stamp
build-stamp: patch
	dh_testdir

	# Build man pages from Docbook sources
	/usr/bin/docbook-to-man debian/cvssh.sgml > cvssh.1
	/usr/bin/docbook-to-man debian/gforge-config.sgml > gforge-config.1

	# Uudecode binary files
	sh $(CURDIR)/deb-specific/manage-uufiles.sh decode

	# Build gettext *.mo files
	utils/manage-translations.sh build

	touch build-stamp

clean: remove-binary-files unpatch
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f $(CURDIR)/debian/*.cron.d
	rm -f $(CURDIR)/debian/*.dirs
	rm -f $(CURDIR)/debian/*.install
	rm -f cvssh.1 gforge-config.1

	rm -rf locales

	perl $(CURDIR)/deb-specific/dsf-helper.pl --clean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k

	# Generate conffiles *.cron.d *.dirs *.install
	$(CURDIR)/debian/rules conffiles

	dh_installdirs

	# Preprocess files with DSF-Helper
	perl $(CURDIR)/deb-specific/dsf-helper.pl

	# Install files
	#dh_install --fail-missing -X.svn -X.arch-ids -X.bzr -X.cvsignore --sourcedir=$(CURDIR)
	dh_install -X.svn -X.arch-ids -X.bzr -X.cvsignore --sourcedir=$(CURDIR)

	# Install few links
	ln -s database-pgsql.php $(CURDIR)/debian/$(PACKAGE)-common/usr/share/$(PACKAGE)/common/include/database.php
	ln -s /usr/share/mediawiki/skins/$(PACKAGE)/loader.php $(CURDIR)/debian/$(PACKAGE)-plugin-mediawiki/etc/mediawiki-extensions/extensions-available/GforgeAuth.php
	# Remove a few others
	rm $(CURDIR)/debian/$(PACKAGE)-web-apache2/usr/share/gforge/www/plugins/mediawiki

	# Fix permissions
	for i in $(addprefix $(CURDIR)/debian/,$(shell dh_listpackages)) ; do find $$i -type f -exec chmod 644 {} \;  ; done
	-chmod 755 $(CURDIR)/debian/*/*/*/*/bin/* $(CURDIR)/debian/*/*/*/*/cronjobs/* \
		$(CURDIR)/debian/*/bin/*  $(CURDIR)/debian/*/usr/share/*/plugins/*/bin/* \
		$(CURDIR)/debian/*/usr/share/*/plugins/*/cronjobs/*
	#
	chmod 755 $(CURDIR)/debian/$(PACKAGE)-db-postgresql/usr/share/*/db/*.php
	chmod 644 $(CURDIR)/debian/$(PACKAGE)-db-postgresql/usr/share/gforge/cronjobs/*.inc
	# viewvc rights
	find $(CURDIR)/debian/$(PACKAGE)-web-apache2/usr/share/*/www/scm/viewvc -type f -exec chmod 0644 {} \;
	grep -ril '^#!' $(CURDIR)/debian/$(PACKAGE)-web-apache2/usr/share/*/www/scm/viewvc | xargs chmod 0755
	#
	find $(CURDIR)/debian/$(PACKAGE)-common/usr/share/*/common/docman/engine/ -type f -name \*.php -not -name \*.inc.php -exec chmod 0755 {} \;
	find $(CURDIR)/debian/$(PACKAGE)-common/usr/share/*/common/docman/engine/ -type f -name \*.sh -exec chmod 0755 {} \;
	#
	rm -f $(CURDIR)/debian/$(PACKAGE)-plugin-scm*/usr/share/*/plugins/scm*/*/README
	chmod 755 $(CURDIR)/debian/$(PACKAGE)-plugin-scmcvs/usr/share/*/plugins/scmcvs/sbin/*
	# remove windows files
	rm -rf $(CURDIR)/debian/$(PACKAGE)-web-apache2/usr/share/*/www/scm/viewvc/windows
	#
	rm -f $(CURDIR)/debian/$(PACKAGE)-web-apache2/usr/share/*/www/themes/gforge/COPYING

	# Remove Snoopy class from binary packages (provided by libphp-snoopy)
	for i in $(addprefix $(CURDIR)/debian/,$(shell dh_listpackages)) ; do find $$i -name Snoopy.class.php | xargs rm -rf ; done

	# Remove nusoap class from binary packages (provided by libnusoap-php)
	for i in $(addprefix $(CURDIR)/debian/,$(shell dh_listpackages)) ; do find $$i -name nusoap.php | xargs rm -rf ; done

	# Remove potential SCM artefacts from binary packages
	for i in $(addprefix $(CURDIR)/debian/,$(shell dh_listpackages)) ; do find $$i -name .svn -or -name {arch} -or -name .arch-ids -or -name .bzr -or -name .cvsignore | xargs rm -rf ; done
	# Also remove uuencoded files
	for i in $(addprefix $(CURDIR)/debian/,$(shell dh_listpackages)) ; do find $$i -name \*.uu | xargs echo rm ; done


binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdebconf	
	dh_installdocs
	#dh_installexamples
	#dh_installmenu
	#dh_installemacsen
	#dh_installpam
	#dh_installinit
	dh_installcron
	dh_installman
	#dh_installinfo
	#dh_undocumented
	dh_installchangelogs ChangeLog
	#dh_link
	dh_strip
	dh_compress
	 dh_fixperms
	#dh_makeshlibs
	dh_installdeb
	#dh_perl
	#dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch: build install
	# (No architecture-dependent files for Gforge, doing nothing here)

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