#! /usr/bin/make -f
## debian/rules for LPE
## (original by Gergely Nagy <algernon@debian.org> but now all changed :)

PACKAGE = lpe
PKGDIR	= ${CURDIR}/debian/${PACKAGE}

CFLAGS	= -O2
ifneq (,$(findstring debug,${DEB_BUILD_OPTIONS}))
	CFLAGS += -g
endif

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

default: build

config: config-stamp
config-stamp:
	$(testdir)
	CFLAGS="${CFLAGS}" ./configure  --prefix=/usr \
					--mandir=/usr/share/man \
					--infodir=/usr/share/info \
					--sysconfdir=/etc \
					--build=${DEB_BUILD_GNU_TYPE} \
					--host=${DEB_HOST_GNU_TYPE}
	touch config-stamp

build: config 
	$(testdir)
	$(MAKE)

clean: clean1
	rm -rf debian/${PACKAGE}.substvars ${PKGDIR} debian/files \
	       config-stamp patch-stamp bootstrap-stamp \
	       debian/patched config.cache autom4te.cache \
	       po/*.gmo data/conv.slc data/init.slc po/lpe.pot

clean1:
	$(testdir)
#	$(testroot)
	-$(MAKE) distclean

install: build
	$(testdir)
	$(testroot)
	rm -rf debian/files debian/${PACKAGE}.substvars ${PKGDIR}
## Install the directories
	install -d ${PKGDIR}
	$(MAKE) prefix=${PKGDIR}/usr mandir=\$${prefix}/share/man install
	rm -f ${PKGDIR}/usr/share/doc/${PACKAGE}/COPYING \
	      ${PKGDIR}/usr/share/doc/${PACKAGE}/NEWS

lpe: build install
	$(testdir)
	$(testroot)
## Install various files
# directories
#	install -d ${PKGDIR}/usr/lib/menu
# menu entry
#	install -m 0644 debian/local/${PACKAGE}.menu \
#		${PKGDIR}/usr/lib/menu/${PACKAGE}
# changelogs
	install -m 0644 NEWS \
		${PKGDIR}/usr/share/doc/${PACKAGE}/changelog
	install -m 0644 debian/changelog \
		${PKGDIR}/usr/share/doc/${PACKAGE}/changelog.Debian
# copyright
	install -m 0644 debian/copyright \
		${PKGDIR}/usr/share/doc/${PACKAGE}/copyright
# strip binaries
ifeq (,$(findstring nostrip,${DEB_BUILD_OPTIONS}))
	strip --remove-section=.comment --remove-section=.note $(find-binaries)
endif
# compress stuff
	find ${PKGDIR}/usr/share/man/ -type f | xargs gzip -9f
	gzip -9f ${PKGDIR}/usr/share/doc/${PACKAGE}/changelog \
		 ${PKGDIR}/usr/share/doc/${PACKAGE}/changelog.Debian \
		 ${PKGDIR}/usr/share/doc/${PACKAGE}/BUGS \
		 ${PKGDIR}/usr/share/doc/${PACKAGE}/CUSTOMIZE \
		 ${PKGDIR}/usr/share/doc/${PACKAGE}/IDEAS \
		 ${PKGDIR}/usr/share/doc/${PACKAGE}/MODES \
		 ${PKGDIR}/usr/share/doc/${PACKAGE}/README \
		 ${PKGDIR}/usr/share/doc/${PACKAGE}/TODO
# fix some permissions
	chmod 0644 ${PKGDIR}/usr/lib/${PACKAGE}/*
# install maintainer scripts into DEBIAN/
	install -d ${PKGDIR}/DEBIAN
	install -m 0755 debian/maint/postinst \
			debian/maint/prerm \
			${PKGDIR}/DEBIAN
## Generate ${shlibs:Depends}
	dpkg-shlibdeps -Tdebian/${PACKAGE}.substvars -dDepends $(find-binaries)
## Generate DEBIAN/control
	dpkg-gencontrol -isp -p${PACKAGE} -Tdebian/${PACKAGE}.substvars -P${PKGDIR}
## Generate DEBIAN/md5sums
	cd ${PKGDIR} >/dev/null ;\
	 find * -type f ! -regex '^DEBIAN/.*' -print0 | xargs -r0 md5sum > DEBIAN/md5sums
## Build the binary package
	dpkg --build ${PKGDIR} ..

binary-indep:
binary-arch: lpe
binary: binary-indep binary-arch


## Some handy macros
define testdir
	test -e debian/control || ( echo "debian/control not found"; exit 1 )
endef

define testroot
	test "x`whoami`" = "xroot" || ( echo "You must run this as root!"; exit 1 )
endef

define find-binaries
	$(shell find ${PKGDIR} -type f \( -name '*.so' -or -path '*/bin/lpe' \) -print)
endef

.PHONY: build clean binary-indep binary-arch binary install unpatch \
	clean1 patch config lpe bootstrap default
