#!/usr/bin/make -f

ifeq "$(findstring debug,$(DEB_BUILD_OPTIONS))" ""
export CFLAGS = -O2 -g
else
export CFLAGS = -g
endif

SRC	:= $(PWD)/src
BUILD	:= $(PWD)/debian/build
TARGET	:= $(PWD)/debian/tla

build: debian/build-stamp
debian/build-stamp:
	dh_testdir

	[ -d $(BUILD) ] || mkdir $(BUILD)
	cd $(BUILD) && tar xzf $(BUILD)/../diffutils-2.8.1.tar.gz
	cd $(BUILD)/diffutils-2.8.1/ && ./configure
	cd $(BUILD)/diffutils-2.8.1/ && $(MAKE)
	sed -e 's|exit 1|exit 0|' $(SRC)/build-tools/auto-conf-lib/nonl-safe-diff >$(SRC)/build-tools/auto-conf-lib/nonl-safe-diff.tmp
	mv $(SRC)/build-tools/auto-conf-lib/nonl-safe-diff.tmp $(SRC)/build-tools/auto-conf-lib/nonl-safe-diff
	cd $(BUILD) && PATH="$(BUILD)/diffutils-2.8.1/src:$${PATH}" $(SRC)/configure --prefix '/usr' --with-gnu-diff3 /usr/share/tla/diff3 --with-gnu-diff /usr/share/tla/diff
	$(MAKE) -C $(BUILD)
#	$(MAKE) -C $(BUILD) test

	touch debian/build-stamp

clean:
	dh_testdir
	rm -f debian/build-stamp

	rm -rf $(BUILD)
	dh_clean

install: debian/build-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) -C $(BUILD) install destdir=$(TARGET)
	mkdir -p $(TARGET)/usr/share/tla
	cp $(BUILD)/diffutils-2.8.1/src/diff $(TARGET)/usr/share/tla/
	cp $(BUILD)/diffutils-2.8.1/src/diff3 $(TARGET)/usr/share/tla/
	#dh_install --sourcedir=$(TARGET)

binary-indep:

binary-arch: build install
	dh_testdir -a
	dh_testroot -a

	dh_installdocs -a
	dh_installchangelogs -a -ptla src/tla/ChangeLog

	dh_link -a
	dh_strip -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

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