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

#BUILD_UDEB := 1

include debian/scripts/vars

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

PPPDDIR := 2.4.4

# 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  := FAQ README SETUP PLUGINS README.MPPE README.MSCHAP8? README.cbcp \
	README.pppoe README.pwfd
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
	NOISY=1 \
	$(MAKE) -f debian/sys-build.mk source.command SOURCE_CMD=" \
		./configure --prefix=/usr \
	"
ifdef BUILD_UDEB
	cd $B && if [ ! -d pppd-udeb/ ]; then \
		mkdir pppd-udeb/ && \
		cd pppd-udeb/ && \
		sh $(CURDIR)/extra/lndir.sh ../pppd; \
	fi
endif
	touch $@


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

udeb: build-udeb
build-udeb: $(STAMP_DIR)/build-udeb
$(STAMP_DIR)/build-udeb: $(STAMP_DIR)/configure
ifdef BUILD_UDEB
	dh_testdir
	NOISY=1 \
	$(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= USE_BUILTIN_CRYPTO=1 \
		&& \
		cd plugins/ && \
		$(MAKE) COPTS=\"-Os -fomit-frame-pointer\" \
			SUBDIRS='rp-pppoe pppoatm' PLUGINS= \
	"
endif
	touch $@

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

	NOISY=1 \
	$(MAKE) -f debian/sys-build.mk source.command SOURCE_CMD=" \
		$(MAKE) install DESTDIR=$D/usr/ \
	"

	{ 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
	rm -rf $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-* extra/interfaces 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 extra/pppoe-discovery.8
	dh_link
	dh_installlogrotate
#	dh_installinit --init-script=pppd-dns --no-start \
#		--update-rcd-params='start 38 S .'

ifdef BUILD_UDEB
	dh_installdirs -p ppp-udeb etc/ppp/peers/ usr/sbin/ \
		usr/lib/pppd/$(PPPDDIR)/ usr/lib/post-base-installer.d/
	grep '^[a-zA-Z0-9]' extra/options > $D-udeb/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 $B/pppd-udeb/plugins/rp-pppoe/pppoe-discovery \
		$D-udeb/usr/sbin/
	cp $B/pppd-udeb/plugins/rp-pppoe/rp-pppoe.so \
		$B/pppd-udeb/plugins/pppoatm/pppoatm.so \
		$D-udeb/usr/lib/pppd/$(PPPDDIR)/
	cp extra/ppp-udeb.ip-up \
		$D-udeb/etc/ppp/ip-up
	chmod 0755 $D-udeb/etc/ppp/ip-up
	install -m755 extra/ppp-udeb-postbaseinst \
		$D-udeb/usr/lib/post-base-installer.d/30ppp
	dh_installdebconf
endif

	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
	dh_installdeb		-a

	dh_gencontrol		-a
	dh_builddeb		-a

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

	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
