#!/usr/bin/make -f

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

ifneq (,$(filter $(DEB_BUILD_ARCH),armhf arm64 armel ppc64el s390x))
SIGNED_CHAR	 = -fsigned-char
else
SIGNED_CHAR	 =
endif

CFLAGS = $(shell dpkg-buildflags --get CFLAGS) $(shell dpkg-buildflags --get CPPFLAGS) $(SIGNED_CHAR) -Wno-error -fcommon
CXXFLAGS = $(shell dpkg-buildflags --get CXXFLAGS) $(shell dpkg-buildflags --get CPPFLAGS) -std=gnu++98 -Wno-error
LDFLAGS = $(shell dpkg-buildflags --get LDFLAGS)

cmake_options = \
	-DCMAKE_INSTALL_PREFIX=/usr \
	-DCMAKE_VERBOSE_MAKEFILE=ON \
	-DCMAKE_BUILD_TYPE=relwithdebinfo \
	-DCMAKE_C_FLAGS_RELWITHDEBINFO="$(CFLAGS)" \
	-DCMAKE_CXX_FLAGS_RELWITHDEBINFO="$(CXXFLAGS)" \
	-DCMAKE_SHARED_LINKER_FLAGS_RELWITHDEBINFO="$(LDFLAGS)" \
	-DCMAKE_EXE_LINKER_FLAGS_RELWITHDEBINFO="$(LDFLAGS)" \
	-DCMAKE_INSTALL_RPATH=/usr/lib/$(DEB_HOST_MULTIARCH)/cuneiform

parallel = -j$(or $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS))),1)

# These don't need to be exported and if they are we'll get the flags
# duplicated in the command line.
unexport CFLAGS
unexport CXXFLAGS
unexport LDFLAGS

export DPKG_GENSYMBOLS_CHECK_LEVEL = 4

# build and build-{arch,indep} targets
# ====================================

.PHONY: build build-arch build-indep

build: build-arch build-indep

build-indep:
# Nothing to do here.

build-arch: obj/build-stamp obj/test-stamp

obj/CMakeCache.txt: CMakeLists.txt
	mkdir -p obj
	cd obj && cmake .. $(cmake_options)

obj/build-stamp: obj/CMakeCache.txt
	$(MAKE) $(parallel) -C obj
	touch $(@)

obj/test-stamp: obj/build-stamp
ifeq "$(filter nocheck,$(DEB_BUILD_OPTIONS))" ""
	CF_DATADIR=$(CURDIR)/datafiles/ debian/tests/ground-truth obj/cuneiform
endif
	touch $(@)


# binary and binary-{arch,indep} targets
# ======================================

.PHONY: binary binary-arch binary-indep

binary: binary-arch binary-indep

binary-arch: build-arch
	dh_testroot
	dh_prep -a
	$(MAKE) -C obj install DESTDIR=$(CURDIR)/debian/tmp/
	dh_install -p libcuneiform-dev '/usr/lib*/libcuneiform.so' /usr/lib/$(DEB_HOST_MULTIARCH)/
	dh_install -p libcuneiform0 '/usr/lib*/lib*.so.*' /usr/lib/$(DEB_HOST_MULTIARCH)/cuneiform/
	cd debian/libcuneiform0/usr/lib/$(DEB_HOST_MULTIARCH)/cuneiform/ && mv -t ../ libcuneiform.so.*
	dh_install -a
	dh_installdocs -a
	dh_installchangelogs -a
	dh_buildinfo -a
	dh_installman -a
	dh_compress -a
	dh_fixperms -a
	dh_strip -a
	dh_makeshlibs -a -X/cuneiform/
	dh_shlibdeps -a
	dh_installdeb -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary-indep: build-indep
	dh_testroot
	dh_prep -i -X tmp
	dh_install -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_buildinfo -a
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# clean target
# ============

.PHONY: clean
clean:
	dh_clean
	rm -rf obj

# get-orig-source target
# ======================

.PHONY: get-orig-source
get-orig-source:
	sh $(here)/debian/get-orig-source.sh $(DEB_VERSION_UPSTREAM)