#!/usr/bin/make -f

testdir  = test -f src/lrz.c && test -f debian/rules
testroot = test x`whoami` = xroot

# FOR AUTOCONF 2.13 ONLY
ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  confflags += $(DEB_HOST_GNU_TYPE)
else
  $(error Cannot cross-compile this package out-of-the-box)
endif

CPPFLAGS = $(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS   = $(shell dpkg-buildflags --get CFLAGS)
LDFLAGS  = $(shell dpkg-buildflags --get LDFLAGS)
export CPPFLAGS
export CFLAGS
export LDFLAGS

build: build-arch build-indep

build-arch: build-stamp

build-indep: build-stamp

build-stamp:
	$(testdir)
	cp -f /usr/share/misc/config.guess /usr/share/misc/config.sub .
	./configure $(CONFFLAGS) --prefix=/usr --mandir='$${prefix}/share/man' --program-transform-name=s/l//
	touch stamp-h.in # to stop autoheader from running
	touch -d yesterday aclocal.m4 # to stop automake from running
	$(MAKE)
	touch $@

clean:
	$(testdir)
	$(testroot)
	cp -f /usr/share/misc/config.guess /usr/share/misc/config.sub .
	touch stamp-h.in # to stop autoheader from running
	touch -d yesterday aclocal.m4 # to stop automake from running
	[ ! -f Makefile ] || $(MAKE) distclean
	rm -rf debian/tmp
	rm -f build-stamp config.guess config.sub po/lrzsz.pot po/*.gmo debian/files debian/rejected debian/substvars

binary: binary-arch binary-indep

binary-arch: build
	$(testdir)
	$(testroot)
	rm -rf debian/tmp

	$(MAKE) prefix=$(CURDIR)/debian/tmp/usr install

ifeq "$(findstring nostrip,$(DEB_BUILD_OPTIONS))" ""
	strip -R .comment -R .note debian/tmp/usr/bin/*
endif

	install -d -m 0755 debian/tmp/usr/share/doc/lrzsz
	install -p -m 0644 NEWS README README.cvs README.isdn4linux TODO debian/copyright debian/tmp/usr/share/doc/lrzsz
	install -p -m 0644 -D ChangeLog debian/tmp/usr/share/doc/lrzsz/changelog
	install -p -m 0644 -D debian/changelog debian/tmp/usr/share/doc/lrzsz/changelog.Debian

	find debian/tmp/usr/share/man -type f -print0 | xargs -0 gzip -9
	find debian/tmp/usr/share/doc -type f ! -name copyright ! -name '*.html' \
		\( -name 'changelog*' -o -size +8 \) -print0 | xargs -0 gzip -9

	ln -s rz.1.gz debian/tmp/usr/share/man/man1/rb.1.gz
	ln -s rz.1.gz debian/tmp/usr/share/man/man1/rx.1.gz
	ln -s sz.1.gz debian/tmp/usr/share/man/man1/sb.1.gz
	ln -s sz.1.gz debian/tmp/usr/share/man/man1/sx.1.gz

	install -d -m 0755 debian/tmp/DEBIAN
	install -p -m 0644 debian/control debian/tmp/DEBIAN
	cd debian/tmp && find usr -type f -print0 | xargs -0 md5sum > DEBIAN/md5sums

	chown -R root:root debian/tmp
	chmod -R go=u-w debian/tmp

	dpkg-shlibdeps debian/tmp/usr/bin/*
	dpkg-gencontrol -isp -plrzsz
	dpkg --build debian/tmp ..

binary-indep:

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