#!/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

SRC_FILES = $(wildcard themes/original/scss/*.scss themes/pmahomme/scss/*.scss themes/metro/scss/*.scss themes/bootstrap/scss/*.scss)
INCLUDE_FILES=$(wildcard themes/original/scss/_*.scss themes/pmahomme/scss/_*.scss themes/metro/scss/_*.scss themes/bootstrap/scss/_*.scss)
SRC_FILES := $(filter-out $(INCLUDE_FILES), $(SRC_FILES))
SRC_FILES_TO_BUILD = $(patsubst %,$(DEB_DIR)/%,$(SRC_FILES))
SKIP_32_BIT = $(shell php -n -r "echo (PHP_INT_SIZE === 4 || php_uname('m') === 's390x') ? ',32bit-incompatible' : '';")

$(DEB_DIR)/%.scss: %.scss
    # generate each .scss file into a .css file in the css folder, example: theme.css
	sassc --sourcemap --style compressed $< $(subst scss,css,$<)

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

override_dh_auto_clean:
	dh_auto_clean --sourcedirectory=doc

override_dh_auto_build: $(SRC_FILES_TO_BUILD)
	# re-build the bootstrap.bundle.min.js file
	minify -o ./js/vendor/bootstrap/bootstrap.bundle.min.js debian/missing-sources/bootstrap/bootstrap.bundle.js
	# 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
	grep -q -F "'versionSuffix' => ''," $(DEB_DIR)/libraries/vendor_config.php
	sed -i "s@'versionSuffix' => '',@'versionSuffix' => 'deb$(REVISION)',@" $(DEB_DIR)/libraries/vendor_config.php
	grep -q -F "'versionSuffix' => 'deb$(REVISION)'," $(DEB_DIR)/libraries/vendor_config.php
	# lint the file
	php -l $(DEB_DIR)/libraries/vendor_config.php

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

override_dh_installdocs:
	dh_installdocs -XChangeLog
