#!/usr/bin/make -f

%:
	dh $@ --with phpcomposer,php

override_dh_auto_clean-indep:
	rm -rf debian/packages_to_build vendor

override_dh_auto_build-indep:
	mkdir debian/packages_to_build
ifeq (,$(findstring nocheck, $(DEB_BUILD_OPTIONS)))
	# Check if licenses of all images are properly covered in d/copyright
	./debian/licensing/bin/check_image_licenses --check
else
	@echo "** image license check disabled"
endif
	# Walk through the parts of upstream's code that should be packaged into
	# separate Debian binary packages and write down a package-to-build info
	# file containing shell variables for each package.
	# Those files will be traverse later on in different targets of this
	# makefile (debian/rules). They solely exists to not repeat the extraction
	# of those variables in each of those target over and over again.
	# Then, build a class loader for the package, using the template in
	# debian/$deb_pkg_name.autoload.php.tpl if it exists (to load dependencies).
	set -e;\
	 for src_path in $$(find src/Symfony/ -mindepth 2 -maxdepth 2 -type d); do \
	  src_part=$$(basename $$(dirname $$src_path)); \
	  src_name=$$(basename $$src_path); \
	  deb_pkg_name=php-$$(cat $$src_path/composer.json | jq -r '.name | tostring' | sed -r 's|/|-|'); \
	  if [ $$(grep -c -E -e "^Package: $$deb_pkg_name\$$" debian/control) -ne 1 ]; then \
	   echo "W: No Debian package '$$deb_pkg_name' defined in debian/control," 1>&2; \
	   echo "   therefor not considering Symfony $$src_part '$$src_name'," 1>&2; \
	   continue; \
	  fi; \
	  echo "# This file contains some variables sourced" > debian/packages_to_build/$$deb_pkg_name; \
	  echo "# by various targets in debian/rules" >> debian/packages_to_build/$$deb_pkg_name; \
	  echo "deb_pkg_name='$$deb_pkg_name'" >> debian/packages_to_build/$$deb_pkg_name; \
	  echo "src_path='$$src_path'" >> debian/packages_to_build/$$deb_pkg_name; \
	  echo "src_part='$$src_part'" >> debian/packages_to_build/$$deb_pkg_name; \
	  echo "src_name='$$src_name'" >> debian/packages_to_build/$$deb_pkg_name; \
	  if [ -e debian/$$deb_pkg_name.autoload.php.tpl ]; then \
	   phpab --output $$src_path/autoload.php \
		--whitelist 'symfony\\*' \
		--template debian/$$deb_pkg_name.autoload.php.tpl \
		$$src_path; \
	  else \
	   phpab --output $$src_path/autoload.php \
		--whitelist 'symfony\\*' \
		--tolerant \
		--template debian/default.autoload.php.tpl \
		$$src_path; \
	  fi; \
	 done
	# The following two components declare a classmap in their composer.json,
	# pointing to Resources/stubs. The classname being outside 'symfony\\*', the
	# --whitelist trick cannot be used here.
	phpab	--output src/Symfony/Component/Intl/autoload.php \
		--template debian/php-symfony-intl.autoload.php.tpl \
		src/Symfony/Component/Intl
	phpab	--output src/Symfony/Component/HttpFoundation/autoload.php \
		--tolerant \
		--template debian/default.autoload.php.tpl \
		src/Symfony/Component/HttpFoundation

override_dh_auto_test-indep:
ifeq (,$(filter nocheck stage1, $(DEB_BUILD_OPTIONS) $(DEB_BUILD_PROFILES)))
	ln -s ./src/Symfony ./Symfony
	mkdir vendor
	ln -s ../debian/autoload.build.php ./vendor/autoload.php
	# Actual tests suite
	components=$$(find src/Symfony -mindepth 3 -type f -name phpunit.xml.dist -printf '%h\n') && \
	 echo "$$components" | parallel --gnu --keep-order '/bin/echo -e "\\nRunning {} tests"; phpunit --colors=always --exclude-group online,network,tty,benchmark,intl-data {} || (/bin/echo -e "\\e[41mKO\\e[0m {}" && $$(exit 1));';
else
	@echo "** tests disabled"
endif

override_dh_auto_install-indep:

override_dh_phpcomposer-indep:
	# In debian/packages_to_build/ a file containing shell variables exists
	# for each package that should be build. Source one file after another
	# to make the shell variables available and run dh_phpcomposer for each
	# package.
	set -e;\
	 for package_info_file in $$(find debian/packages_to_build/ -mindepth 1 -maxdepth 1 -type f); do \
	  . $$package_info_file; \
	  dh_phpcomposer --package=$$deb_pkg_name --sourcedir=$$src_path; \
	 done

override_dh_install-indep:
	# In debian/packages_to_build/ a file containing shell variables exists
	# for each package that should be build. Source one file after another
	# to make the shell variables available and run dh_install in order to
	# provide the PHP runtime code for each package.
	set -e; \
	 for package_info_file in $$(find debian/packages_to_build/ -mindepth 1 -maxdepth 1 -type f); do \
	  . $$package_info_file; \
	  dh_install \
	   -X.git \
	   -X.md \
	   -Xcomposer.json \
	   -XConsole/Resources \
	   -XDebug/Resources \
	   -XDoctrine/Test \
	   -XForm/Test \
	   -XFrameworkBundle/Test \
	   -XVarDumper/Test \
	   -XLICENSE \
	   -XResources/meta \
	   -Xphpunit.xml.dist \
	   -XResources/bin \
	   -XTests \
	   --package=$$deb_pkg_name \
	   $$src_path/* \
	   usr/share/php/Symfony/$$src_part/$$src_name/; \
	 done

override_dh_installdocs-indep:
	dh_installdocs
	# In debian/packages_to_build/ a file containing shell variables exists
	# for each package that should be build. Source one file after another
	# to make the shell variables available and run dh_installdocs in order
	# to provide some basic documentation for each package.
	set -e; \
	 for package_info_file in $$(find debian/packages_to_build/ -mindepth 1 -maxdepth 1 -type f); do \
	  . $$package_info_file; \
	  if [ $$(ls $$src_path/*.md | wc -l) -eq 0 ]; then \
	   continue; \
	  fi; \
	  dh_installdocs \
	   -XCHANGELOG.md \
	   --package=$$deb_pkg_name \
	   $$src_path/*.md; \
	  dh_installdocs \
	   --package=$$deb_pkg_name \
	   debian/README.Debian; \
	 done

override_dh_installchangelogs-arch:
	dh_installchangelogs src/Symfony/Component/Security/CHANGELOG.md

override_dh_installchangelogs-indep:
	for i in $$(ls CHANGELOG-* -r); do cat $$i >> CHANGELOG && echo '' >> CHANGELOG; done
	dh_installchangelogs
	# In debian/packages_to_build/ a file containing shell variables exists
	# for each package that should be build. Source one file after another
	# to make the shell variables available and run dh_installchangelogs in
	# order to provide upstream changelogs for each package.
	set -e; \
	 for package_info_file in $$(find debian/packages_to_build/ -mindepth 1 -maxdepth 1 -type f); do \
	  . $$package_info_file; \
	  if [ ! -f "$$src_path/CHANGELOG.md" ]; then \
	   continue; \
	  fi; \
	  dh_installchangelogs \
	   --package=$$deb_pkg_name \
	   $$src_path/CHANGELOG.md; \
	 done

get-orig-source:
	uscan --verbose --rename --force
