#!/usr/bin/make -f

DEB_BUILD_GNU_TYPE = $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE = $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifneq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
CONFARGS = --host=$(DEB_HOST_GNU_TYPE)
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif
ifneq (,$(findstring thumb,$(DEB_BUILD_OPTIONS)))
    CFLAGS += -mthumb
endif

# Comment out to execute regression tests
# (tests 9 10 11 13 15 16 17 61 will fail with qemu)
__NO_TESTS_PLEASE__=1

configure: configure-stamp
configure-stamp:
	dh_testdir

	RSH="/usr/bin/rsh" CFLAGS="-O2 -g -Wall $(CFLAGS)" \
		./configure --prefix=/usr --libexecdir=/usr/sbin $(CONFARGS)

	touch configure-stamp

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

	RSH="/usr/bin/rsh" CFLAGS="-O2 -g -Wall -fno-gnu89-inline $(CFLAGS)" $(MAKE)
ifndef __NO_TESTS_PLEASE__
ifeq ($(DEB_BUILD_GNU_TYPE),$(DEB_HOST_GNU_TYPE))
	(cd tests ; $(MAKE) clean)
	$(MAKE) check
endif
endif

	touch build-stamp

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

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

	-test -r /usr/share/misc/config.sub && \
		cp -f /usr/share/misc/config.sub build-aux/config.sub
	-test -r /usr/share/misc/config.guess && \
		cp -f /usr/share/misc/config.guess build-aux/config.guess

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	dh_install

	make install bindir=`pwd`/debian/gnutar/bin prefix=`pwd`/debian/gnutar/usr \
		libexecdir=`pwd`/debian/gnutar/usr/sbin 
	rm -rf debian/gnutar/usr/sbin debian/gnutar/etc
	uudecode debian/tar-26.b64 -o debian/gnutar/usr/share/icons/hicolor/26x26/mimetypes/application-x-tar.png
	uudecode debian/tar-48.b64 -o debian/gnutar/usr/share/icons/hicolor/48x48/mimetypes/application-x-tar.png
	uudecode debian/tar-64.b64 -o debian/gnutar/usr/share/icons/hicolor/64x64/mimetypes/application-x-tar.png

# Build architecture-independent files here.
binary-indep: build install

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	mv debian/gnutar/bin/tar debian/gnutar/bin/gtar
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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