#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-

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

# Activate full hardening build flags
export DEB_BUILD_MAINT_OPTIONS := hardening=+all

PKD := $(abspath $(dir $(MAKEFILE_LIST)))

include /usr/share/dpkg/default.mk

DIR_PPP := $(CURDIR)/debian/ppp
DIR_PPP_DEV := $(CURDIR)/debian/ppp-dev
DIR_PPP_UDEB := $(CURDIR)/debian/ppp-udeb

PPP_PLUGIN_ABI = $(shell awk -F ' *= *' '/^VERSION / { print $$2 }' \
	build-deb/Makefile)
VERSION_DEBIAN := $(shell echo '$(DEB_VERSION)' | \
	perl -ne 'print $$1 if m{^(?:\d+:)?(?:\d.*)\-(\d+.*)$$};')
VERSION_ABI_PART := $(shell echo "$(VERSION_DEBIAN)" | \
	perl -ne 'print $$1 if m{^(^\d+)\+};')

CONFFLAGS = \
  --prefix=/usr \
  --libdir=/usr/lib \
  --disable-static \
  --with-logfile-dir=/var/log \
  --with-runtime-dir=/run/ \
  # end

CONFFLAGS_deb  = $(CONFFLAGS) \
  $(shell dpkg-buildflags --export=configure || true) \
  --enable-cbcp \
  --enable-multilink \
  --enable-systemd \
  # end

CONFFLAGS_udeb = $(CONFFLAGS) \
  $(subst -O2,-O2 -Os -fomit-frame-pointer,$(shell dpkg-buildflags --export=configure || true)) \
  --without-pam \
  --without-pcap \
  --disable-eaptls \
  --disable-microsoft-extensions \
  --disable-openssl-engine \
  --disable-peap \
  # end

##############################################################################
%:
	dh $@ --builddir=build-deb

build-deb/config.status:
	dh_auto_configure -- $(CONFFLAGS_deb)

build-udeb/config.status:
	rm -f pppd/pppdconf.h
	dh_auto_configure --builddir=build-udeb -- $(CONFFLAGS_udeb)
	perl -i -pe 's/^#define (HAVE_CRYPT_H|HAVE_SHADOW_H) .*//' build-udeb/pppd/config.h
	perl -i -pe 's/^(CRYPT_LIBS =).*/$$1/' build-udeb/pppd/Makefile
	perl -i -pe 's/^(SUBDIRS =).*/$$1 pppoe pppoatm/; s/^(pppd_plugin_LTLIBRARIES =).*//' build-udeb/pppd/plugins/Makefile

override_dh_auto_configure: build-deb/config.status build-udeb/config.status
	@if [ "$(PPP_PLUGIN_ABI)" != "$(DEB_VERSION_UPSTREAM)" ]; then \
	  echo; echo "**********"; \
	  echo "VERSION in Makefile must match upstream version."; \
	  echo "Upstream version is '$(DEB_VERSION_UPSTREAM)'"; \
	  echo "VERSION is '$(PPP_PLUGIN_ABI)'"; \
	  echo "Aborting the build."; \
	  echo "**********"; echo; \
	  exit 1; \
	fi
	@if [ -z "$(VERSION_ABI_PART)" ]; then \
	  echo; echo "**********"; \
	  echo "No ABI component found in the Debian revision field:"; \
	  echo "$(VERSION_ABI_PART)"; \
	  echo "Please see debian/README.source for more information."; \
	  echo "Aborting the build."; \
	  echo "**********"; echo; \
	  exit 1; \
	fi

build-deb/changelog:
	{ \
	  sed -ne "/^What's new in / {p; :1; n; /^For a list of the changes made during /q; p; b1}" README ; \
	  echo ; \
	  cat Changes-2.4 ; \
	  cat Changes-2.3 ; \
	} > $@

override_dh_auto_build:
	dh_auto_build
	dh_auto_build --builddir=build-udeb

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp
	dh_auto_install --builddir=build-udeb --destdir=debian/tmp-udeb

execute_after_dh_auto_install-indep:
	$(PKD)/gen_substvars_dev $(DEB_VERSION_UPSTREAM) $(VERSION_ABI_PART) \
		> $(DIR_PPP_DEV)/usr/share/ppp-dev/substvars
	pod2man --section=1p --utf8 debian/extra/dh_ppp \
		$(DIR_PPP_DEV)/usr/share/man/man1/dh_ppp.1p

execute_after_dh_install-arch:
	rm -f $(DIR_PPP)/usr/lib/pppd/*/*.*a $(DIR_PPP_UDEB)/usr/lib/pppd/*/*.*a
	cd $(DIR_PPP)/usr/lib/pppd/*/ && ln -s pppoe.so rp-pppoe.so

override_dh_installchangelogs: build-deb/changelog
	dh_installchangelogs build-deb/changelog

execute_after_dh_installexamples-arch:
	rm $(DIR_PPP)/usr/share/doc/ppp/examples/scripts/Makefile*
	# use our own version, not the upstream one
	mv $(DIR_PPP)/usr/share/doc/ppp/examples/autopppd \
		$(DIR_PPP)/usr/share/doc/ppp/examples/scripts/

execute_after_dh_lintian:
	install -D -p -m644 debian/ppp-udeb.lintian-overrides \
		$(DIR_PPP_UDEB)/usr/share/lintian/overrides/ppp-udeb

override_dh_perl:
	dh_perl -d --package=ppp-dev

execute_after_dh_fixperms-arch:
	chown root:dip \
		$(DIR_PPP)/etc/chatscripts/ \
		$(DIR_PPP)/etc/ppp/ \
		$(DIR_PPP)/etc/ppp/peers/ \
		$(DIR_PPP)/usr/sbin/pppd
	chmod 4754 $(DIR_PPP)/usr/sbin/pppd
	chmod 2750 $(DIR_PPP)/etc/ppp/peers/ $(DIR_PPP)/etc/chatscripts/

# Force dpkg-gensymbols to inspect pppd (it doesn't end .so) so that the
# symbols file is generated. This needs the SONAME hack that gets applied in
# debian/patches/pppd-soname-hack.patch to work. The -c2 below makes sure that
# the build fails if there are any discrepancies in the symbols file in order
# to catch ABI changes.
override_dh_makeshlibs:
	dh_makeshlibs --no-scripts --package=ppp -Xusr/lib/pppd/ -- -c2 -edebian/ppp/usr/sbin/pppd -V
	dh_makeshlibs --no-package=ppp
