#!/usr/bin/make -f

#export DH_VERBOSE=1

DEB_HOST_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE	?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

export DEB_BUILD_MAINT_OPTIONS:=hardening=+all

CXXFLAGS += $(shell dpkg-buildflags --get CXXFLAGS) -Wall -fno-strict-aliasing -Wformat -Wformat-security
LDFLAGS += $(shell dpkg-buildflags --get LDFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64

ifneq (,$(filter noopt,$(DEB_BUILD_OPTIONS)))
        CXXFLAGS += -O0
else
        CXXFLAGS += -O3
endif

# Enable IEEE-conformant floating point math on alphas (not the default)
ifeq (alpha-linux-gnu,$(DEB_HOST_GNU_TYPE))
	CXXFLAGS += -mieee
endif

kgb: kgb_arch_posix_by_slawek.cpp
	dh_testdir
	$(CXX) $(CXXFLAGS) $(LDFLAGS) -o $@ $<

test: kgb
	dh_testdir
ifeq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	sh debian/test.sh
else
	@echo 'Skipping test...'
endif

build-arch: kgb test
build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	$(RM) kgb
	$(RM) -r test
	dh_clean

install: kgb
	dh_testdir
	dh_testroot
	dh_installdirs
	install -D -m 755 kgb debian/kgb/usr/bin/kgb

binary-indep:

binary-arch: install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installman
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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