#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. GNU copyright 1997 by Joey Hess.
# merged with old ppp makefile. GNU copyright 1998 by Philip Hands.

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

#
# dbs stuff.
#
include debian/scripts/vars
BUILD_DIR=$(SOURCE_DIR)/$(TAR_DIR)

# package build dir.
tmp = $(CURDIR)/debian/tmp
tmp-udeb = $(CURDIR)/debian/ppp-udeb

#
# Various collections of files for use with dh_* tools.
#
DOCS=$(BUILD_DIR)/README.linux $(BUILD_DIR)/FAQ $(BUILD_DIR)/README \
	$(BUILD_DIR)/SETUP $(BUILD_DIR)/PLUGINS \
	$(BUILD_DIR)/README.MSCHAP80 $(BUILD_DIR)/README.cbcp \
	debian/README.STATIC-IP debian/README.win9x debian/kernel.fix-2.0.30-2
EXAMPLES=$(BUILD_DIR)/scripts/ $(BUILD_DIR)/etc.ppp/chap-secrets
CHANGELOGS=debian/changelog-from-README $(BUILD_DIR)/Changes-2.3


#
# Compile source.
#
build: $(STAMP_DIR)/build
$(STAMP_DIR)/build: $(STAMP_DIR)/source.make
	dh_testdir
	# have to do make clean since udeb and deb build in same area
	-(cd $(BUILD_DIR) && make clean)
	(cd $(BUILD_DIR) && ./configure)
	$(MAKE) -C $(BUILD_DIR) USE_PAM=1 COPTS="-DLOG_PPP=LOG_LOCAL2"
	touch $@

build-udeb: $(STAMP_DIR)/build-udeb
$(STAMP_DIR)/build-udeb: source.make
	dh_testdir
	# udeb clean: have to do make clean since udeb and deb build in same area
	-(cd $(BUILD_DIR) && make clean)
	(cd $(BUILD_DIR) && ./configure)
	$(MAKE) -C $(BUILD_DIR) COPTS="-DLOG_PPP=LOG_LOCAL2" UDEB_CFLAGS="-Os -fomit-frame-pointer -UPPP_FILTER -UPLUGIN -UHAVE_MULTILINK" 
	touch $@


#
# Unpack upstream source, and apply local patches
#
source.make: $(STAMP_DIR)/source.make
$(STAMP_DIR)/source.make:
	$(MAKE) -f debian/sys-build.mk source.make
	touch $@

# Various source manipulation
#
source.unpack: clean
	$(MAKE) -f debian/sys-build.mk source.build

source.diff:
	$(MAKE) -f debian/sys-build.mk make-diff

#
# Remove uppacked source, and clean up debian/
#
clean:
	dh_testdir
	$(MAKE) -f debian/sys-build.mk source.clean
	rm -rf $(STAMP_DIR)
	dh_clean debian/changelog-from-README


binary-indep:
# We have nothing to do, no architecture-independent packages here.


#
# Build the package!
#
binary-arch: ppp ppp-udeb
ppp: build
	# dh_testversion
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C $(BUILD_DIR) install DESTDIR=$(tmp) MANDIR='$$(DESTDIR)/usr/share/man'

	cd $(tmp)/etc/ppp && rm -f pap-secrets chap-secrets

	cp $(BUILD_DIR)/include/net/pppio.h $(BUILD_DIR)/include/net/*compress.h $(tmp)/usr/include/net
	cd debian && install -m644 options options.ttyXX no_ppp_on_boot $(tmp)/etc/ppp
	cd debian && install -m755 ip-up ip-down $(tmp)/etc/ppp
	cd debian && install -m755 pon plog poff $(tmp)/usr/bin
	cd debian && install -m644 pon.1 $(tmp)/usr/share/man/man1
	cd $(tmp)/usr/share/man/man1 && ln -s pon.1 poff.1 && ln -s pon.1 plog.1

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

	dh_installdocs -p$@ $(DOCS)
	dh_installexamples $(EXAMPLES)
	dh_installchangelogs -p$@ $(CHANGELOGS)
	dh_installmodules -p$@
	dh_installinit --no-restart-on-upgrade --update-rcd-params='start 14 2 3 4 5 . stop 14 0 1 6 .'
	cp $(BUILD_DIR)/pppd/ppp.pam $(tmp)/etc/pam.d/ppp
	dh_installdebconf
	dh_strip
	dh_compress
	dh_fixperms
	chmod -R g-ws $(tmp)
	# Special permissions
	chown root.dip $(tmp)/usr/sbin/pppd
	chmod 4754 $(tmp)/usr/sbin/pppd
	chown root.dip $(tmp)/etc/ppp $(tmp)/etc/ppp/peers $(tmp)/etc/chatscripts
	chmod 750 $(tmp)/etc/ppp
	chmod 2750 $(tmp)/etc/ppp/peers $(tmp)/etc/chatscripts
	dh_shlibdeps
	dh_gencontrol -p$@
	dh_installdeb
	dh_md5sums
	dh_builddeb -p$@

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 | cut -d ' ' -f 2)


ppp-udeb: build-udeb
	mkdir -p ./debian/$@
	$(MAKE) -C $(BUILD_DIR) install DESTDIR=$(tmp-udeb)
	rm -rf $(tmp-udeb)/usr/man/
	rm -rf $(tmp-udeb)/usr/lib/
	rm -rf $(tmp-udeb)/usr/sbin/pppdump
	rm -rf $(tmp-udeb)/usr/sbin/pppstats
	@echo "Building $@"
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_strip                -p$@
	dh_compress             -p$@
	dh_fixperms             -p$@
	dh_installdeb           -p$@
	dh_shlibdeps            -p$@
	# Don't write your stupid guesses to debian/files.
	dh_gencontrol           -p$@ -- -fdebian/files~
	# Register file manually.
	dpkg-distaddfile        $(FILENAME) debian-installer $(PRIORITY)
	dh_md5sums              -p$@
	dh_builddeb             -p$@ --filename=$(FILENAME)

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





