#! /usr/bin/make -f
## debian/rules for libedit, mostly a pmake workaround..
## hacked together by Gergely Nagy <algernon@debian.org>
## $MadHouse: Debian/libedit/debian/rules,v 1.3 2001/11/04 01:57:26 algernon Exp $

# This is the debhelper compatability version to use.
export DH_COMPAT = 2
PACKAGE		 = libedit
PKGDIR		 = ${CURDIR}/debian/tmp
VERSION		 = 20050518
PMAKE_ARGS	 = MKPROFILE=no MKCATPAGES=no MLINKS= MANPAGES= \
		 SHLIB_SHFLAGS="-Wl,-soname,libedit.so.${major}" LDADD="*.so -lcurses"
EDITLINE_3_LINKS = el_init el_end el_reset el_gets el_getc el_push \
		 el_parse el_set el_get el_source el_resize el_line \
		 el_insertstr el_deletestr history_init history_end history

version		 = 2.9
major		 = 2
## For automatic update from CVS
CVSROOT		 = :pserver:anoncvs@anoncvs.netbsd.org:/cvsroot
DATE		 = $(shell date +%Y%m%d)
ROOT		 = basesrc
MISC_STUFF	 = lib/libc/include/namespace.h lib/libc/string/strlcat.c \
		 lib/libc/string/strlcpy.c lib/libc/gen/vis.c \
		 lib/libc/gen/unvis.c include/vis.h

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

# Workaround for pmake fussiness
ifeq (mips-linux,$(DEB_HOST_GNU_TYPE))
  PMAKE_ARGS += MACHINE_ARCH=mipseb
endif

build: patch build-stamp
build-stamp:
	dh_testdir
	chmod 755 libedit-20050518/makelist	# workaround a bug in upstream source
	cd libedit-${VERSION} && pmake ${PMAKE_ARGS}
	touch build-stamp

clean: clean1 unpatch
	rm -rf debian/patched build-stamp
clean1:
	dh_testdir
	dh_testroot
	dh_clean
	-cd libedit-${VERSION} && pmake distclean

install: build
	dh_testdir
	dh_testroot
	dh_clean
	install -d ${PKGDIR}/usr/lib
	install -d ${PKGDIR}/usr/include/readline
	install -d ${PKGDIR}/usr/share/man/man3
	install -d ${PKGDIR}/usr/share/man/man5
	cd libedit-${VERSION} && \
	 pmake install incinstall DESTDIR=${PKGDIR} ${PMAKE_ARGS}
## pmake workaround
	cd libedit-${VERSION}/readline && \
	 pmake incinstall DESTDIR=${PKGDIR} ${PMAKE_ARGS}
	mv ${PKGDIR}/usr/include/readline ${PKGDIR}/usr/include/editline
	ln -sf libedit.so.${version} ${PKGDIR}/usr/lib/libedit.so.${major}
	ln -sf libedit.so.${major} ${PKGDIR}/usr/lib/libedit.so

binary-indep: build install
binary-arch: build install
	dh_testdir
	dh_testroot
## Install manpages
	install -m 0644 libedit-${VERSION}/editline.3 ${PKGDIR}/usr/share/man/man3/editline.3el
	install -m 0644 libedit-${VERSION}/editrc.5 ${PKGDIR}/usr/share/man/man5/editrc.5el
## End
	dh_movefiles
	@list="${EDITLINE_3_LINKS}"; for l in $$list; do \
		echo "ln -sf editline.3el ${CURDIR}/debian/libedit-dev/usr/share/man/man3/$$l.3el"; \
		ln -sf editline.3el ${CURDIR}/debian/libedit-dev/usr/share/man/man3/$$l.3el; \
	done
	dh_installdocs
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

patch: ${CURDIR}/patch-stamp
${CURDIR}/patch-stamp:
	test -d debian/patched || install -d debian/patched
	echo "Patches applied in the Debian version of ${PACKAGE}:" > $@T
	@cd libedit-${VERSION} && \
	for patch in `cat ../debian/patches/00list`; do \
		stamp=../debian/patched/$$patch.dpatch; \
		patch=../debian/patches/$$patch.dpatch; \
		test -x $$patch || chmod +x $$patch; \
		if test -f $$stamp; then \
			echo "$$patch already applied."; \
			echo -e "\n$$patch:" >> $@T; \
			sed -n 's/^## *DP: */  /p' $$patch >> $@T; \
		else \
			echo -n "applying patch $$patch..."; \
			if $$patch -patch >$$stamp.new 2>&1; then \
				mv $$stamp.new $$stamp; \
				touch $$stamp; \
				echo -e "\n$$patch:" >> $@T; \
				sed -n 's/^## *DP: */  /p' $$patch >> $@T; \
				echo " ok."; \
			else \
				echo " failed."; \
				exit 1; \
			fi; \
		fi; \
	done
	mv -f $@T $@

unpatch:
	@cd libedit-${VERSION} && \
	for patch in `sort -r ../debian/patches/00list`; do \
		stamp=../debian/patched/$$patch.dpatch; \
		patch=../debian/patches/$$patch.dpatch; \
		test -x $$patch || chmod +x $$patch; \
		if test -f $$stamp; then \
			echo -n "reverting patch $$patch..."; \
			if $$patch -unpatch 2>&1 >/dev/null; then \
				rm -f $$stamp; \
				echo " ok."; \
			else \
				echo " failed."; \
				exit 1; \
			fi; \
		fi; \
	done
	rm -f patch-stamp

binary: binary-indep binary-arch

cvs-update:
	install -d glibc-compat/tmp
	cd glibc-compat/tmp && \
	 cvs -d ${CVSROOT} export -D now $(patsubst %,${ROOT}/%,${MISC_STUFF})
	find glibc-compat/tmp -type f -exec mv {} glibc-compat/ \;
	rm -rf glibc-compat/tmp
	cvs -d ${CVSROOT} export -D now -d libedit-${DATE} ${ROOT}/lib/libedit

.PHONY: build clean binary-indep binary-arch binary install \
	patch unpatch clean1 cvs-update
