#!/usr/bin/make -f

#export DH_VERBOSE=1
export DH_OPTIONS

DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifeq ($(origin CC),default)
CC = $(DEB_HOST_GNU_TYPE)-gcc
endif

# TODO: there are a lot of warnings with -Wall on. Fix them.
DEB_CFLAGS = `dpkg-buildflags --get CFLAGS`
DEB_CFLAGS += -Wall
DEB_CFLAGS += `dpkg-buildflags --get CPPFLAGS`
LDFLAGS = `dpkg-buildflags --get LDFLAGS`

INSTALL_PROG = install -m 0755
DEB_VER = $(shell dpkg-parsechangelog | sed -n 's/^Version: //p')

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp:
	dh_testdir
	$(MAKE) linux \
	    CC="$(CC) $(DEB_CFLAGS)" LDFLAGS="$(LDFLAGS)" STATIC='' \
	    DFLAGS='-DLINUX -DTELNET -DGAPING_SECURITY_HOLE -DIP_TOS -DDEBIAN_VERSION=\"$(DEB_VER)\"'
	touch $@

build-indep: build-indep-stamp
build-indep-stamp:
	touch $@

clean:
	dh_testdir
	dh_clean build-arch-stamp build-indep-stamp
	$(MAKE) clean

install: install-indep install-arch
install-indep: build-indep
	dh_testdir
	dh_testroot
	dh_prep -i
	dh_installdirs -i
	dh_installdocs -i
	dh_installchangelogs -i
	dh_install -i

install-arch: build-arch
	dh_testdir
	dh_testroot
	dh_prep -s
	dh_installdirs -s
	dh_installdocs -s README
	dh_installexamples -s data scripts debian/contrib
	dh_installchangelogs -s Changelog
	$(INSTALL_PROG) nc $(CURDIR)/debian/netcat-traditional/bin/nc.traditional
	cp debian/man/nc.1 $(CURDIR)/debian/netcat-traditional/usr/share/man/man1/nc.traditional.1

binary-common:
	dh_testdir
	dh_testroot
	dh_link
	dh_strip
	dh_compress -Xexamples
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

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