#!/usr/bin/make -f

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

%:
	dh $@ --buildsystem=phppear --with phpcomposer

override_dh_clean:
	## Remove Files-Excluded (when built from checkout or non-DFSG tarball):
	$(RM) -rv `perl -0nE 'say $$1 if m{^Files\-Excluded\:\s*(.*?)(?:\n\n|Files:|Comment:)}sm;' debian/copyright`
	dh_clean
	## dh_linktree can't replace directory with a symlink (#783924)
	## so we have to drop it (and since dh_linktree/replace do not
	## create new directories (#783906) a new symlink is to be made
	## manually if we want this folder in destination):
	$(RM) -vr packages/IDS/vendors/htmlpurifier/HTMLPurifier/DefinitionCache/Serializer

override_dh_install:
	dh_install \
            -Xlicense.txt -XLICENSE.txt -XLICENSE -XLICENSE.md -XCOPYING -Xgpl.txt \
            -XLICENSE.GPL -XLICENSE.LGPL -XLICENSE.TXT \
            -X.gitignore -X.gitmodules -X.gitattributes -X.DS_Store -X.swf \
            -Xagpl-3.0.txt -Xagpl-3.0.exception.txt \
            -XCHANGELOG.TXT -Xchangelog.txt -X/CHANGELOG.md -X/changelog.md \
            -X/bower.json -X/Gruntfile.js -X/package.json -X/composer.json -X/Makefile \
            -X/backbone-forms/scripts/build -Xicons.psd
	## executable-not-elf-or-script
	chmod -R -c a-x+X debian/*/usr/share
	## customise config file name:
	perl -pi -E 's{civicrm\.settings\.php}{civicrm-drupal.settings.php}sg;' \
            debian/drupal7-mod-civicrm/usr/share/drupal7/modules/civicrm/civicrm.config.php
	perl -pi -E 's{civicrm\.settings\.php}{civicrm-wordpress.settings.php}sg;' \
            debian/wordpress-civicrm/usr/share/wordpress/wp-content/plugins/civicrm/civicrm/civicrm.config.php
	## defaults: disable update check:
	perl -pi -E "s{'CiviCRM Preferences','versionCheck','i:\K1}{0}sg;" \
            debian/civicrm-common/usr/share/civicrm/sql/civicrm_generated.mysql

override_dh_missing:
	dh_missing --fail-missing

.PHONY: debian/civicrm-common
debian/civicrm-common:
	## Assist dh_linktree to do the right "replace" job:
	##  a) remove symlinks from destination (#783917):
	##  b) create tree of source directories in destination (#783906):
	grep ^replace $@.linktrees | \
        while read M S D; do\
            echo "pre-linktree: $$S --> $@$$D" ;\
            [ -f "$$S" ] && continue ;\
            [ -d "$@$$D" ] || mkdir -v -p "$@$$D" ;\
            find "$@$$D" -type l -delete -printf 'removed symlink %p\n' ;\
            cp -r --no-clobber --preserve=all "$$S"/* "$@$$D"/ ;\
        done

override_dh_link: debian/civicrm-common
	## we need to call dh_linktree before dh_link, not after like it
	## happens by default in "--with linktree" mode.
	dh_linktree
	## link minified .CSS and .JS with to their uncompressed versions:
	find $(CURDIR)/debian/civicrm-common/usr -type f -name "*[-.]min.css" -or -name "*[-.]min.js" | \
        while read MF; do \
            UF="$$(echo $${MF} | perl -pE 's{[\.-]min\.(js|css)}{.$$1};')" ;\
            if [ -f "$${UF}" -o -h "$${UF}" ]; then \
                ln -svfr "$${UF}" "$${MF}" ;\
            else \
                UF="$$(echo $${UF} | perl -pE 's{/\K([^/]+)\Z}{../$$1};')" ;\
                if [ -f "$${UF}" -o -h "$${UF}" ]; then \
                    ln -svfr "$${UF}" "$${MF}" ;\
                    continue ;\
                else \
                    UF="$$(echo $${UF} | perl -pE 's{/\K([^/]+)\Z}{src/$$1};')" ;\
                    [ -f "$${UF}" -o -h "$${UF}" ] && ln -svfr "$${UF}" "$${MF}" ;\
                fi ;\
            fi ;\
        done
	## Reduce duplication in CRM/*:
	$(RM) -r $(CURDIR)/debian/civicrm-common/usr/share/civicrm/CRM/Core
	$(RM) -r $(CURDIR)/debian/civicrm-common/usr/share/civicrm/CRM/Utils
	find $(CURDIR)/debian/civicrm-common/usr/share/civicrm/CRM -maxdepth 1 -type d ! -name "CRM" | \
        while read L; do \
            BL="$$(basename $$L)" ;\
            true|| ln -sv "/usr/share/civicrm/CRM/$$BL" "$(CURDIR)/debian/drupal7-mod-civicrm/usr/share/drupal7/modules/civicrm/CRM/$$BL" ;\
            ln -sv "/usr/share/civicrm/CRM/$$BL" "$(CURDIR)/debian/wordpress-civicrm/usr/share/wordpress/wp-content/plugins/civicrm/civicrm/CRM/$$BL" ;\
        done
	dh_link
	## remove empty directories:
	find $(CURDIR)/debian/civicrm-common/usr -type d -empty -delete -printf 'removed %p\n'
	## remove the following file due to security risk pop-up:
	$(RM) -v $(CURDIR)/debian/civicrm-common/usr/share/civicrm/vendor/dompdf/dompdf/dompdf.php
