#!/usr/bin/make -f

export DH_VERBOSE=1

# Install the self-contained venv here.
export DH_VIRTUALENV_INSTALL_ROOT=/opt/sm-sdk

# Ensure automated builds use the expected Python package indexes.
export PIP_EXTRA_INDEX_URL=https://git.spacemit.com/api/v4/projects/33/packages/pypi/simple
export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple

%:
	dh $@ --with python-virtualenv

override_dh_fixperms:
	# Work around a debhelper dh_fixperms implementation that removes owner execute on
	# directories (chmod go=rX,u+rw,a-s), which breaks traversal and aborts the build.
	# We implement a safe, minimal perms normalization instead.
	chown -R 0:0 debian/sm-sdk 2>/dev/null || true
	find debian/sm-sdk -type d -print0 | xargs -0r chmod 0755
	find debian/sm-sdk -type f -perm /111 -print0 | xargs -0r chmod 0755
	find debian/sm-sdk -type f ! -perm /111 -print0 | xargs -0r chmod 0644
	find debian/sm-sdk ! -type l -print0 | xargs -0r chmod a-s

override_dh_strip:
	# Local build: disable stripping/debug-splitting.
	# Some bundled third-party libraries (e.g. sherpa-onnx's libonnxruntime.so)
	# can cause objcopy to fail during debug symbol extraction.
	true

override_dh_dwz:
	# Do not run dwz on bundled wheel binaries.
	# We observed runtime SIGBUS after dh_dwz rewrote some sherpa_onnx .so files
	# (e.g. sherpa_onnx.libs/libasound-*.so), so keep upstream wheels untouched.
	true

override_dh_shlibdeps:
	# dh-virtualenv bundles many private .so files (e.g. scipy.libs, numpy.libs).
	# Help dpkg-shlibdeps find them, otherwise it can fail with “cannot find library”.
	SITEPKG_DIR=$$(echo debian/sm-sdk/opt/sm-sdk/sm-sdk/lib/python*/site-packages); \
	dh_shlibdeps -- \
		-l$${SITEPKG_DIR}/scipy.libs \
		-l$${SITEPKG_DIR}/numpy.libs \
		-l$${SITEPKG_DIR}/sherpa_onnx.libs \
		-l$${SITEPKG_DIR}/sherpa_onnx/lib \
		-l$${SITEPKG_DIR}/onnxruntime/capi

override_dh_virtualenv:
	dh_virtualenv --python=/usr/bin/python3.13
