#!/usr/bin/make -f
#
# 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 name variables
#
P=gmp3
L=gmp
L2=mp
SO=3
SO1=3.2.1
SO2=3.1.3
P2=$(P)-dev
P3=$(P)-doc
arch=$(shell dpkg --print-architecture)
CNFL=--enable-mpbsd --build=`dpkg-architecture -qDEB_BUILD_GNU_TYPE`

# Create CFLAGS content variable CFCT
#
ifneq ($(shell dpkg-architecture -qDEB_HOST_GNU_CPU),m68k)
       CFCT="-O3"
else
       # work around gcc bug in m68k
       CFCT="-O3 -fno-force-mem"
endif

# 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$(L)	$(v1)	lib$(P) (>= $(v2))
#
shl1=lib$(L)	$(SO)	lib$(P) (>= $(v2))
shl2=lib$(L2)	$(SO)	lib$(P) (>= $(v2))

# Build the binary components and assemble the libraries
# =====================================================================
build:
	$(checkdir)
	./configure --prefix=/usr $(CNFL)
	$(MAKE) CFLAGS=$(CFCT)
	mkdir -p ./hold
	$(MAKE) prefix=`pwd`/hold install
	touch build

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

binary: binary-arch binary-indep

# build -doc as independent components to this package
#======================================================================
binary-indep:	checkroot

# package libgmp3-doc
#
# Clean out old tmp directory
#
	-rm -rf debian/tmp

# Create new directories in debian/tmp
#
	install -d debian/tmp debian/tmp/DEBIAN

# Install installation scripts
#
	install -m 755 debian/postinst.doc debian/tmp/DEBIAN/postinst
	install -m 755 debian/prerm.doc debian/tmp/DEBIAN/prerm

# Create additional installation directories
#
	install -d debian/tmp/usr/share/doc/lib$(P3)
	install -d debian/tmp/usr/share/doc/lib$(P3)/demos
	install -d debian/tmp/usr/share/doc/lib$(P3)/docs
	install -d debian/tmp/usr/share/doc/lib$(P3)/demos/calc
	install -d debian/tmp/usr/share/doc/lib$(P3)/demos/expr
	install -d debian/tmp/usr/share/doc/lib$(P3)/demos/perl
	install -d debian/tmp/usr/share/doc/lib$(P3)/demos/perl/GMP
	install -d debian/tmp/usr/share/info

# Install docs in proper directory and gzip them
#
	cp debian/changelog debian/tmp/usr/share/doc/lib$(P3)/changelog.Debian
	cp ChangeLog debian/tmp/usr/share/doc/lib$(P3)/changelog
	gzip -9v debian/tmp/usr/share/doc/lib$(P3)/change*
	install -m 644 ./doc/* debian/tmp/usr/share/doc/lib$(P3)/docs/.
	install -m 644 ./demos/*.c debian/tmp/usr/share/doc/lib$(P3)/demos/.
	install -m 644 ./demos/M* debian/tmp/usr/share/doc/lib$(P3)/demos/.
	install -m 644 ./demos/calc/* debian/tmp/usr/share/doc/lib$(P3)/demos/calc/.
	install -m 644 ./demos/expr/* debian/tmp/usr/share/doc/lib$(P3)/demos/expr/.
	-install -m 644 ./demos/perl/* debian/tmp/usr/share/doc/lib$(P3)/demos/perl/.
	install -m 644 ./demos/perl/GMP/* debian/tmp/usr/share/doc/lib$(P3)/demos/perl/GMP/.

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

# Install and gzip the info pages
#
	install -m 644 $(L).info* debian/tmp/usr/share/info/.
	gzip -9v debian/tmp/usr/share/info/*

# Generate control file for libgmp3
#
	dpkg-gencontrol -plib$(P3) -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 ..	


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

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

# Create new directories in debian/tmp
#
	install -d debian/tmp debian/tmp/DEBIAN

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

# Create additional installation directories
#
	install -d debian/tmp/usr/share/doc/lib$(P)
	install -d debian/tmp/usr/lib

# Install installation scripts
#
	install -m 755 debian/postinst debian/tmp/DEBIAN/.
	install -m 755 debian/prerm debian/tmp/DEBIAN/.

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

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

# Install shared libraries
#
	strip --strip-unneeded -R .note -R .comment ./hold/lib/lib$(L).so.$(SO1)
	install -m 644 ./hold/lib/lib$(L).so.$(SO1) \
		debian/tmp/usr/lib/lib$(L).so.$(SO1)
	strip --strip-unneeded -R .note -R .comment ./hold/lib/lib$(L2).so.$(SO2)
	install -m 644 ./hold/lib/lib$(L2).so.$(SO2) \
	        debian/tmp/usr/lib/lib$(L2).so.$(SO2)

# Create compatibility .so links
#
#	ln -s lib$(L).so.$(SO1) debian/tmp/usr/lib/lib$(P).so.$(SO)

# Create real .so link
#
	ln -s lib$(L).so.$(SO1) debian/tmp/usr/lib/lib$(L).so.$(SO)
	ln -s lib$(L2).so.$(SO2) debian/tmp/usr/lib/lib$(L2).so.$(SO)

# Create dependency information
#
	dpkg-shlibdeps ./hold/lib/lib$(L).so.$(SO1)

# Generate control file for libgmp3
#
	dpkg-gencontrol -plib$(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 libgmp3-dev......................................
#
# Clean out old tmp directory
#
	-rm -rf debian/tmp

# Create new directories in debian/tmp
#
	install -d debian/tmp debian/tmp/DEBIAN

# Install installation scripts
#
	install -m 755 debian/postinst.dev debian/tmp/DEBIAN/postinst
	install -m 755 debian/prerm.dev debian/tmp/DEBIAN/prerm

# Create additional installation directories
#
	install -d debian/tmp/usr/share/doc/lib$(P2)
	install -d debian/tmp/usr/lib
	install -d debian/tmp/usr/include

# Install docs in proper directory and gzip them
#
	install -m 644 README debian/tmp/usr/share/doc/lib$(P2)/.
	cp debian/changelog debian/tmp/usr/share/doc/lib$(P2)/changelog.Debian
	cp ChangeLog debian/tmp/usr/share/doc/lib$(P2)/changelog
	cp debian/README.headers debian/tmp/usr/share/doc/lib$(P2)
	gzip -9v debian/tmp/usr/share/doc/lib$(P2)/change*

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

# Install header files
#
	install -m 644 *.h debian/tmp/usr/include/.
	rm -f debian/tmp/usr/include/*config.h
	rm -f debian/tmp/usr/include/gmp-impl.h

# Install static libraries
#
#	strip --strip-debug ./.libs/lib$(L).a
	install -m 644 ./hold/lib/lib$(L).a debian/tmp/usr/lib/.
	install -m 644 ./hold/lib/lib$(L).la debian/tmp/usr/lib/.
	install -m 644 ./hold/lib/lib$(L2).a debian/tmp/usr/lib/.
	install -m 644 ./hold/lib/lib$(L2).la debian/tmp/usr/lib/.
#
# Create old link
#
#	ln -s ./lib$(L).a debian/tmp/usr/lib/lib$(P).a
#	ln -s ./lib$(L).la debian/tmp/usr/lib/lib$(P).la
#
# Create Generic links
#
	ln -s lib$(L).so.$(SO1) debian/tmp/usr/lib/lib$(L).so
	ln -s lib$(L2).so.$(SO2) debian/tmp/usr/lib/lib$(L2).so

# Generate control file for gmp3-dev
#
	dpkg-gencontrol -plib$(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 gmp-h.in
endef

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

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