#!/usr/bin/make -f
## ----------------------------------------------------------------------
## debian/rules : package script for expat
## ----------------------------------------------------------------------

## ----------------------------------------------------------------------
## uncomment this to turn on verbose mode
#export DH_VERBOSE=1

## ----------------------------------------------------------------------
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

## ----------------------------------------------------------------------
CONFFLAGS	=
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
    CONFFLAGS	+= --build $(DEB_HOST_GNU_TYPE)
else
    CONFFLAGS	+= --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
endif

## ----------------------------------------------------------------------
INSTALL		= install
INSTALL_FILE	= $(INSTALL) -p -o root -g root -m  644
INSTALL_PROGRAM	= $(INSTALL) -p -o root -g root -m  755
INSTALL_SCRIPT	= $(INSTALL) -p -o root -g root -m  755
INSTALL_DIR	= $(INSTALL) -p -o root -g root -m  755 -d

## ----------------------------------------------------------------------
CFLAGS		= -Wall -g -pthread -D_REENTRANT

## ----------------------------------------------------------------------
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
    CFLAGS	+= -O0
else
    CFLAGS	+= -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
    INSTALL_PROGRAM += -s
endif

# Use soft-float and thumb mode if it is enabled.
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -mthumb
endif

## ----------------------------------------------------------------------
UPACKAGE	= $(shell dh_listpackages | grep -- -udeb$$)
VERSION		= $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2)
ARCH		= $(shell dpkg --print-architecture)
UFILENAME	= $(UPACKAGE)_$(VERSION)_$(ARCH).udeb

## ----------------------------------------------------------------------
TMP_DIR		= $(CURDIR)/debian/tmp

## ----------------------------------------------------------------------
## targets

clean:		configure
		dh_testdir
		dh_testroot
		[ ! -f Makefile ] || $(MAKE) distclean
		dh_clean configure-stamp build-stamp install-stamp

configure:
		./autogen.sh

configure-stamp:	
		dh_testdir
		touch configure.in && touch aclocal.m4 && touch configure
		CFLAGS="$(CFLAGS)" \
		./configure $(CONFFLAGS) --prefix=/usr
		touch configure-stamp 

build:		build-stamp
build-stamp:	configure-stamp
		dh_testdir
		$(MAKE)
		touch build-stamp 

install:	install-stamp
install-stamp:	build
		dh_testdir
		dh_testroot
		dh_clean -k
		dh_installdirs
		$(MAKE) install prefix=$(TMP_DIR)/usr \
			man1dir=$(TMP_DIR)/usr/share/man/man1
		touch install-stamp

binary-indep:

binary-arch:	build install
		dh_testdir
		dh_testroot
		dh_install -a --sourcedir=$(TMP_DIR)
		dh_installdocs -a -N$(UPACKAGE)
		dh_installexamples -a -N$(UPACKAGE)
		dh_installchangelogs -a Changes -N$(UPACKAGE)
		dh_link -a
		dh_strip -a --dbg-package=libexpat1
		dh_compress -a
		dh_fixperms -a
		dh_makeshlibs -a -V -N$(UPACKAGE)
		dh_installdeb -a
		dh_shlibdeps -a -l $(PWD)/debian/libexpat1/usr/lib
		dh_gencontrol -a -N$(UPACKAGE)
		dh_gencontrol -p$(UPACKAGE) -- -fdebian/files~
		dpkg-distaddfile $(UFILENAME) debian-installer extra
		dh_md5sums -a -N$(UPACKAGE)
		dh_builddeb -a -N$(UPACKAGE)
		dh_builddeb -p$(UPACKAGE) --filename=$(UFILENAME)

binary:		binary-indep binary-arch

.PHONY:		clean build install binary-indep binary-arch binary

## ----------------------------------------------------------------------
