#!/usr/bin/make -f
#
# John Lapeyre modified the gmp2 rules file
# This is a debian/rules make file. It supports the targets:
# build, clean, binary-indep, binary-arch, and binary
#
# This file was created by Dale Scheetz <dwarf@polaris.net> and modified
# to create "versioned" package names in late November of 1997.
# This file is copyright 1997 by Software in the Public Interest and 
# is thereby licensed under the GPL.

# Package names
#
P=slatec
P2=slatec-dev
INSTALL = /usr/bin/install
#CC := $(shell if [ -x /usr/bin/egcc ]; then echo egcc; else echo cc; fi)

# Create the two so version strings for the symbolic link names
#
command=/usr/lib/dpkg/parsechangelog/debian <debian/changelog
v1 := $(shell $(command) |sed -n '/Version: /s;;;p' |sed -e 's/\..\+//')
v2 := $(shell $(command) |sed -n '/Version: /s;;;p' |sed -e 's/-.\+//')

# Construct the library dependency entry for shlibs
#
shl1=lib$(P)	$(v1)	$(P) (>= $(v2))

# Build the binary components and assemble the libraries
# =====================================================================
build:
	$(checkdir)
	$(MAKE) MA_N=$(v1) MI_N=$(v2) CC=$(CC)
#	$(MAKE) MA_N=$(v1) MI_N=$(v2) $(P)-static
#	$(MAKE) MA_N=$(v1) MI_N=$(v2) $(P)-shared
	touch build

# Clean up after a build and before building a source package
#======================================================================
clean:
	$(checkdir)
	-rm -f build
	-$(MAKE) distclean
	-rm -f `find . -name "*~"`
	-rm -rf debian/tmp debian/*~ *~ *.orig ./#*# *.log debian/files \
	    debian/substvars

# No binary independent components to this package
#======================================================================
binary-indep:
#None:
	@echo >&2 'No independent packages'

# Install package components into debian/tmp to be built into packages
#======================================================================
binary binary-arch:		checkroot build

# First round...package slatec...........................................
#
# Clean out old tmp directory
#
	-rm -rf debian/tmp

# Create new directories in debian/tmp
#
	mkdir debian/tmp debian/tmp/DEBIAN

# Insert appropriate lines into shlibs file
#
	echo -e '$(shl1)\n' >debian/tmp/DEBIAN/shlibs

# Create additional installation directories
#
	$(INSTALL) -d debian/tmp/usr/share/doc/$(P)
#	install -d debian/tmp/usr/{info,lib}
	$(INSTALL) -d debian/tmp/usr/lib


# Install docs in proper directory and gzip them
#
	cp debian/changelog debian/tmp/usr/share/doc/$(P)/changelog.Debian
#	cp ChangeLog debian/tmp/usr/share/doc/$(P)/changelog
	$(INSTALL) -m 644 debian/README.debian debian/tmp/usr/share/doc/$(P)
	$(INSTALL) -m 644 readme debian/tmp/usr/share/doc/$(P)/README
	gzip -9v debian/tmp/usr/share/doc/$(P)/*

# but don't gzip the copyright statement
#
	cp debian/copyright debian/tmp/usr/share/doc/$(P)/copyright

# Install installation scripts
#
	cp debian/postinst.slatec debian/tmp/DEBIAN/postinst
	cp debian/prerm.slatec debian/tmp/DEBIAN/prerm
	chmod +x debian/tmp/DEBIAN/postinst debian/tmp/DEBIAN/prerm

# Install shared library
#
	strip --strip-unneeded lib$(P).so.$(v2)
	$(INSTALL) -m 644 lib$(P).so.$(v2) debian/tmp/usr/lib/.

# Create .so link
#
	ln -s lib$(P).so.$(v2) debian/tmp/usr/lib/lib$(P).so.$(v1)

# Create dependency information
#
	dpkg-shlibdeps ./lib$(P).so.$(v2)



# Generate control file for $(P)
#
	dpkg-gencontrol -p$(P) -isp

# Clean up file ownership
#
	chown -R root.root debian/tmp

# and permissions
#
	chmod -R g-ws debian/tmp

# and build the package
#
	dpkg --build debian/tmp ..	

# Second round...package slatec-dev......................................
#
# Clean out old tmp directory
#
	-rm -rf debian/tmp

# Create new directories in debian/tmp
#
	mkdir debian/tmp debian/tmp/DEBIAN

# Create additional installation directories
#
	$(INSTALL) -d debian/tmp/usr/share/doc/$(P2)
#	$(INSTALL) -d debian/tmp/usr/{include,lib}
	$(INSTALL) -d debian/tmp/usr/lib
#	$(INSTALL) -d debian/tmp/usr/include/$(P)

# Install docs in proper directory and gzip them
#
#	$(INSTALL) -m 644 readme install debian/tmp/usr/share/doc/$(P2)/.
	$(INSTALL) -m 644 debian/README.debian debian/tmp/usr/share/doc/$(P2)
	cp debian/changelog debian/tmp/usr/share/doc/$(P2)/changelog.Debian
#	cp ChangeLog debian/tmp/usr/share/doc/$(P2)/changelog

	# Install package docs
	$(INSTALL) -d debian/tmp/usr/share/doc/$(P2)/
	$(INSTALL) -m 644 install debian/tmp/usr/share/doc/$(P2)/INSTALL
	$(INSTALL) -m 644 readme debian/tmp/usr/share/doc/$(P2)/README
	$(INSTALL) -m 644 doc/* debian/tmp/usr/share/doc/$(P2)/
	gzip -9fv debian/tmp/usr/share/doc/$(P2)/*

# but don't gzip the copyright statement
#
	cp debian/copyright debian/tmp/usr/share/doc/$(P2)/copyright

# Install test files
	$(INSTALL) -d  debian/tmp/usr/share/doc/$(P2)/tests
	$(INSTALL) -m 644 src/chk/*.f src/chk/Makefile src/chk/README.tests  debian/tmp/usr/share/doc/$(P2)/tests
	$(INSTALL) -m 755 src/chk/dotests debian/tmp/usr/share/doc/$(P2)/tests
	gzip -9vf debian/tmp/usr/share/doc/$(P2)/tests/*

# Install installation scripts
#
	cp debian/postinst.slatec-dev debian/tmp/DEBIAN/postinst
	cp debian/prerm.slatec-dev debian/tmp/DEBIAN/prerm
	chmod +x debian/tmp/DEBIAN/postinst debian/tmp/DEBIAN/prerm

# Install header files
#
#	$(INSTALL) -m 644 gmp.h mp.h debian/tmp/usr/include/$(P)/.
#	$(INSTALL) -m 644 mpn/generic/gmp-mparam.h debian/tmp/usr/include/$(P)/.

# Install static library
#
	strip --strip-debug lib$(P).a
	$(INSTALL) -m 644 lib$(P).a debian/tmp/usr/lib/.

# Create .so link
#
	ln -s lib$(P).so.$(v2) debian/tmp/usr/lib/lib$(P).so

# Generate control file for slatec-dev
#
	dpkg-gencontrol -p$(P2) -isp

# Clean up file ownership
#
	chown -R root.root debian/tmp

# and permissions
#
	chmod -R g-ws debian/tmp

# and build the package
#
	dpkg --build debian/tmp ..	



define checkdir
	test -f src/machine/i1mach.c
endef

# Below here is fairly generic really
checkroot:
	$(checkdir)
	test root = "`whoami`"

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