#!/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.
# It was adapted to build libmdate in November of 2000 by David Starner
# <dstarner98@aasaa.ofe.org>.
# This file is copyright 1997 by Software in the Public Interest and 
# is thereby licensed under the GPL.

# Package name variables
#
P=mdate0
L=mdate
P2=mdate-dev
arch=$(shell dpkg --print-architecture)

# 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)	$(v1)	lib$(P)

# Build the binary components and assemble the libraries
# =====================================================================
build:
	$(checkdir)
	./configure --target=`dpkg-architecture -qDEB_BUILD_GNU_TYPE`
	$(MAKE) CFLAGS="-O3"
	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 -f ./.dep

# 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 libmdate0...........................................
#
# 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/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
	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 library
#
	strip --strip-unneeded ./src/.libs/lib$(L).so.$(v2)
	install -m 644 ./src/.libs/lib$(L).so.$(v2) \
		debian/tmp/usr/lib/lib$(L).so.$(v2)

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

# Create dependency information
#
	dpkg-shlibdeps ./src/.libs/lib$(L).so.$(v2)

# Generate control file for libmdate0
#
	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 libmdate-dev......................................
#
# Clean out old tmp directory
#
	-rm -rf debian/tmp

# Create new directories in debian/tmp
#
	mkdir 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 -d debian/tmp/usr/share/info

# 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
	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 src/mdate.h debian/tmp/usr/include/.

# Create, install and gzip the info pages
#
	cd doc; make libmdate.info; cd ..
	install -m 644 doc/lib$(L).info* debian/tmp/usr/share/info/.
	gzip -9v debian/tmp/usr/share/info/*
	
# Install static library
#
#	strip --strip-debug ./.libs/lib$(L).a
	install -m 644 ./src/.libs/lib$(L).a debian/tmp/usr/lib/.
#
# Create old link
#
	ln -s ./lib$(L).a debian/tmp/usr/lib/lib$(P).a

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

# Generate control file for libmdate-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 src/mdate.h
endef

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

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