#!/usr/bin/make -f
# Based on Ian Jackson's sample debian.rules file - for GNU Hello (1.3).
# Modified for readline by Ray Dassen <jdassen@wi.LeidenUniv.nl>.

package=libreadline
version=2.0
debian=4

# The next section may have to be extensively modified

build: Makefile
	#
	# First build the shared libraries
	#
	$(MAKE) "CFLAGS=-O2 -fPIC -I/usr/include/ncurses" 
	gcc -shared -Wl,-soname,libhistory.so.$(version) -o libhistory.so history.o
# Note that we simply include history.o here
	gcc -shared -Wl,-soname,libreadline.so.$(version) -o libreadline.so.$(version) `ls *.o` 
	#
	# Clean up (to prepare for building static lib)
	# We cannot do 'make clean' since this would remove the shared libs too
	#
	rm -f *.o
	#
	# Build the static library (it does not need Position Independent Code,
	# which reserves one register; thus without -fPIC we get more efficient
	# code).
	#
	$(MAKE) "CFLAGS=-O2 -fomit-frame-pointer -I/usr/include/ncurses" LDFLAGS=-s
	touch build

clean:
	$(checkdir)
	-rm -f build
	-$(MAKE) -i distclean
	-rm -rf debian-tmp *~ *.bak

binary:		checkroot build
	-rm -rf debian-tmp
	mkdir debian-tmp debian-tmp/DEBIAN
	install -d debian-tmp/usr/doc/copyright
	sed -e '2s/=/$(version)/; 3s/=/$(debian)/' \
		debian.control > debian-tmp/DEBIAN/control
	cp debian.postinst debian-tmp/DEBIAN/postinst
	cp debian.postrm debian-tmp/DEBIAN/postrm
	chmod +x debian-tmp/DEBIAN/{postinst,postrm}
	install -d debian-tmp/usr/man/man3
	isntall -c -m 644 doc/readline.3 debian-tmp/usr/man/man3/readline.3readline
	install -d debian-tmp/usr/info
	install -c -m 644 doc/history.info doc/readline.info debian-tmp/usr/info
	gzip -9v debian-tmp/usr/info/*
	install -d debian-tmp/usr/lib
	install -d debian-tmp/usr/include
	install -c -m 644 readline.h chardefs.h keymaps.h history.h debian-tmp/usr/include
	install -c -m 644 libreadline.a libhistory.a debian-tmp/usr/lib
	install -c -m 644 libhistory.so debian-tmp/usr/lib
	install -c -m 644 libreadline.so.$(version) debian-tmp/usr/lib
	cd debian-tmp/usr/lib ; ln -s libreadline.so.$(version) libreadline.so
	cp debian.README debian-tmp/usr/doc/copyright/$(package)
	chown -R root.root debian-tmp
	chmod -R g-ws debian-tmp
	dpkg --build debian-tmp
	mv debian-tmp.deb ../$(package)-$(version)-$(debian).deb

define checkdir
	test -f readline.c
endef

Makefile:
	$(checkdir)
	./configure -v --prefix=/usr
	

# Below here is fairly generic really

source:		clean
	chmod +x debian.rules
	cd .. && \
	tar cf $(package)-$(version)-$(debian).tar $(package)-$(version) && \
	gzip -9vf $(package)-$(version)-$(debian).tar
 
diff:		clean
	cd .. && \
	(diff -ruN $(package)-$(version).orig $(package)-$(version) \
	 >$(package)-$(version)-$(debian).diff; [ $$? = 1 ]) && \
	gzip -9vf $(package)-$(version)-$(debian).diff

checkroot:
	$(checkdir)
	test root = "`whoami`"

.PHONY: binary source diff clean checkroot
