#!/usr/bin/make -f

# -f debian/rules in rules), you need to pass a flag to avoid adding "-jX" when the childs encounters this line
DEB_BUILD_OPTIONS_PARALLEL ?= $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += $(if $(DEB_BUILD_OPTIONS_PARALLEL),-j$(DEB_BUILD_OPTIONS_PARALLEL))

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

# Debian architectures
DEB_BUILD_ARCH ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
DEB_HOST_ARCH  ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

UCFLAGS += -Wall -g -O$(if $(findstring noopt,$(DEB_BUILD_OPTIONS)),0,2)

ifeq (i386,$(DEB_BUILD_ARCH))
UCFLAGS  += -march=i686 -mtune=pentiumpro -mmmx -msse
endif

ifeq (i486,$(DEB_BUILD_ARCH))
UCFLAGS  += -march=i686 -mtune=pentiumpro -mmmx -msse
endif

ifeq (amd64,$(DEB_BUILD_ARCH))
UCFLAGS  += -msse2
endif

export CFLAGS = $(UCFLAGS)
export CXXFLAGS = -Wall $(UCFLAGS)
export DH_VERBOSE = 1
export DEB_BUILD_MAINT_OPTIONS = 
export DEB_CFLAGS_MAINT_APPEND  = -Wall $(UCFLAGS)
export DEB_CXXFLAGS_MAINT_APPEND  = -Wall $(UCFLAGS)
#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed
#LDFLAGS += -Wl,-z,defs

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

%:
	dh $@ --parallel

override_dh_auto_clean:
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -f c++-gtk-utils-2-2.0.pc
	rm -f c++-gtk-utils-3-2.0.pc
	rm -rf debian/tmp1
	rm -rf debian/tmp2

override_dh_auto_configure:
#test gtk 2 and then gtk3
	[ -f ./configure.bak ] || ( mv ./configure ./configure.bak && mv ./configure-gtk2 ./configure )
# now configure gtk2
	dh_auto_configure -- --prefix=/usr --disable-silent-rules --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)
	mv ./configure ./configure-gtk2 && mv ./configure.bak ./configure
# now compile gtk2
	$(MAKE)
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp1 install
	sed -i "/dependency_libs/ s/'.*'/''/" `find debian/tmp1/usr/lib/$(DEB_HOST_MULTIARCH) -type f -name '*.la'`
# now configure gtk3
	[ ! -f Makefile ] || $(MAKE) clean
	rm -f c++-gtk-utils-2-2.0.pc
	[ -f ./configure.bak ] || ( mv ./configure ./configure.bak && mv ./configure-gtk3 ./configure )
	dh_auto_configure -- --prefix=/usr --disable-silent-rules --libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH)
	mv ./configure ./configure-gtk3 && mv ./configure.bak ./configure
# now compile gtk3
	$(MAKE)
	$(MAKE) DESTDIR=$(CURDIR)/debian/tmp2 install
	sed -i "/dependency_libs/ s/'.*'/''/" `find debian/tmp2/usr/lib/$(DEB_HOST_MULTIARCH) -type f -name '*.la'`
# now moving and fixing debian source to deb install
	rm -f debian/tmp1/usr/share/doc/c++-gtk-utils/2.2/COPYING
	rm -f debian/tmp2/usr/share/doc/c++-gtk-utils/2.2/COPYING

override_dh_auto_test:
# Tests require a display

override_dh_auto_build:
#	was buil in configure

override_dh_auto_install:
#	already manage at configure time

override_dh_install:
	mkdir -p debian/tmp
	cp -r debian/tmp1/* debian/tmp/
	cp -r debian/tmp2/* debian/tmp/
	dh_install --list-missing

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog
