#!/usr/bin/make -f

include /usr/share/rustc/architecture.mk

RUFF_VER := $(shell sed -n 's/^version = "\(.*\)"/\1/p' pyproject.toml | head -n1)
PYDIR    := usr/lib/python3/dist-packages

# ty is versioned independently of ruff. Read the ty version from the patched
# crates/ty/Cargo.toml and give the ty binary package a version recording both
# upstreams: <ty>+ruff<ruff>+ds-<revision>.
TY_UPSTREAM    := $(shell sed -n 's/^version = "\(.*\)"/\1/p' crates/ty/Cargo.toml | head -n1)
DEB_REVISION   := $(shell dpkg-parsechangelog -SVersion | sed 's/.*-//')
TY_DEB_VERSION := $(TY_UPSTREAM)+ruff$(RUFF_VER)+ds-$(DEB_REVISION)

%:
	dh $@ --buildsystem cargo --with python3

override_dh_auto_build:
	# no-op: the shipped binaries are built in the -arch rule
override_dh_auto_test:
	# no-op: tests are not run at build time, avoid a double build

override_dh_auto_configure-indep:
	# no-op: python3-ruff needs no Cargo configuration

override_dh_auto_install-arch:
	CARGO_HOME=$(CURDIR)/debian/cargo_home CARGO_TARGET_DIR=$(CURDIR)/target DESTDIR=debian/tmp cargo -Zavoid-dev-deps install --verbose --target $(DEB_HOST_RUST_TYPE) --path crates/ruff --root debian/tmp/usr
	CARGO_BUILT_USING_TARGET_DIR=$(CURDIR)/target/$(DEB_HOST_RUST_TYPE)/release /usr/share/cargo/bin/dh-cargo-built-using ruff
	CARGO_HOME=$(CURDIR)/debian/cargo_home CARGO_TARGET_DIR=$(CURDIR)/target DESTDIR=debian/tmp cargo -Zavoid-dev-deps install --verbose --target $(DEB_HOST_RUST_TYPE) --path crates/ty --root debian/tmp/usr
	CARGO_BUILT_USING_TARGET_DIR=$(CURDIR)/target/$(DEB_HOST_RUST_TYPE)/release /usr/share/cargo/bin/dh-cargo-built-using ty

override_dh_auto_install-indep:
	install -d debian/python3-ruff/$(PYDIR)/ruff
	install -m644 python/ruff/*.py debian/python3-ruff/$(PYDIR)/ruff/
	install -d debian/python3-ruff/$(PYDIR)/ruff-$(RUFF_VER).dist-info
	printf 'Metadata-Version: 2.3\nName: ruff\nVersion: %s\n' '$(RUFF_VER)' > debian/python3-ruff/$(PYDIR)/ruff-$(RUFF_VER).dist-info/METADATA

# ty is released independently of ruff; give its binary package the combined
# version.
override_dh_gencontrol:
	dh_gencontrol -pty -- -v$(TY_DEB_VERSION)
	dh_gencontrol --remaining-packages
