#!/usr/bin/make -f

PKG_VERSION  := $(shell dpkg-parsechangelog | grep -E '^Version:' | cut -d' ' -f2)
DIST_NAME    := $(shell lsb_release -si)
DIST_RELEASE := $(shell lsb_release -sc)
DIST_VERSION := $(shell lsb_release -sr)
PKG_VERSION_TAG := $(PKG_VERSION)+$(DIST_NAME).$(DIST_RELEASE).$(DIST_VERSION)
SUBSTVARS_BINARY_VERSION := -Vbinary:Version="$(PKG_VERSION_TAG)"
SUBSTVARS_SOURCE_VERSION := -Vsource:Version="$(PKG_VERSION_TAG)"
DEB_TARGET_GNU_TYPE := $(shell dpkg-architecture -qDEB_TARGET_GNU_TYPE)

# Default value seems to be a bit small...
# For instance, a 115Mb JPEG 2000 cannot be open through
# OpenCV with this setting
#MAX_SAMPLES = $(shell expr 64 \* 1048576)
MAX_SAMPLES = $(shell expr 64 \* 1048576 \* 1024)
CMAKE_C_FLAGS += -DJAS_DEC_DEFAULT_MAX_SAMPLES=$(MAX_SAMPLES) $(shell dpkg-buildflags --get CFLAGS)

%:
	dh $@ --parallel

override_dh_auto_configure:
	dh_auto_configure -- \
	  -DCMAKE_C_FLAGS="$(CMAKE_C_FLAGS)"
# Disable ATM, breaks unit tests....
#	  -DCMAKE_SKIP_RPATH=ON

override_dh_auto_install:
	dh_auto_install
	find debian/tmp/usr/bin/ -type f -exec chrpath --delete {} \;
	find debian/tmp/usr/lib/ -name '*.so.*.*.*' -type f -exec chrpath --delete {} \;

override_dh_gencontrol:
	dh_gencontrol -- -v"$(PKG_VERSION_TAG)" $(SUBSTVARS_BINARY_VERSION) $(SUBSTVARS_SOURCE_VERSION)
