#!/usr/bin/make -f
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

export DEB_BUILD_MAINT_OPTIONS   := hardening=+all
# this upstream version is required to always have a YYYY.XX version. slang sometimes adds .1 to it
export UPSTREAM_VERSION := $(shell echo $(DEB_VERSION_EPOCH_UPSTREAM) | sed 's/\([[:digit:]]\+\.[[:digit:]]\+\).*/\1/')
export DEB_LDFLAGS_MAINT_APPEND := -Wl,-rpath,/usr/lib/$(DEB_HOST_MULTIARCH)/slang-compiler
export DH_VERBOSE := 1


%:
	dh $@ --builddirectory=build/

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_BUILD_TYPE=RelWithDebInfo \
		-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH) \
		-DSLANG_SLANG_LLVM_FLAVOR="DISABLE" \
		-DSLANG_ENABLE_EXAMPLES="ON" \
		-DSLANG_ENABLE_TESTS="ON" \
		-DSLANG_ENABLE_SLANG_RHI="ON" \
		-DSLANG_ENABLE_SPLIT_DEBUG_INFO="OFF" \
		-DSLANG_ENABLE_GFX="ON" \
		-DSLANG_RHI_ENABLE_CUDA="OFF" \
		-DSLANG_RHI_ENABLE_OPTIX="OFF" \
		-DSLANG_USE_SYSTEM_UNORDERED_DENSE="OFF" \
		-DSLANG_USE_SYSTEM_MINIZ="OFF" \
		-DSLANG_USE_SYSTEM_LZ4="ON" \
		-DSLANG_USE_SYSTEM_VULKAN_HEADERS="ON" \
		-DSLANG_USE_SYSTEM_SPIRV_TOOLS="ON" \
		-DSLANG_USE_SYSTEM_SPIRV_HEADERS="ON" \
		-DSLANG_USE_SYSTEM_GLSLANG="ON" \
		-DSLANG_VERSION_FULL=$(DEB_SOURCE) \
		-DSLANG_VERSION_NUMERIC=$(UPSTREAM_VERSION)

# override_dh_makeshlibs:
# 	dh_makeshlibs -V -plibslang-compiler0.${UPSTREAM_VERSION} -plibslang-rt0.${UPSTREAM_VERSION} -plibgfx0.${UPSTREAM_VERSION} -- -c4

override_dh_auto_build:
	dh_auto_build
	help2man --name="Slang Compiler" --help-option="-h" --version-string="$(UPSTREAM_VERSION)" build/RelWithDebInfo/bin/slangc  > slangc.1
	help2man --name="Slang Interpreter" --help-option="-help" --version-string="$(UPSTREAM_VERSION)" build/RelWithDebInfo/bin/slangi > slangi.1

override_dh_install:
	# The headers are installed double and are not correct this way. Remove the
	# duplicate
	rm debian/tmp/usr/include/*.h
	# This is the installed slang library which we do not want as we also have
	# the libslang-compiler one. But it is create due to the cmake wrapper
	rm debian/tmp/usr/lib/libslang.so

	# modify the cmake import prefix creation
	sed -i -e "s|get_filename_component(_IMPORT_PREFIX.*|set(_IMPORT_PREFIX \"/usr\")|" debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/slang/slangTargets.cmake

	# This is done with sed because it needs to include the upstream version
	# and adjust the folders as well as the path names. the two modules are
	# located in a slang-compiler-VERSION folder, which is not seen in the
	# cmake file as cmake install it all in the same directory due to
	# everything being wrapped into the same function
	sed -i -E \
		-e 's|${_IMPORT_PREFIX}/lib|${_IMPORT_PREFIX}/lib/${DEB_HOST_MULTIARCH}|' \
		-e "s|(libslang-gl)|slang-compiler/\1|" \
		debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/cmake/slang/slangTargets-relwithdebinfo.cmake

	dh_install

override_dh_auto_test:
	(./build/RelWithDebInfo/bin/slang-test -category smoke -use-fully-isolated-test-server -expected-failure-list tests/expected-failure-github.txt  -expected-failure-list tests/expected-failure-no-gpu.txt)
	dh_auto_test
