#!/usr/bin/make -f
# GNU copyright 1997 to 1999 by Joey Hess.

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

DESTDIR=$(CURDIR)/debian/arj

INSTALL=install
INSTALL_DATA=$(INSTALL) -m 644

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

CFLAGS = -Wall -g

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

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	DEBUG = "DEBUG=1"
endif

include debian/patch.mk

gnu/config.status: patch gnu/configure
gnu/configure:
	dh_testdir
	
	( \
	cd gnu; \
	autoconf; \
	./configure \
		--prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
	)

build: gnu/config.status
	dh_testdir
	
	$(MAKE) CFLAGS="$(CFLAGS)" $(DEBUG)

clean::
	dh_testdir
	dh_testroot
	rm -f configure-stamp
	
	-$(MAKE) clean
	-rm -f gnu/{configure,config.{status,log,cache}}
	-rm -rf gnu/autom4te.cache
	-rm -f c_defs.h GNUmakefile
	
	-test -r /usr/share/misc/config.sub && \
	  cp -fu /usr/share/misc/config.sub gnu/config.sub
	-test -r /usr/share/misc/config.guess && \
	  cp -fu /usr/share/misc/config.guess gnu/config.guess
	
	dh_clean

install-arch: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	
	$(MAKE) install DESTDIR=$(DESTDIR)
	$(INSTALL_DATA) $(CURDIR)/resource/rearj.cfg.example \
			$(DESTDIR)/etc/rearj.cfg

binary-common:
	dh_testdir
	dh_testroot
#	dh_installdebconf
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installmime
	dh_installcron
	dh_installman
	dh_installinfo
	dh_installchangelogs ChangeLog
	dh_link
	# NOTE: Do not strip, it is currently done in the linker stage.
	# Otherwise when stripping the self-extracting modules and the
	# help text are removed.
#	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep:

binary-arch: install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

binary: binary-arch binary-indep

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

