#!/usr/bin/make -f
# debian/rules file for libraries
#
# To build the packages, run `dpkg-buildpackage' or `debuild' from the
# parent directory of this file. (You may need to specify the `-rfakeroot'
# option if you are using dpkg-buildpackage and are not running as root)
#
# $Id: rules,v 1.8 2003/04/30 07:45:50 timshel Exp $
#
# Copyright (C) 1999, 2000, 2001, 2002  Timshel Knoll <timshel@debian.org>
# Licensed under the terms of the GNU General Public License
#
# Based originally on Sample debian/rules that uses debhelper, from dh-make,
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE       ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE      ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)


CFLAGS += -g
ifeq (, $(findstring noopt, $(DEB_BUILD_OPTIONS)))
	CFLAGS += -O2
else
	CFLAGS += -O0
endif


# The name of the library - this is the base name of the packages that
# will be built
LIBRARY = liboop

# This is the soname of the package being built - we have to know this
# before the start of the build because changing the control file half
# way though the build probably isn't a good idea, and this would also
# mean renaming the $(LIBRARY)$(SONAME).{files,docs,...} files
SONAME = 4

# A list of variables to substitute when generating files from .in files
# If you put an 'x' here, then all @x@'s in .in files will be substituted
# with the value of $(x) in the output file
SUBSTS = SONAME

GENFILES = debian/control \
	debian/$(LIBRARY)$(SONAME).install \
	debian/$(LIBRARY)$(SONAME).dirs

# We can't use these until after the package has been built ... otherwise
# they will fail because no .libs/lib*.so.* exists
version = $(shell ls .libs/lib*.so.* | \
	    awk '{if (match($$0,/[0-9]+\.[0-9]+\.[0-9]+$$/)) \
	         print substr($$0,RSTART)}')
major = $(shell ls .libs/lib*.so.* | \
	  awk '{if (match($$0,/\.so\.[0-9]+$$/)) print substr($$0,RSTART+4)}')

# This builds a substitution list for sed based on the SUBSTS variable
# and the variables whose names SUBSTS contains ...
SUBSTLIST = $(foreach subst, $(SUBSTS),s/@$(subst)@/$($(subst))/g;)

# A sane default rule 
default:
	@echo "Try: debian/rules [configure|build|clean|install|binary|binary-arch|binary-indep]"
	@echo "Vars:"
	@echo "	SUBSTLIST:	$(SUBSTLIST)"
	@echo "	SONAME:		$(SONAME)"

# Pattern rules:

# How to generate files from .in's
debian/%: debian/%.in debian/rules
	sed -e '$(SUBSTLIST)' < $< > $@

# This puts the $(LIBRARY)* packaging files in their right places
# Could I / should I use ln?
debian/$(LIBRARY)$(SONAME).%: debian/$(LIBRARY).%
	cp $< $@

# Do the substitution/moving stuff
packaging-files: $(GENFILES)

configure: packaging-files configure-stamp
configure-stamp:
	dh_testdir

	autoreconf -sfi

	env CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) \
		--build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr

	touch $@

build: configure-stamp build-stamp
build-stamp:
	dh_testdir

	$(MAKE)

	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	[ ! -f Makefile ] || $(MAKE) distclean

	rm -rf Makefile.in aclocal.m4 ltmain.sh configure mkinstalldirs config.sub config.guess autom4te.cache missing depcomp install-sh
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep

	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp

binary-indep:
	dh_testdir
	dh_testroot
	dh_install -i

	dh_installdocs -i
	dh_installman -i
	dh_installinfo -i
	dh_installchangelogs -i
	dh_link -i
	dh_compress -i -Xliboop-doc/html
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

binary-arch: install
	dh_testdir
	dh_testroot
	dh_install -a

	dh_installdocs -a
	dh_installman -a
	dh_installinfo -a
	dh_installchangelogs -a
	dh_link -a
	dh_strip -a --dbg-package=liboop-dbg
	dh_compress -a -Xliboop-doc/html
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
#	 Don't add the depends for adapter libraries - programs which link
#	 with them will also link with the appropriate library
	dh_shlibdeps -a -Xliboop-
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch

.PHONY: packaging-files configure build install
.PHONY: binary-indep binary-arch binary clean
