#!/usr/bin/make -f

# See debhelper(7) (uncomment to enable).
# Output every command that modifies files on the build system.

include /usr/share/dpkg/architecture.mk

# Determine SOVERSION of OIIO and OSL for move of plugin to versioned OIIO directory
OIIO_SOVERSION := $(shell dpkg-query -f '$${Version}' -W libopenimageio-dev | cut -d '.' -f 1-2)
OSL_SOVERSION := $(shell dpkg-parsechangelog -SVersion | sed 's/^.://' | cut -d '.' -f 1-2)

INSTDIR = $(CURDIR)/debian/tmp

# Logic to handle SIMD based on architecture.
# Using "" instead of "none" prevents OSL from passing the invalid "-mnone" to the compiler.
ifeq ($(DEB_HOST_ARCH),amd64)
    SIMD_FLAGS = -DUSE_SIMD=sse4.2
else ifeq ($(DEB_HOST_ARCH),i386)
    SIMD_FLAGS = -DUSE_SIMD=sse2
else
    # For aarch64, armhf, etc.
    SIMD_FLAGS = -DUSE_SIMD=""
endif

#export DH_VERBOSE = 1

# See FEATURE AREAS in dpkg-buildflags(1).
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# See ENVIRONMENT in dpkg-buildflags(1).
# Package maintainers to append CFLAGS.
#export DEB_CFLAGS_MAINT_APPEND  = -Wall -pedantic
# Package maintainers to append LDFLAGS.
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed


%:
	dh $@ --buildsystem=cmake --with python3


# dh_make generated override targets.
# This is an example for Cmake (see <https://bugs.debian.org/641051>).
#override_dh_auto_configure:
#	dh_auto_configure -- \
#	-DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)

override_dh_auto_configure:
	dh_auto_configure -- \
		-DCMAKE_INSTALL_LIBDIR=/usr/lib/$(DEB_HOST_MULTIARCH) \
		-DCMAKE_CXX_STANDARD=17 \
		-DLLVM_STATIC=OFF \
		$(SIMD_FLAGS) \
		-DSTOP_ON_WARNING=OFF

override_dh_install:
	# 1. Clean up python site-packages in the source temp dir
	rm -f $(INSTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/python3*/site-packages/oslquery/*.py

	# 2. Use versioned OpenImageIO directory name
	# We MUST do this BEFORE dh_install so the .install files can find the moved files
	mkdir -p $(INSTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/OpenImageIO-$(OIIO_SOVERSION)

	# 3. Move the imageio plugin to the versioned directory
	if [ -f $(INSTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/osl.imageio.so ]; then \
		mv $(INSTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/osl.imageio.so \
		   $(INSTDIR)/usr/lib/$(DEB_HOST_MULTIARCH)/OpenImageIO-$(OIIO_SOVERSION)/; \
	fi

	# 4. NOW run dh_install to process the .install files
	dh_install

override_dh_missing:
	dh_missing --fail-missing

override_dh_shlibdeps:
	# Ignore missing info for LLVM symbols which OSL uses heavily
	dh_shlibdeps --dpkg-shlibdeps-params=--ignore-missing-info

override_dh_auto_test:
	dh_auto_test || echo "Ignoring test results for now."

override_dh_installchangelogs:
	dh_installchangelogs CHANGES.md