#!/usr/bin/make -f

RUST_VERSION ?= 1.88.0
RUST_SERIES ?= 1.88
RUST_TRIPLE ?= riscv64gc-unknown-linux-gnu
RUST_DIST_TARBALL ?= vendor/rust-$(RUST_VERSION)-$(RUST_TRIPLE).tar.xz
INSTALL_PREFIX := /opt/openkylin-xr/rust-$(RUST_SERIES)

%:
	dh $@

override_dh_auto_build:
	test -s "$(RUST_DIST_TARBALL)" || \
	  (echo "missing $(RUST_DIST_TARBALL); run tools/fetch-rust-dist.sh first" >&2; exit 1)

override_dh_auto_install:
	mkdir -p debian/openkylin-xr-rust-1.88/tmp-src
	tar -C debian/openkylin-xr-rust-1.88/tmp-src --strip-components=1 -xf "$(RUST_DIST_TARBALL)"
	debian/openkylin-xr-rust-1.88/tmp-src/install.sh \
	  --prefix="$(CURDIR)/debian/openkylin-xr-rust-1.88$(INSTALL_PREFIX)" \
	  --components="rustc,cargo,rust-std-$(RUST_TRIPLE)" \
	  --disable-ldconfig
	mkdir -p debian/openkylin-xr-rust-1.88/usr/lib/openkylin-xr/rust-$(RUST_SERIES)
	{ \
	  echo 'export OPENKYLIN_XR_RUST_HOME=$(INSTALL_PREFIX)'; \
	  echo 'export PATH="$$OPENKYLIN_XR_RUST_HOME/bin:$$PATH"'; \
	} >debian/openkylin-xr-rust-1.88/usr/lib/openkylin-xr/rust-$(RUST_SERIES)/env
	rm -rf debian/openkylin-xr-rust-1.88/tmp-src

override_dh_auto_test:
	:

override_dh_fixperms:
	dh_fixperms
	chmod 0755 debian/openkylin-xr-rust-1.88$(INSTALL_PREFIX)/bin/*

override_dh_clean:
	dh_clean
	rm -rf debian/openkylin-xr-rust-1.88/tmp-src
