#!/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_DIR=debian/phpmyadmin/usr/share/phpmyadmin

# keep in sync with debian/tests/phpunit
# Use \PHPUnit\Runner\Version::majorVersionNumber() >= 11 when PHPUnit is updated to a newer version
SEPARATOR = $(shell php -n -r "require_once '/usr/share/php/PHPUnit/Autoload.php';echo ((int) explode('.', \PHPUnit\Runner\Version::series())[0]) >= 11 ? ' --exclude-group ' : ',';")

SKIP_32_BIT = $(shell php -n -r "echo (PHP_INT_SIZE === 4 || php_uname('m') === 's390x') ? '$(SEPARATOR)32bit-incompatible' : '';")

override_dh_auto_test:
	# keep in sync with debian/tests/phpunit
	LC_ALL=C.UTF-8 phpunit --no-coverage --exclude-group selenium$(SEPARATOR)network$(SEPARATOR)git-revision$(SEPARATOR)ext-xdebug$(SEPARATOR)with-trigger-error$(SEPARATOR)extension-iconv$(SKIP_32_BIT) --bootstrap debian/phpunit-bootstrap.php

override_dh_auto_clean:
	dh_auto_clean --sourcedirectory=doc

override_dh_auto_build: $(SRC_FILES_TO_BUILD)
	# re-build the jquery-migrate.min.js file
	minify -o ./js/vendor/jquery/jquery-migrate.min.js debian/missing-sources/jquery/jquery-migrate.js
	# re-build the jquery.validate.min.js file
	minify -o ./js/vendor/jquery/jquery.validate.min.js debian/missing-sources/jquery/jquery.validate.js
	# build OpenLayers
	webpack --config ./js/config/ol/webpack.config.js
	# build phpMyAdmin user documentation
	make -C doc/ html
	# generate autoload-files. keep in sync with debian/tests/phpunit
	phpab --template debian/autoload.php.tpl \
		--output autoload.php \
		libraries/classes
	phpab --template debian/autoload-test.php.tpl \
		--output test/autoload.php \
		test

override_dh_install:
	dh_install -XLICENSE
	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
	sassc --load-path debian/missing-sources/ --sourcemap --style compressed $(DEB_DIR)/themes/original/scss/theme.scss $(DEB_DIR)/themes/original/css/theme.css
	sassc --load-path debian/missing-sources/ --sourcemap --style compressed $(DEB_DIR)/themes/pmahomme/scss/theme.scss $(DEB_DIR)/themes/pmahomme/css/theme.css
	sassc --load-path debian/missing-sources/ --sourcemap --style compressed $(DEB_DIR)/themes/bootstrap/scss/theme.scss $(DEB_DIR)/themes/bootstrap/css/theme.css
	# Metro
	sassc --load-path debian/missing-sources/ --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/blueeyes-theme.scss $(DEB_DIR)/themes/metro/css/blueeyes-theme.css
	sassc --load-path debian/missing-sources/ --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/mono-theme.scss $(DEB_DIR)/themes/metro/css/mono-theme.css
	sassc --load-path debian/missing-sources/ --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/redmond-theme.scss $(DEB_DIR)/themes/metro/css/redmond-theme.css
	sassc --load-path debian/missing-sources/ --sourcemap --style compressed $(DEB_DIR)/themes/metro/scss/teal-theme.scss $(DEB_DIR)/themes/metro/css/teal-theme.css
	sassc --load-path debian/missing-sources/ --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
