#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

DESTDIR=$(CURDIR)/debian/tmp

# This has to be exported to make some magic below work.
export DH_OPTIONS

# 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)
ifneq ($(DEB_HOST_GNU_TYPE), $(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

# These are used for support -j option of dpkg-buildpackage
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
MAKEFLAGS += -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

build: build-arch build-indep
build-arch: build-stamp
build-indep: build-stamp
configure-stamp:
	dh_testdir
	dh_autoreconf
	./configure $(CROSS) --prefix=/usr --sysconfdir=/etc --disable-static \
	    --enable-DBF=incl --enable-SQLite=incl --enable-DBGate=incl --enable-JavaLikeCalc=incl --enable-LogicLev=incl \
	    --enable-Sockets=incl --enable-Serial=incl --enable-SelfSystem=incl --enable-UserProtocol=incl \
	    --enable-FLibComplex1=incl --enable-FLibSYS=incl \
	    --enable-LDAP --enable-MMS --enable-DiamondBoards --enable-Comedi --disable-ICP_DAS --disable-GPIO
	touch $@
build-stamp: configure-stamp
	dh_testdir
	$(MAKE)
	touch $@

install: install-indep install-arch
install-arch: build-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s install-common
install-indep: build-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i install-common
install-common: install-stamp
	dh_testdir
	dh_testroot

	dh_installdirs
	dh_install
install-stamp:
	dh_testdir
	$(MAKE) DESTDIR=$(DESTDIR) install
	touch $@

binary: clean-install binary-arch binary-indep
binary-indep: install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common
binary-arch: install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common
binary-common:
	dh_testdir
	dh_testroot

	dh_installchangelogs ChangeLog
	dh_installdocs
#	dh_installexamples
#	dh_installmenu
#	dh_installdebconf
#	dh_installlogrotate
#	dh_installmime
	dh_installinit
#	dh_installsystemd
#	dh_installcron
#	dh_installinfo
	dh_installman
	dh_link
	dh_strip --dbg-package=openscada-dbg
	dh_compress -X.pdf -X.odt
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

clean-install:
	dh_clean
	rm -f install-stamp

clean:
	dh_testdir
	dh_testroot

	rm -f configure-stamp build-stamp install-stamp

	[ ! -f Makefile ] || $(MAKE) distclean

	dh_autoreconf_clean
	dh_clean

.PHONY: build build-arch build-indep install install-indep install-arch binary binary-indep binary-arch clean
.NOTPARALLEL:
