#!/usr/bin/make -f

export DH_VERBOSE=1

DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

DEB_VERSION := $(shell dpkg-parsechangelog -SVersion | sed 's/-[^-]*$$//')
UPSTREAM_VERSION := $(DEB_VERSION)
SOFTWARE_NAME := sheldon

export CARGO_HOME=$(CURDIR)/.cargo-home
export LIBZ_SYS_STATIC=0
export CARGO_TARGET_DIR=$(CURDIR)/target

%:
	dh $@

override_dh_auto_build:
	# vendor/ is unpacked by dpkg-source from the orig-vendor component;
	# .cargo/config.toml (shipped in the orig tarball) points cargo at it.
	@test -d vendor || { \
		echo "ERROR: vendor/ missing; orig-vendor component did not unpack" >&2; \
		exit 1; \
	}
	@test -f Cargo.lock || { \
		echo "ERROR: Cargo.lock missing; --frozen requires it" >&2; \
		exit 1; \
	}

	RUSTFLAGS="-C link-arg=-Wl,--as-needed" cargo build --release --frozen --offline

	@test -f target/release/$(SOFTWARE_NAME) || (echo "ERROR: binary not built" && exit 1)
	@echo "==> Binary built successfully: $(SOFTWARE_NAME)"

override_dh_auto_install:
	install -Dm755 target/release/$(SOFTWARE_NAME) $(CURDIR)/debian/$(SOFTWARE_NAME)/usr/bin/$(SOFTWARE_NAME)
	install -Dm644 LICENSE-MIT $(CURDIR)/debian/$(SOFTWARE_NAME)/usr/share/doc/$(SOFTWARE_NAME)/copyright

override_dh_auto_test:

override_dh_strip:
