#!/usr/bin/make -f
#export DH_VERBOSE = 1

# Cargo from PATH when the `cargo` package is installed (see Build-Depends).
# Override: `dpkg-buildpackage` → pass through environment, e.g. `CARGO=/path/to/cargo dpkg-buildpackage`.
CARGO ?= cargo
# Prefer clang for aws-lc-sys/toolchain compatibility.
CC := $(shell command -v clang 2>/dev/null || command -v clang-10 2>/dev/null || command -v cc 2>/dev/null)
export CC
DEB_HOST_RUST_TYPE := $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE | sed 's/-gnu$$/_unknown_linux_gnu/' | tr '-' '_')
export CC_$(DEB_HOST_RUST_TYPE)=$(CC)

%:
	dh $@

# debhelper does not detect Rust/Cargo; build the workspace explicitly.
override_dh_auto_build:
	# Build frontend assets
	cd web_kbot && npm ci && npm run build
	# Build Rust binaries
	$(CARGO) build --release --locked

override_dh_auto_install:
	install -D -m 0755 target/release/zeroclaw \
		debian/kylin-bot/opt/system/bin/kylinbot
	install -d debian/kylin-bot/usr/share/zeroclawlabs/web/dist
	cp -a web_kbot/dist/. debian/kylin-bot/usr/share/zeroclawlabs/web/dist/
	# Install skills directory to system share directory
	mkdir -p debian/kylin-bot/usr/share/kylinbot/skills
	cp -r skills/ debian/kylin-bot/usr/share/kylinbot/

# GUI integration tests are not suitable for default package builds.
override_dh_auto_test:
	:

override_dh_auto_clean:
	dh_auto_clean
	-$(CARGO) clean

override_dh_shlibdeps:
	@echo "skip dh_shlibdeps intentionally"
