#!/usr/bin/make -f

include /usr/share/dpkg/pkg-info.mk
include /usr/share/dpkg/architecture.mk

# DEB_VERSION will look like a.b.c[.d]+ubuntuYY.MM[.x]
PACKAGE_MAJOR := $(shell echo $(DEB_VERSION) | cut -d. -f1)

# CUDA_TARGET_ARCH
# if DEB_HOST_ARCH is arm64, CUDA_TARGET_ARCH is sbsa
CUDA_TARGET_ARCH := $(shell if [ "$(DEB_HOST_ARCH)" = "arm64" ]; then echo "sbsa"; else echo "x86_64"; fi)

CUDA_VERSION := $(file < debian/cuda-version)
CUDA_MAJOR := $(shell echo $(CUDA_VERSION) | cut -d. -f1)
CUDA_MINOR := $(shell echo $(CUDA_VERSION) | cut -d. -f2)
CUDA_PATCH := $(shell echo $(CUDA_VERSION) | cut -d. -f3)

.PHONY: binary binary-arch binary-indep build build-arch build-indep clean install install-arch install-indep get-orig-source

get-orig-source:
	debian/scripts/get-orig-source

clean:
	rm -f debian/*.lintian-overrides debian/*.postinst debian/*.postrm
	debian/scripts/replace-templates --verbose
	dh_clean

binary binary-arch binary-indep build build-arch build-indep install install-arch install-indep:
	export CUDA_TARGET_ARCH=$(CUDA_TARGET_ARCH); export NO_PKG_MANGLE=1; dh $@

override_dh_auto_configure:
	# No configuration needed

override_dh_auto_build:
	# Meta package - no build needed

override_dh_auto_test:
	# Skip tests

override_dh_auto_install:
	# Install files from upstream tarball based on architecture
	mkdir -p debian/tmp/
	# Copy architecture-specific directories
	if [ -d "$(DEB_TARGET_ARCH)" ]; then \
		cp -a $(DEB_TARGET_ARCH)/* debian/tmp/ || true; \
	fi

override_dh_install:
	export CUDA_TARGET_ARCH=$(CUDA_TARGET_ARCH); \
	export CUDA_MAJOR=$(CUDA_MAJOR); \
	export CUDA_MINOR=$(CUDA_MINOR); \
	dh_install
override_dh_usrlocal:
	# Skip usr/local - we're violating it on purpose
