#!/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:
	# bootstrap.css
	mkdir -p dist/css 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
	sassc --sourcemap=auto scss/bootstrap-utilities.scss dist/tmp/bootstrap-utilities.css

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

	for FILE in dist/tmp/*.css; \
	do \
		sed -i -e "s|\(sourceMappingURL\)=data.*|\1=$$(basename $${FILE}).map \*/|" $${FILE}; \
	done

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

	esbuild dist/css/bootstrap.css --sourcemap --outfile=dist/css/bootstrap.min.css --minify
	esbuild dist/css/bootstrap-grid.css --sourcemap --outfile=dist/css/bootstrap-grid.min.css --minify
	esbuild dist/css/bootstrap-reboot.css --sourcemap --outfile=dist/css/bootstrap-reboot.min.css --minify
	esbuild dist/css/bootstrap-utilities.css --sourcemap --outfile=dist/css/bootstrap-utilities.min.css --minify

	rm -rf dist/tmp

	# bootstrap plugins
	mkdir -p dist/js
	NODE_PATH=node_modules node build/build-plugins.mjs

	# bootstrap.js
	NODE_PATH=node_modules rollup --sourcemap --environment BUNDLE:false --config build/rollup.config.mjs
	esbuild dist/js/bootstrap.js --sourcemap --outfile=dist/js/bootstrap.min.js --minify

	# bootstrap.bundle.js
	NODE_PATH=node_modules rollup --sourcemap --environment BUNDLE:true --config build/rollup.config.mjs
	esbuild dist/js/bootstrap.bundle.js --sourcemap --outfile=dist/js/bootstrap.bundle.min.js --minify

	# bootstrap.esm.js
	esbuild js/index.esm.js --bundle --sourcemap --outfile=dist/js/bootstrap.esm.js
	esbuild dist/js/bootstrap.esm.js --sourcemap --outfile=dist/js/bootstrap.esm.min.js --minify

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
