#!/usr/bin/make -f
# -*- makefile -*-
# debian/rules for libpaper

export DH_OPTIONS=

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

package	:= $(firstword $(shell dh_listpackages))
prefix	:= $(PWD)/debian/tmp
share	:= /usr/share

config_flags	:= --prefix=/usr \
	--sysconfdir=/etc \
	--mandir=$(share)/man \
	--infodir=$(share)/info \
	--build $(DEB_HOST_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
config_flags	+= --host $(DEB_HOST_GNU_TYPE)
endif

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

export CFLAGS=$(cflags)
export CXXFLAGS=$(cflags)

version	:= $(shell dpkg-parsechangelog | \
			sed -ne 's/^Version: *\([0-9]\+:\)*//p')

get_misc_file		= ln -sf /usr/share/misc/$(1) .
update_config_sub       := $(call get_misc_file,config.sub)
update_config_guess     := $(call get_misc_file,config.guess)

tag:
	cvs tag -c -F $(subst .,_,debian_version_$(version))
ifeq ($(findstring -,$(version)),)
	cvs tag -c -F $(subst .,_,upstream_version_$(version))
endif

config:	config-stamp
config-stamp:
	dh_testdir
	$(update_config_sub)
	$(update_config_guess)
	$(SHELL) ./configure $(config_flags)
	touch $@

build:	config build-stamp
build-stamp:
	$(MAKE)
	touch $@

clean:	checkroot
	rm -f *-stamp config.sub config.guess
	-$(MAKE) distclean
	dh_clean

binary-indep:	DH_OPTIONS=-i
binary-indep:	checkroot build
	dh_clean -k
	dh_installdirs

	dh_link

	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-arch:	DH_OPTIONS=-a
binary-arch:	checkroot build
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=$(prefix)

	dh_install

	dh_installchangelogs
	dh_installdocs

	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdebconf
	dh_installdeb
	dh_shlibdeps -L$(package) -ldebian/$(package)/usr/lib
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary: binary-indep binary-arch

.PHONY:	binary binary-indep binary-arch clean checkroot build config tag
