#!/usr/bin/make -f

export BDIR             := BUILD
export LDFLAGS          := -Wl,-z,defs -Wl,--as-needed $(shell dpkg-buildflags --get LDFLAGS)
export CPPFLAGS         := $(shell dpkg-buildflags --get CPPFLAGS)
export CXXFLAGS         := $(shell dpkg-buildflags --get CXXFLAGS) -Wall -Wno-deprecated -I$(CURDIR)
export QTDIR            := /usr
DH_AUTO_OPTIONS         := -v -Sautoconf --parallel -B$(BDIR)
DESTDIR                 := $(CURDIR)/debian/tmp

build: export MISSING_BUILD_DEPS := $(shell dpkg-checkbuilddeps 2>&1)
%: force
	dh "$@"


ifneq ($(shell dh_listpackages),$(shell dh_listpackages -i))  # arch:any packages are built
override_dh_auto_build:   $(BDIR)/build-arch-stamp

override_dh_auto_install: install-arch-into-pkgdir
endif

ifneq ($(shell dh_listpackages),$(shell dh_listpackages -a)) # arch:all packages are built
  ifneq (,$(MISSING_BUILD_DEPS))
    $(warning $(MISSING_BUILD_DEPS))
    $(warning Postponing build of architecture independent files until binary or binary-indep target is called)
  else
override_dh_auto_build:   $(BDIR)/build-indep-stamp
  endif
override_dh_auto_install: install-indep-into-pkgdir
endif

override_dh_auto_configure: $(BDIR)/configure-stamp


override_dh_auto_clean:
	rm -f $(BDIR)/build-arch-stamp $(BDIR)/build-indep-stamp $(BDIR)/configure-stamp
	$(MAKE) -C doc/docbook/guide clean
	dh_autoreconf_clean
	dh_auto_clean  $(DH_AUTO_OPTIONS)


$(BDIR)/configure-stamp:
	dh_autoreconf
	dh_auto_configure $(DH_AUTO_OPTIONS) -- \
	                  --enable-nls  
	touch "$@"

$(BDIR)/build-arch-stamp: $(BDIR)/configure-stamp
	dh_auto_build $(DH_AUTO_OPTIONS) 
	
	dh_auto_build $(DH_AUTO_OPTIONS) -- -C po
	touch "$@"
	

$(BDIR)/build-indep-stamp: $(BDIR)/configure-stamp
	$(MAKE) -C doc/docbook/guide
	touch "$@"
	


install-arch-into-pkgdir: $(BDIR)/build-arch-stamp
	dh_auto_install $(DH_AUTO_OPTIONS)
	dh_auto_install $(DH_AUTO_OPTIONS) -- -C po
	
	mkdir -p $(DESTDIR)/usr/share/pixmaps
	install --mode=644 pics/subcommander.xpm $(DESTDIR)/usr/share/pixmaps


install-indep-into-pkgdir: $(BDIR)/build-indep-stamp
	$(MAKE) -C doc/docbook/guide install  DESTDIR=$(DESTDIR)/usr/share/doc/subcommander/html

force debian/rules:

.PHONY: force debian/rules \
	override_dh_auto_build override_dh_auto_clean \
	override_dh_auto_configure override_dh_auto_install \
	install-arch-into-pkgdir install-indep-into-pkgdir

.DEFAULT_GOAL := binary
