#!/usr/bin/make -f
# See debhelper(7) (uncomment to enable)
#export DH_VERBOSE = 1

# multiarch
include /usr/share/dpkg/architecture.mk

build_multiarch=build/$(DEB_HOST_MULTIARCH)
install_bin=debian/tmp/usr/bin
install_lib=debian/tmp/usr/lib
install_lib_multiarch=debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
install_libexec_multiarch=debian/tmp/usr/libexec/$(DEB_HOST_MULTIARCH)

# parallelism
DEB_BUILD_OPTIONS ?= parallel=8

# hardening
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
# Use build/ for output, so that it is in the .gitignore of upstream
	dh $@ --buildsystem=cmake --builddirectory=$(build_multiarch)

override_dh_auto_configure:
	dh_auto_configure -- \
	-DCMAKE_LIBRARY_ARCHITECTURE="$(DEB_HOST_MULTIARCH)" \
	-DCMAKE_BUILD_TYPE=RelWithDebInfo \
	\
	-DBUILD_SHARED_LIBS=ON \
	-DWHISPER_USE_SYSTEM_GGML=ON \
	-DCMAKE_LIBRARY_PATH=/usr/libexec/$(DEB_HOST_MULTIARCH)/ggml \
	\
	-DWHISPER_BUILD_TESTS=OFF \
	-DWHISPER_BUILD_EXAMPLES=ON \
	-DWHISPER_FFMPEG=OFF \
	-DWHISPER_SDL2=ON \

override_dh_auto_install:
	dh_auto_install
	
	# Move executables to libexec, so that they can load the GGML backends
	# and link them to bin
	mkdir -p $(install_libexec_multiarch)/ggml
	for file in $(install_bin)/whisper-*; do \
    	mv $$file $(install_libexec_multiarch)/ggml/$$(basename "$$file"); \
		ln -s --relative -t $(install_bin) $(install_libexec_multiarch)/ggml/$$(basename "$$file"); \
	done
	
	# Multiarch pkgconfig
	mkdir -p $(install_lib_multiarch)/pkgconfig
	mv $(install_lib)/pkgconfig/*.pc $(install_lib_multiarch)/pkgconfig

override_dh_auto_test:
	# tests which depends on remote location are failing
	dh_auto_test || true
