#!/usr/bin/make -f
#export DH_VERBOSE=1

# The lib real name version:
include /usr/share/dpkg/pkg-info.mk
LIB_R_VERSION=${DEB_VERSION_UPSTREAM}

# The SONAME version (the upstream asks for making own adjustments to build).
# https://github.com/gdabah/distorm/wiki/BuildCompilationEnvironment#compiling-the-source-code
LIB_S_VERSION=$(shell echo ${DEB_VERSION_UPSTREAM} | cut -d"." -f1)

DESTDIR=$(CURDIR)/debian/libdistorm3-3
TEMPDEB=$(CURDIR)/debian/tmp
LIBMULTIDIR=$(DESTDIR)/usr/lib/${DEB_TARGET_MULTIARCH}

# Providing full GCC hardening with -Wl,-z,now
# and a SONAME with -soname,libdistorm3.so.$(LIB_S_VERSION)
export DEB_LDFLAGS_MAINT_APPEND = -Wl,-z,now,-soname,libdistorm3.so.$(LIB_S_VERSION)

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

# Installing the .so file as real name (from libdistorm3.so to real name).
# This is the only file lib provided from the upstream (with a wrong name).
override_dh_install-arch:
	dh_install usr/lib/python3*/dist-packages/*distorm*.so usr/lib/${DEB_TARGET_MULTIARCH}/
	cd $(LIBMULTIDIR); mv *distorm*.so libdistorm3.so.${DEB_VERSION_UPSTREAM}
	cd $(LIBMULTIDIR); find -name '*distorm*.so' -exec ln -s {} libdistorm3.so.$(LIB_R_VERSION) \;

# Create a symlink for the real name file.
# Note that the runtime package (libdistorm3-3) must be the first binary stanza
# in debian/control.
override_dh_link-arch:
	dh_link usr/lib/${DEB_TARGET_MULTIARCH}/libdistorm3.so.$(LIB_R_VERSION) \
                usr/lib/${DEB_TARGET_MULTIARCH}/libdistorm3.so.3
