#!/usr/bin/make -f

SHELL = /bin/sh -e

include /usr/share/dpatch/dpatch.make


PACKAGES := $(shell dh_listpackages)
MODULES = $(filter-out core ldap,$(patsubst egroupware-%,%,$(filter egroupware-%,$(PACKAGES))))
COREDIR = debian/egroupware-core/usr/share/egroupware
COREDOC = debian/egroupware-core/usr/share/doc/egroupware-core
PACKAGEDIR = debian/egroupware-$${APP}/usr/share/egroupware/$${APPDIR}
PACKAGEDOC = debian/egroupware-$${APP}/usr/share/doc/egroupware-$${APPDIR}

COPYRIGHT = -iname 'copyright*' -or -iname 'copying*' -or -iname '*license*'
CHANGELOG = -iname 'changelog*'
EXTRADOC = -iname readme -or -iname todo

FINDOPT = -type f
APPFILES = -not \( -path '*doc*' -or -path '.' -or -name '*.pl' -or $(COPYRIGHT) -or $(CHANGELOG) -or -iname install -or $(EXTRADOC) \)
SPECIALDOC = -size 0 -or -iname install -or $(CHANGELOG) -or $(COPYRIGHT) -or \
	-name '*.sgml' -or -name '*.lyx' -or -name '*.ps' -or -name '*.dvi' -or -name '*.rtf'
DOCFILES = -not \( $(SPECIALDOC) \)

INSTALL = install -D -m 644


build: patch


clean: clean-patched unpatch

clean-patched:
	dh_testdir
	dh_testroot
	debconf-updatepo
	dh_clean


install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# install core modules

	for DIR in admin home phpgwapi preferences setup syncml ; do \
	  cd $(CURDIR)/$${DIR}; \
	  find $(FINDOPT) $(APPFILES) \
	    -exec $(INSTALL) {} $(CURDIR)/$(COREDIR)/$${DIR}/{} \; ; \
	  if [ -d $(CURDIR)/$${DIR}/doc ] ; then \
	    cd $(CURDIR)/$${DIR}/doc ; \
	    find $(FINDOPT) $(DOCFILES) \
	      -exec $(INSTALL) {} $(CURDIR)/$(COREDOC)/$${DIR}/{} \; ;\
	  fi ; \
	done

	# install applications

	for APP in $(MODULES) ; do \
	  APPDIR=$$(echo $${APP} | sed 's/-/_/g') ; \
	  if [ -d $(CURDIR)/$${APPDIR} ] ; then \
	    cd $(CURDIR)/$${APPDIR}; \
	    find $(FINDOPT) $(APPFILES) \
	      -exec $(INSTALL) {} $(CURDIR)/$(PACKAGEDIR)/{} \; ; \
	    if [ -d $(CURDIR)/$${APPDIR}/doc ] ; then \
	      cd $(CURDIR)/$${APPDIR}/doc; \
	      find $(FINDOPT) $(DOCFILES) \
	        -exec $(INSTALL) {} $(CURDIR)/$(PACKAGEDOC)/{} \; ;\
	    fi ; \
	  else \
	    echo "PACKAGING ERROR: directory $${APP} not found" 1>&2; exit 1 ; \
	  fi ; \
	done

	dh_install
	$(INSTALL) debian/egroupware-ldap.README.Debian $(CURDIR)/$(COREDIR)/phpgwapi/doc/ldap/README

	# delete some stuff
	for APKG in $(PACKAGES) ; do \
	  if [ -f debian/$${APKG}.remove ] ; then \
	    gawk '/^[^#].*$$/ { ORS="\0";  print $$0 }' < debian/$${APKG}.remove | xargs -0ri /bin/sh -c "rm -rf debian/$${APKG}/{}" ; \
	  fi ; \
	done

	# remove executable bit from php files
	find debian/egroupware*/ -type f -and -perm +111 -and -name '*.php' -print0 | xargs -0ri chmod a-x '{}'
	# make files starting with #! executable
	find debian/egroupware*/ -type f -exec grep -q '^#!' '{}' \; -print0 | xargs -0ri chmod a+x '{}'


binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installdebconf -i
	dh_installdocs -i
	dh_installexamples -i
	dh_installchangelogs  -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	gawk '/^[^#].*$$/ { ORS="\0" ;  print "-V" $$0 }' < debian/substvars | xargs -0 dh_gencontrol -i -- -Vegw:Modules=`for x in $(MODULES); do echo -n "egroupware-$$x(=\\$${Source-Version})," ; done`
	dh_md5sums -i
	dh_builddeb -i


binary: binary-indep binary-arch


.PHONY: build binary binary-arch binary-indep clean install