#!/usr/bin/make -f 
# Made with the aid of debmake, by Christoph Lameter,
# based on the sample debian/rules file for GNU hello by Ian Jackson.

package=gsl

version=$(shell expr `pwd` : '.*-\([0-9.]*\)')
version_major=$(shell expr `pwd` : '.*-\([0-9]*\).[0-9.]*')

build:
	$(checkdir)
	-mkdir shared
	./configure --prefix=/usr --includedir=/usr/include/gsl
	$(MAKE) CFLAGS="-O2 -fPIC -pipe"
	cd shared && ar -x ../libgsl.a && \
	gcc -shared -Wl,-soname,lib$(package).so.$(version_major) -o lib$(package).so.$(version) `ls .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) clean && $(MAKE) CFLAGS="-O2 -pipe" LDFLAGS="-s"
	touch build

clean:
	$(checkdir)
	-rm -rf static shared
	-rm -f build
	-make distclean
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp `find debian/* -type d ! -name CVS` debian/files* core
	-rm -f debian/*substvars

binary-indep: checkroot build
	$(checkdir)
# There are no architecture-independent files to be uploaded
# generated by this package.  If there were any they would be
# made here.

binary-arch: checkroot build
	$(checkdir)
	-rm -rf debian/tmp `find debian/* -type d`
	install -d debian/tmp        
	install -d debian/tmp/bin/DEBIAN debian/tmp/dev/DEBIAN
	install -d debian/tmp/dev/usr/doc
	ln -s $(package) debian/tmp/dev/usr/doc/$(package)-dev
#	cp debian/{postinst,prerm} debian/tmp/bin/DEBIAN/.
#	chmod +x debian/tmp/bin/DEBIAN/{postinst,prerm}
	#
	cd debian/tmp/bin && install -d `cat ../../dirs`
	#
	make install prefix=`pwd`/debian/tmp/usr includedir=`pwd`/debian/tmp/usr/include/gsl
	# now take out some extra stuff
	rm -f `pwd`/debian/tmp/usr/lib/*.a
	cd `pwd`/debian/tmp/usr/bin && rm -f test-switch
	install -m644 lib$(package).a debian/tmp/bin/usr/lib/
	# not working
#	install -m644 shared/lib$(package).so.$(version) debian/tmp/bin/usr/lib
	#
#	debstd -m ChangeLog NEWS README TODO
	debstd -m -u ChangeLog NEWS README TODO
	# now move things around to split out gsl-dev and gsl
	install -d debian/tmp/bin/usr/doc
	mv debian/tmp/usr/doc/$(package) debian/tmp/bin/usr/doc
	mv debian/tmp/usr/bin debian/tmp/bin/usr
	install -d debian/tmp/dev/usr/lib debian/tmp/dev/usr/info
	mv debian/tmp/usr/info/gsl* debian/tmp/dev/usr/info/
	mv debian/tmp/usr/include debian/tmp/dev/usr/.
	if [ -d debian/tmp/usr/man/man1 ]; then \
		mv debian/tmp/usr/man/man1 debian/tmp/bin/usr/man/.; \
	fi
	mv debian/tmp/bin/usr/lib/*.a debian/tmp/dev/usr/lib/.
	#mv debian/tmp/bin/usr/lib/*.so debian/tmp/dev/usr/lib/.
	rm -f debian/tmp/bin/usr/lib/*.so
	#mv debian/tmp/bin/usr/info/gsl* debian/tmp/dev/usr/info/.
	# We can strip shared libraries
#	strip --strip-unneeded debian/tmp/bin/usr/lib/libgsl*.so*
	#
	dpkg-gencontrol -pgsl-dev -Pdebian/tmp/dev
	dpkg-gencontrol -pgsl -Pdebian/tmp/bin
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp/bin/
	chmod -R go=rX debian/tmp/dev/
	dpkg --build debian/tmp/dev ..
	dpkg --build debian/tmp/bin ..

define checkdir
	test -f debian/rules
endef

binary: binary-indep binary-arch

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

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

