#!/usr/bin/make -f

# Dart Sass is built twice:
#
#   * as a self-contained native executable, installed as /usr/bin/dart-sass
#     by the dart-sass binary package;
#   * with dart2js, as the JavaScript module for Node.js consumers, installed
#     as "sass" under /usr/share/nodejs by the node-sass binary package.
#
# Nothing is fetched from the network.  Debian ships the Dart packages that
# Dart Sass depends on as sources under /usr/share/dart/packages/; the Dart
# tools need a package_config.json to resolve "package:<name>/..." imports, so
# debian/gen-package-config.py builds one from those directories.
#
# The JavaScript module is not used as dart2js emits it: upstream wraps the
# output with a loader that inlines the node_preamble and injects the
# require()s the Dart code performs (that wrapper is normally written by
# cli_pkg, which cannot run in a Debian build because it drives npm).
# debian/make-node-module.py reproduces it.

# dpkg does not export the package version to debian/rules, so take it from the
# changelog the supported way (lintian: debian-rules-parses-dpkg-parsechangelog).
include /usr/share/dpkg/pkg-info.mk

%:
	dh $@

override_dh_auto_build:
	mkdir -p .dart_tool build
	python3 debian/gen-package-config.py > .dart_tool/package_config.json
	dart compile exe -Dversion=$(DEB_VERSION_UPSTREAM) bin/sass.dart -o build/dart-sass
	dart compile js -O4 --no-minify --no-source-maps --fast-startup \
	    -Dversion=$(DEB_VERSION_UPSTREAM) lib/src/js.dart -o build/sass-raw.js
	python3 debian/make-node-module.py build/sass-raw.js build/npm

override_dh_auto_clean:
	rm -rf build .dart_tool
	dh_auto_clean

# strip(1) removes the ELF section that carries the AOT snapshot of a
# `dart compile exe` executable: the result is 5 MB smaller and behaves like
# plain dartaotruntime instead of the compiled program (measured: 10687408 ->
# 5453960 bytes, and `dart-sass --version` then prints the Dart SDK version).
# The compiler therefore must not be stripped.
override_dh_strip:
	dh_strip --exclude=dart-sass

# Upstream's pub.dev archives record every directory entry with an epoch (0)
# timestamp, and dh_install preserves the mtime of the directories it copies,
# which lintian reports as package-contains-ancient-file.  Stamp installed
# directories with the source date instead (dh exports SOURCE_DATE_EPOCH from
# debian/changelog, so builds stay reproducible).  No file content is affected.
override_dh_install:
	dh_install
	find debian/dart-sass debian/node-sass -type d -exec touch -d "@$${SOURCE_DATE_EPOCH}" {} +
