#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

# Runtime configuration
VERSION ?= $(shell debian/get-version.sh)
ARCH = riscv64
PREFIX = spacemit-ort
SUFFIX = tar.gz
TARGET = onnxruntime
NEXUS_URL_REPO = https://nexus.bianbu.xyz/repository/ai/$(TARGET)
RUNTIME_DIR = v$(VERSION)/workspace/$(PREFIX)
PACKAGE_FILE = v$(VERSION)/dist/$(PREFIX).$(ARCH).$(VERSION).$(SUFFIX)

%:
	dh $@

.PHONY: download-spacemit-ort
download-spacemit-ort:
	# Download and extract spacemit-ort if not exists
	@if [ ! -d "$(RUNTIME_DIR)" ]; then \
		echo "Downloading and extracting spacemit-ort v$(VERSION)..."; \
		mkdir -p v$(VERSION)/dist; \
		if [ ! -f "$(PACKAGE_FILE)" ]; then \
			wget "$(NEXUS_URL_REPO)/$(PREFIX).$(ARCH).$(VERSION).$(SUFFIX)" \
				-O "$(PACKAGE_FILE)" --no-check-certificate; \
		fi; \
		mkdir -p "$(RUNTIME_DIR)"; \
		tar xzf "$(PACKAGE_FILE)" -C "$(RUNTIME_DIR)" --strip-components 1; \
	fi
	# Update softlink
	@if [ -L "$(PREFIX)" ]; then rm "$(PREFIX)"; fi
	@ln -sf "$(RUNTIME_DIR)" "$(PREFIX)"
	# Verify python wheel package exists
	@PY_WHL_CNT=$$(ls $(PREFIX)/python/spacemit_ort*-py3-none-linux_riscv64.whl 2>/dev/null | wc -l); \
	if [ "$$PY_WHL_CNT" != "1" ]; then \
		echo "ERROR: Unexpected spacemit_ort cp313 wheel package count: $$PY_WHL_CNT != 1"; \
		exit 1; \
	fi

override_dh_prep:
	dh_prep
	@$(MAKE) -f debian/rules download-spacemit-ort

override_dh_auto_install-arch:
	dh_auto_install $@
# 	# install(copy) prebuilt onnxruntime
# 	mkdir -p $(CURDIR)/debian/onnxruntime/usr
# 	for d in bin lib include; do cp -rdf $(CURDIR)/spacemit-ort/$${d} $(CURDIR)/debian/onnxruntime/usr; done
	# install prebuilt spacemit-ort wheel package
	mkdir -p $(CURDIR)/debian/python3-spacemit-ort/usr
	python3 -m pip install \
		$(CURDIR)/spacemit-ort/python/spacemit_ort-*-py3-none-linux_riscv64.whl \
		--prefix=$(CURDIR)/debian/python3-spacemit-ort/usr \
		--force-reinstall \
		--ignore-installed \
		--break-system-packages \
		-i https://pypi.tuna.tsinghua.edu.cn/simple \
		--extra-index-url https://git.spacemit.com/api/v4/projects/33/packages/pypi/simple \
		--trusted-host pypi.tuna.tsinghua.edu.cn \
		--trusted-host git.spacemit.com
	if [ -d $(CURDIR)/debian/python3-spacemit-ort/usr/local ]; then \
		mv $(CURDIR)/debian/python3-spacemit-ort/usr/local/* $(CURDIR)/debian/python3-spacemit-ort/usr; \
		rm -rf $(CURDIR)/debian/python3-spacemit-ort/usr/local; \
	fi
	find $(CURDIR)/debian/python3-spacemit-ort/usr -name "__pycache__" -type d | xargs rm -rf
	rm -rf $(CURDIR)/debian/python3-spacemit-ort/usr/bin
	patchelf --add-needed libonnxruntime.so $(CURDIR)/debian/python3-spacemit-ort/usr/lib/python3.14/dist-packages/spacemit_ort/libspacemit_ep.so.*
	rm -rf $(CURDIR)/debian/python3-spacemit-ort/usr/share

# TODO: fix install whl package directly
override_dh_virtualenv-indep:
	dh_virtualenv -v --requirement=requirements.txt --preinstall $(CURDIR)/spacemit-ort/python/spacemit_ort-*-py3-none-linux_riscv64.whl --destdir=$(CURDIR)/debian/python3-spacemit-ort

override_dh_installdeb:
	dh_installdeb $@
	# post process: copy udev rules for udev-rules
	mkdir -p $(CURDIR)/debian/udev-rules/usr/lib/udev/rules.d/
	cp udev-rules/99-tcm.rules $(CURDIR)/debian/udev-rules/usr/lib/udev/rules.d/99-tcm.rules

override_dh_strip:
	dh_strip --exclude=python3-spacemit-ort

override_dh_shlibdeps:
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info
