#!/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)

# XXX: The upstream build system does not support cross-building...
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  conf_gnu_type += --build $(DEB_HOST_GNU_TYPE)
else
  conf_gnu_type += --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
endif

CFLAGS = -Wall -g

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

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

gnu/configure: gnu/configure.in
	dh_testdir
	
	cd gnu && autoconf -f -i

gnu/config.status: gnu/configure
	dh_testdir
	
	-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
	
	cd gnu && ./configure \
	  $(conf_gnu_type) \
	  --prefix=/usr \
	  --mandir=\$${prefix}/share/man

build-indep:

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

build: build-arch build-indep

clean:
	dh_testdir
	dh_testroot
	
	[ ! -f Makefile ] || $(MAKE) clean
	
	-rm -f gnu/configure
	-rm -f gnu/config.status gnu/config.log gnu/config.cache
	-rm -rf gnu/autom4te.cache
	-rm -f c_defs.h GNUmakefile
	rm -f gnu/config.sub gnu/config.guess
	
	dh_clean

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

binary-common:
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installexamples
	dh_installmenu
	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 build-indep build-arch install-arch
.PHONY: binary-common binary-indep binary-arch binary
