#!/usr/bin/make -f
SHELL=/bin/bash -e

export DH_VERBOSE=1
export NOISY=1

#BUILD_UDEB := 1

include debian/scripts/vars

BUILD_DIR := $(SOURCE_DIR)/$(TAR_DIR)
B := $(BUILD_DIR)
D := $(CURDIR)/debian/ppp

ifdef BUILD_UDEB
PACKAGE := ppp-udeb
VERSION := $(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)
ARCH := $(shell dpkg --print-architecture)
FILENAME := $(PACKAGE)_$(VERSION)_$(ARCH).udeb
PRIORITY := $(shell grep ^Priority: debian/control | head -1 | cut -d ' ' -f 2)
endif

# Use soft-float and thumb mode if it enabled.
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -mthumb
endif

export CFLAGS

# Various collections of files for use with dh_* tools.
DOCS  := README.linux FAQ README SETUP PLUGINS README.MSCHAP80 README.cbcp
DOCS2 := README.STATIC-IP README.win9x defaultroute

all: build

unpack: $(STAMP_DIR)/unpack
$(STAMP_DIR)/unpack:
	$(MAKE) -f debian/sys-build.mk source.make
	touch $@

# used by the maintainer
unpack.nopatch: 
	$(MAKE) -f debian/sys-build.mk source.build

# used by the maintainer
diff:
	$(MAKE) -f debian/sys-build.mk make-diff

clean:
	$(MAKE) -f debian/sys-build.mk source.clean
	dh_clean


configure: $(STAMP_DIR)/configure
$(STAMP_DIR)/configure: $(STAMP_DIR)/unpack
	dh_testdir
	-$(MAKE) clean
	$(MAKE) -f debian/sys-build.mk source.command SOURCE_CMD=" \
		./configure \
	"
	touch $@


build: $(STAMP_DIR)/build
$(STAMP_DIR)/build: $(STAMP_DIR)/configure
	dh_testdir
	$(MAKE) -f debian/sys-build.mk source.command SOURCE_CMD=" \
		$(MAKE) USE_PAM= FILTER= HAVE_INET6= CBCP= \
	"
	touch $@

udeb: build-udeb
build-udeb: $(STAMP_DIR)/build-udeb
$(STAMP_DIR)/build-udeb: $(STAMP_DIR)/configure
ifdef BUILD_UDEB
	dh_testdir
	cd $B && if [ ! -d pppd-udeb/ ]; then \
		mkdir pppd-udeb/ && \
		cd pppd-udeb/ && \
		chmod +x $(CURDIR)/extra/lndir.sh && \
		$(CURDIR)/extra/lndir.sh ../pppd && \
		$(MAKE) clean && \
		cd plugins && $(MAKE) clean ; \
	fi
	$(MAKE) -f debian/sys-build.mk source.command SOURCE_CMD=" \
		cd pppd-udeb/ && \
		$(MAKE) COPTS=\"-Os -fomit-frame-pointer\" \
			CHAPMS= USE_CRYPT= NO_CRYPT_HACK=1 MPPE= \
			FILTER= HAVE_MULTILINK= USE_TDB= \
			HAS_SHADOW= USE_PAM= HAVE_INET6= \
			CBCP= USE_SRP= MAXOCTETS= \
		&& \
		cd plugins/ && \
		$(MAKE) COPTS=\"-Os -fomit-frame-pointer\" \
			SUBDIRS=rp-pppoe PLUGINS= \
	"
endif
	touch $@

install: $(STAMP_DIR)/build $(STAMP_DIR)/build-udeb checkroot
	dh_testdir
	dh_clean -k
	dh_installdirs

	$(MAKE) -f debian/sys-build.mk source.command SOURCE_CMD=" \
		$(MAKE) install DESTDIR=$D MANDIR='$D/usr/share/man' \
	"

	{ echo "Changelog extracted from README file:" ; \
	  echo ; \
	  sed -e "/^What's new in ppp-/,/package, see the Changes-2.3 file./!d" $B/README ; \
	} > $B/changelog-from-README

binary-arch: install
	rm -rf $D/etc/ppp/*-secrets $D/usr/include/

	install -m755 extra/pon extra/plog extra/poff $D/usr/bin/
	cp extra/pon.completion $D/etc/bash_completion.d/pon

	cp extra/options $D/etc/ppp/
	install -m755 extra/ip-up extra/ip-down extra/ipv6-up extra/ipv6-down \
		$D/etc/ppp/
	install -m755 extra/0000usepeerdns-up $D/etc/ppp/ip-up.d/0000usepeerdns
	install -m755 extra/0000usepeerdns-down \
		$D/etc/ppp/ip-down.d/0000usepeerdns
	install -m755 extra/apm $D/etc/apm/event.d/ppp

	install -m644 extra/pap-secrets extra/chap-secrets \
		extra/provider.peer extra/provider.chatscript \
		$D/usr/share/ppp/
	install -m644 extra/chatscript.pap $D/etc/chatscripts/pap

	dh_installchangelogs	-p ppp \
		$B/changelog-from-README Changes-2.3
	dh_installdocs		-p ppp \
		$(addprefix $B/,$(DOCS)) $(addprefix extra/,$(DOCS2))
	dh_installexamples $B/scripts/ extra/autopppd extra/userscripts-* \
		extra/popp extra/filters extra/options.ttyXX \
		extra/peers-pppoe extra/per-linkname
	# use our own version, not the upstream one
	mv $D/usr/share/doc/ppp/examples/autopppd \
		$D/usr/share/doc/ppp/examples/scripts/
	dh_installman extra/pon.1
	dh_link
	dh_installlogrotate
	dh_installinit --init-script=pppd-dns --no-start \
		--update-rcd-params='start 38 S .'
	dh_installinit --no-start \
		--update-rcd-params='start 14 2 3 4 5 . stop 86 0 1 6 .'

	dh_strip		-a
	dh_compress		-a
	dh_fixperms		-a

	chown root:dip $D/usr/sbin/pppd \
		$D/etc/ppp/ $D/etc/ppp/peers/ $D/etc/chatscripts/
	chmod 4754 $D/usr/sbin/pppd
	chmod 2750 $D/etc/ppp/peers/ $D/etc/chatscripts/

	dh_md5sums		-a
	dh_shlibdeps		-a --exclude=pppoatm.so
	dh_installdeb		-a

#	ppp ###############################
	dh_gencontrol		-p ppp
	dh_builddeb		-p ppp

ifdef BUILD_UDEB
#	ppp-udeb ##########################
	dh_installdirs -p ppp-udeb etc/ppp/peers/ usr/sbin/ \
		usr/lib/pppd/2.4.2b3/ usr/lib/prebaseconfig.d/
	grep '^[a-zA-Zo-9]' extra/options > $D/etc/ppp/options
#	cp $B/chat/chat $D-udeb/usr/sbin/
	cp $B/pppd-udeb/pppd $D-udeb/usr/sbin/
	cp $B/pppd-udeb/pppd $D-udeb/usr/sbin/
	cp $B/pppd-udeb/plugins/rp-pppoe/rp-pppoe.so \
		$D-udeb/usr/lib/pppd/2.4.2b3/
	cp extra/prebaseconfig $D-udeb/usr/lib/prebaseconfig.d/60pppoe

	dh_installdeb		-p ppp-udeb
	dh_installdebconf	-p ppp-udeb
#	Don't write your stupid guesses to debian/files.
	dh_gencontrol		-p ppp-udeb -- -fdebian/files~
#	Register file manually.
	dpkg-distaddfile	$(FILENAME) debian-installer $(PRIORITY)
	dh_builddeb		-p ppp-udeb --filename=$(FILENAME)
endif

binary-indep: install
	dh_installchangelogs	-i \
		$B/changelog-from-README Changes-2.3
	dh_installdocs		-i

	cp $(addprefix $B/include/net/,pppio.h *compress.h) \
		$(addprefix $B/pppd/,pppd.h fsm.h lcp.h ipcp.h patchlevel.h) \
		$D-dev/usr/include/net/

	dh_compress		-i
	dh_fixperms		-i

	dh_installdeb		-i
	dh_gencontrol		-i
	dh_builddeb		-i


checkroot:
	test root = "`whoami`"

binary:	binary-arch

.PHONY: binary binary-arch binary-indep unpack configure build clean checkroot
