#!/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 distribution (module and command line
#     compiler) installed under /usr/share/nodejs by the node-sass binary
#     package.
#
# The native build only exists where the Dart SDK is ported to (amd64, arm64,
# armhf, riscv64).  The generic command name /usr/bin/sass is therefore owned by
# the arch:all "sass" package, which runs the native compiler when it is
# installed and the JavaScript one otherwise.
#
# 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 output 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, and debian/sass_npm.dart stands in
# for the entry point cli_pkg generates to put the library and the command line
# compiler into one bundle.

# 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

# cli_pkg stamps this into the JavaScript build as well, so that `sass --version`
# can say which build it is; it is the SDK the sources were compiled with.
DART_SDK_VERSION := $(shell dart --version 2>&1 | sed -n 's/^Dart SDK version: \([^ ]*\).*/\1/p')

# dart-sass --embedded announces this version to the host on startup.  Upstream
# reads it from sass/language's spec/EMBEDDED_PROTOCOL_VERSION while building,
# which a Debian build cannot access, so it is pinned here; 1.107.1 and later
# report 3.4.0 (CHANGELOG.md: "Properly report the embedded protocol version as
# 3.4.0"), and the upstream release binary of this version answers 3.4.0.
EMBEDDED_PROTOCOL_VERSION := 3.4.0

# Both builds compile the Dart SDK's runtime and core libraries into their
# output, so Built-Using (Policy 7.8) names the SDK package the build used.
# debian/control refers to it as ${dart-sdk:Version}.
DART_SDK_DEB_VERSION := $(shell dpkg-query -W -f='$${Version}' dart-sdk)

%:
	dh $@

# Built-Using must name the SDK version that was actually compiled in, which is
# the installed package, not whatever the changelog says.
override_dh_gencontrol:
	dh_gencontrol -- -Vdart-sdk:Version=$(DART_SDK_DEB_VERSION)

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) \
	    -Dcompiler-version=$(DEB_VERSION_UPSTREAM) \
	    -Dprotocol-version=$(EMBEDDED_PROTOCOL_VERSION) \
	    bin/sass.dart -o build/dart-sass
	dart compile js -O4 --no-minify --no-source-maps --fast-startup \
	    -Dversion=$(DEB_VERSION_UPSTREAM) -Dnode=true \
	    -Ddart-version=$(DART_SDK_VERSION) \
	    debian/sass_npm.dart -o build/sass-raw.js
	python3 debian/make-node-module.py build/sass-raw.js build/npm

# pubspec.lock is not part of the upstream archive: the Dart tool writes it
# when it resolves packages.  It must not be left behind, because
# dpkg-source -b runs before the build and refuses to add a new upstream
# file to a 3.0 (quilt) source package.
override_dh_auto_clean:
	rm -rf build .dart_tool pubspec.lock
	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

# The command line launcher carries a shebang, so it has to be executable
# ("script-not-executable"); dh_fixperms takes the bit away from /usr/share.
# node-sass is arch: all, so -B builds do not create its tree: guard the chmod.
override_dh_fixperms:
	dh_fixperms
	if [ -e debian/node-sass/usr/share/nodejs/sass/sass.js ]; then \
	    chmod 755 debian/node-sass/usr/share/nodejs/sass/sass.js; \
	fi

# The manual pages carry @VERSION@ rather than a literal version, so that they
# follow the package instead of drifting (the page shipped with 1.108.1-2 was
# still stamped 1.105.0).  Only the pages of the packages being built exist,
# hence the guard.
override_dh_installman:
	dh_installman
	for f in debian/*/usr/share/man/man1/*.1; do \
	    if [ -e "$$f" ]; then sed -i 's/@VERSION@/$(DEB_VERSION)/g' "$$f"; fi; \
	done

# 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
	install -D -m 755 debian/sass-wrapper.sh debian/sass/usr/bin/sass
	for d in debian/dart-sass debian/node-sass; do \
	    if [ -d "$$d" ]; then \
	        find "$$d" -type d -exec touch -d "@$${SOURCE_DATE_EPOCH}" {} +; \
	    fi; \
	done
