#!/usr/bin/make -f

export BROWSERSLIST_IGNORE_OLD_DATA=1

%:
	dh ${@}

execute_after_dh_auto_clean:
	rm -rf dist js/dist

override_dh_auto_build:
	mkdir -p dist/css dist/js dist/tmp

	sassc --sourcemap=auto scss/bootstrap.scss dist/tmp/bootstrap.css
	sassc --sourcemap=auto scss/bootstrap-grid.scss dist/tmp/bootstrap-grid.css
	sassc --sourcemap=auto scss/bootstrap-reboot.scss dist/tmp/bootstrap-reboot.css

	postcss --config build/postcss.config.js --replace 'dist/tmp/*.css' '!dist/tmp/*.min.css'

	cp -v dist/tmp/*.css dist/css/
	cp -v dist/tmp/*.css.map dist/css/

	sassc --sourcemap=auto --style compressed dist/tmp/bootstrap.css dist/css/bootstrap.min.css
	sassc --sourcemap=auto --style compressed dist/tmp/bootstrap-grid.css dist/css/bootstrap-grid.min.css
	sassc --sourcemap=auto --style compressed dist/tmp/bootstrap-reboot.css dist/css/bootstrap-reboot.min.css

	rm -rf dist/tmp

	NODE_PATH=node_modules node build/build-plugins.mjs
	NODE_PATH=node_modules rollup --sourcemap --environment BUNDLE:true --config build/rollup.config.mjs
	NODE_PATH=node_modules rollup --sourcemap --environment BUNDLE:false --config build/rollup.config.mjs

	cd dist/js && terser --compress --mangle --comments "/^!" --source-map content=bootstrap.bundle.js.map,includeSources,url=bootstrap.bundle.min.js.map -o bootstrap.bundle.min.js bootstrap.bundle.js
	cd dist/js && terser --compress --mangle --comments "/^!" --source-map content=bootstrap.js.map,includeSources,url=bootstrap.min.js.map -o bootstrap.min.js bootstrap.js

override_dh_auto_install:
	# disabled

override_dh_auto_test:
	# disabled

execute_before_dh_link:
	# javascript-common
	cd debian/libjs-bootstrap5 && for FILE in $$(find usr/share/bootstrap-html -type f); \
	do \
		mkdir -p $$(dirname $$(echo $${FILE} | sed -e 's|usr/share/bootstrap-html|usr/share/javascript/bootstrap5|')); \
		ln -s /$${FILE} $$(echo $${FILE} | sed -e 's|usr/share/bootstrap-html|usr/share/javascript/bootstrap5|'); \
	done

	# nodejs
	cd debian/libjs-bootstrap5 && for FILE in $$(find usr/share/bootstrap-html -type f); \
	do \
		mkdir -p ../node-bootstrap/$$(dirname $$(echo $${FILE} | sed -e 's|usr/share/bootstrap-html|usr/share/nodejs/bootstrap|')); \
		ln -s /$${FILE} ../node-bootstrap/$$(echo $${FILE} | sed -e 's|usr/share/bootstrap-html|usr/share/nodejs/bootstrap|'); \
	done

	ln -s ../../sass/bootstrap debian/node-bootstrap/usr/share/nodejs/bootstrap/scss

execute_after_dh_install:
	sed -i -e 's|dist\/||g' debian/node-bootstrap/usr/share/nodejs/bootstrap/package.json

override_dh_installdocs:
	dh_installdocs --exclude license.md
