#!/usr/bin/make -f

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

%:
	dh $@ --parallel --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 --fail-missing \
            -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

.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)" ;\
            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

PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?([\d.]+)};')
DTYPE = +dfsg
.PHONY: get-orig-source
get-orig-source: $(info I: $(PKG)_$(VER)) $(PKG)_$(VER)$(DTYPE).orig-wordpress.tar.xz $(PKG)_$(VER)$(DTYPE).orig-l10n.tar.xz
	## get main archive:
	[ -s "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" ] \
        || uscan --noconf --verbose --rename --repack --compression xz --destdir=$(CURDIR) \
            --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)

$(PKG)_$(VER)$(DTYPE).orig-l10n.tar.xz:
	$(if $(wildcard civicrm),$(error "civicrm" exists -- already extracted? Aborting..))
	$(if $(wildcard l10n),$(error "l10n" exists -- already extracted? Aborting..))
	## get translations and country data:
	[ -s "$(PKG)_$(VER)$(DTYPE).orig-l10n.tar.gz" ] \
        || wget --tries=3 --timeout=40 --read-timeout=40 --continue -O $(PKG)_$(VER)$(DTYPE).orig-l10n.tar.gz \
            http://downloads.sourceforge.net/project/civicrm/civicrm-stable/$(VER)/civicrm-$(VER)-l10n.tar.gz \
        || wget --tries=3 --timeout=40 --read-timeout=40 --continue -O $(PKG)_$(VER)$(DTYPE).orig-l10n.tar.gz \
            https://download.civicrm.org/civicrm-$(VER)-l10n.tar.gz?src=donate
	## repack translations:
	tar -xf $(PKG)_$(VER)$(DTYPE).orig-l10n.tar.gz
	mv -v civicrm l10n
	find -L "l10n" -xdev -type f -print | LC_ALL=C sort \
        | XZ_OPT="-6v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig-l10n.tar.xz" -T- --owner=root --group=root --mode=a+rX \
        && $(RM) -r "l10n"

$(PKG)_$(VER)$(DTYPE).orig-wordpress.tar.xz:
	$(if $(wildcard civicrm),$(error "civicrm" exists -- already extracted? Aborting..))
	$(if $(wildcard wordpress),$(error "wordpress" exists -- already extracted? Aborting..))
	## get wordpress archive:
	[ -s "$(PKG)_$(VER).orig-wordpress.zip" ] \
	|| wget --tries=3 --timeout=40 --read-timeout=40 -O $(CURDIR)/$(PKG)_$(VER).orig-wordpress.zip \
            http://downloads.sourceforge.net/project/civicrm/civicrm-stable/$(VER)/civicrm-$(VER)-wordpress.zip \
	|| wget --tries=3 --timeout=40 --read-timeout=40 -O $(CURDIR)/$(PKG)_$(VER).orig-wordpress.zip \
            https://download.civicrm.org/civicrm-$(VER)-wordpress.zip?src=donate
	unzip $(CURDIR)/$(PKG)_$(VER).orig-wordpress.zip
	mv -v civicrm/civicrm/civicrm-version.php civicrm/
	$(RM) -r civicrm/civicrm/*
	mv -v civicrm/civicrm-version.php civicrm/civicrm/
	mv -v civicrm wordpress
	@echo "# Packing..."
	find -L "wordpress" -xdev -type f -print | LC_ALL=C sort \
        | XZ_OPT="-6v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig-wordpress.tar.xz" -T- --owner=root --group=root --mode=a+rX \
        && $(RM) -r "wordpress"
