#!/usr/bin/make -f
# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

BUILD_DIR       := BUILD
DH_AUTO_OPTIONS := -v -Sautoconf --parallel -B$(BUILD_DIR)
DESTDIR         := $(CURDIR)/debian/$(shell dh_listpackages)

DEB_BUILD_MAINT_OPTIONS  := hardening=+all abi=+lfs
# TODO: add support for libtirpc into upstream packaging instead
DEB_CFLAGS_MAINT_APPEND  := $(shell pkgconf --cflags libtirpc)
DEB_LDFLAGS_MAINT_APPEND := $(shell pkgconf --libs libtirpc)
DPKG_EXPORT_BUILDFLAGS   := 1

include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
  CC := $(DEB_HOST_GNU_TYPE)-gcc
else
  CC := gcc
endif

# check for maintainer mode
ifneq (,$(findstring maintainer,$(DEB_BUILD_OPTIONS)))
  MAINTAINER_MODE := yes
else
  MAINTAINER_MODE := no
  ifeq (,$(findstring debian/gcc-wrapper,$(CC)))
    CC            := $(CURDIR)/debian/gcc-wrapper $(CC)
  endif
endif


%:
	dh ${@}

override_dh_auto_configure:
	dh_auto_configure $(DH_AUTO_OPTIONS) -- \
	                  --with-libwrap        \
	                  --with-libcap         \
	                  --without-lsf         \
	                  --without-libnsl      \
	                  --disable-silent-rules \
	                  --enable-warnings      \
	                  --enable-maintainer-mode=$(MAINTAINER_MODE) \
	                  CC="$(CC)"


override_dh_auto_build:
	dh_auto_build  $(DH_AUTO_OPTIONS)

override_dh_auto_clean:
	dh_auto_clean $(DH_AUTO_OPTIONS)

override_dh_auto_install:
	dh_auto_install $(DH_AUTO_OPTIONS)

	cp -a debian/rlinetd.conf $(DESTDIR)/etc
	find $(DESTDIR)/usr/lib -name '*.la' -delete

	sed -e 's/#VERSION#/$(DEB_VERSION)/g'      \
	        <debian/update-inetd.sh            \
	        > $(DESTDIR)/usr/sbin/update-inetd
	chmod 755 $(DESTDIR)/usr/sbin/update-inetd
