#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

REVISION := $(shell head -1 debian/changelog | sed 's/.*(//;s/).*//;s/.*-//')

%:
	dh $@


DEB_ROOT=debian/phpmyadmin
DEB_DIR=$(DEB_ROOT)/usr/share/phpmyadmin

# debian/tmp is the directory dh_install searches; dh_prep empties it at the
# start of the binary target, so the files have to be generated from
# override_dh_auto_install, which runs after dh_prep and before dh_install
BUILD_TMP=$(CURDIR)/debian/tmp

# keep in sync with debian/tests/phpunit
SKIP_32_BIT = $(shell php -n -r "echo (PHP_INT_SIZE === 4 || php_uname('m') === 's390x') ? '--exclude-group 32bit-incompatible' : '';")

override_dh_auto_test:
	# keep in sync with debian/tests/phpunit
	LC_ALL=C.UTF-8 phpunit --do-not-record-test-run-history --fail-on-empty-test-suite --no-coverage --exclude-group selenium --exclude-group network --exclude-group git-revision --exclude-group ext-xdebug --exclude-group with-trigger-error --exclude-group extension-iconv $(SKIP_32_BIT) --bootstrap debian/phpunit-bootstrap.php

# the bundles rebuilt below come from debian/missing-sources, which must match
# the versions package.json declares
check_missing_sources_versions:
	php $(CURDIR)/debian/check-missing-sources-versions.php

override_dh_auto_build: check_missing_sources_versions
	# generate the autoload files here, not in dh_auto_install: dh_auto_test runs
	# before that and needs them. They also have to be at the source root, because
	# libraries/constants.php resolves AUTOLOAD_FILE as ROOT_PATH.'/autoload.php'
	# and test/bootstrap-dist.php requires it directly. They are untracked and
	# removed by debian/clean. keep in sync with debian/tests/phpunit
	# the basedir must be the source root, otherwise phpab writes classmap paths
	# relative to the output file, which breaks once installed
	phpab --template debian/autoload.php.tpl \
		--basedir . \
		--output autoload.php \
		libraries/classes
	phpab --template debian/autoload-test.php.tpl \
		--output test/autoload.php \
		test

override_dh_auto_install:
	# babel.config.json is a root config, so run from the source root
	babeljs-7 js/src -d $(BUILD_TMP)/js/dist
	# re-build the jquery-migrate.min.js file
	minify -o $(BUILD_TMP)/js/vendor/jquery/jquery-migrate.min.js debian/missing-sources/jquery/jquery-migrate.js
	# build OpenLayers
	OL_BUILD_OUT=$(BUILD_TMP)/js/vendor/openlayers webpack --config $(CURDIR)/debian/webpack.ol.config.js
	# build phpMyAdmin user documentation
	make -C doc/ html BUILDDIR=$(BUILD_TMP)/doc
	# sphinx keeps its intermediate pickles next to the html, do not ship them
	rm -rf $(BUILD_TMP)/doc/doctrees
	test -s $(BUILD_TMP)/js/dist/functions.js
	test -s $(BUILD_TMP)/js/vendor/openlayers/OpenLayers.js
	test -s $(BUILD_TMP)/js/vendor/jquery/jquery-migrate.min.js
	test -s $(BUILD_TMP)/doc/html/index.html

override_dh_install:
	dh_install -XLICENSE

override_dh_sphinxdoc:
	dh_sphinxdoc
	rm -rf $(DEB_DIR)/js/vendor/codemirror
	rm -rf $(DEB_DIR)/themes/original/jquery/images
	rm -rf $(DEB_DIR)/themes/pmahomme/jquery/images
	rm -rf $(DEB_DIR)/themes/metro/jquery/images
	rm -rf $(DEB_DIR)/themes/bootstrap/jquery/images
	# set Debian marker as version suffix
	# Check for the variable we need to change
	grep -P "'versionSuffix' => '(.*)',$$" $(DEB_DIR)/libraries/vendor_config.php
	# Change it
	sed -i -E "s@'versionSuffix' => '(.*)',@'versionSuffix' => '\1deb$(REVISION)',@" $(DEB_DIR)/libraries/vendor_config.php
	# Check it is still there
	grep -P "'versionSuffix' => '(.*)',$$" $(DEB_DIR)/libraries/vendor_config.php
	# Check the contents of versionSuffix, it must include the debian version
	grep -q -P "'versionSuffix' => '(.*)deb$(subst +,\\+,$(REVISION))'," $(DEB_DIR)/libraries/vendor_config.php
	# Lint the file
	php -l $(DEB_DIR)/libraries/vendor_config.php

	# Generic themes
	# node-bootstrap Breaks/Replaces libjs-bootstrap4, which installs Bootstrap 4
	# to the same path, so refuse to compile anything but Bootstrap 5 sources
	test -f /usr/share/sass/bootstrap/_maps.scss || { echo "Bootstrap 5 Sass sources from node-bootstrap are missing (is libjs-bootstrap4 installed instead?)"; exit 1; }
	sassc --load-path /usr/share/sass --sourcemap --style compressed $(DEB_DIR)/themes/original/scss/theme.scss $(DEB_DIR)/themes/original/css/theme.css
	sassc --load-path /usr/share/sass --sourcemap --style compressed $(DEB_DIR)/themes/pmahomme/scss/theme.scss $(DEB_DIR)/themes/pmahomme/css/theme.css
	sassc --load-path /usr/share/sass --sourcemap --style compressed $(DEB_DIR)/themes/bootstrap/scss/theme.scss $(DEB_DIR)/themes/bootstrap/css/theme.css
	# Metro
	sassc --load-path /usr/share/sass --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/blueeyes-theme.scss $(DEB_DIR)/themes/metro/css/blueeyes-theme.css
	sassc --load-path /usr/share/sass --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/mono-theme.scss $(DEB_DIR)/themes/metro/css/mono-theme.css
	sassc --load-path /usr/share/sass --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/redmond-theme.scss $(DEB_DIR)/themes/metro/css/redmond-theme.css
	sassc --load-path /usr/share/sass --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/teal-theme.scss $(DEB_DIR)/themes/metro/css/teal-theme.css
	sassc --load-path /usr/share/sass --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/theme.scss $(DEB_DIR)/themes/metro/css/theme.css

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog upstream
	dh_installchangelogs

override_dh_installdocs:
	dh_installdocs -XChangeLog
