#!/usr/bin/make -f
# David Martnez <ender@debian.org>
# It seems that I finally changed this cursed Makefile to DHv3.
# Now I only have to change this cursed Makefile to DHv4. :-)

version=$(shell expr `pwd` : '.*-\([0-9.]*\)')
version_major=$(shell expr `pwd` : '.*-\([0-9]*\).[0-9.]*')

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -g
endif

# WBS codec is licensed by British Telecommunications,
# and its license is not compatible with DFSG.
CFLAGS += -DREMOVE_WBS_CODEC

CFLAGS += -DUSE_INTERP_RESULT

#export DH_VERBOSE=1

CFLAGS += -O2
export CFLAGS

# Let's roll...
config: patch config-stamp

config-stamp:
	dh_testdir
	dh_autotools-dev_updateconfig
	./configure --enable-ipv6 \
		    --with-tcl=/usr/include/tcl \
		    --with-tk=/usr/include/tcl \
		    --with-tcltk-version="" \
		    --with-common=/usr/include/uclmmbase \
		    --mandir=$(CURDIR)/debian/tmp/usr/share/man
	touch config-stamp

build: config build-stamp

build-stamp:
	dh_testdir
	$(MAKE)
	touch build-stamp

clean: clean1 unpatch

clean1:
	dh_testdir
	dh_testroot
	rm -f build-stamp config-stamp patch-stamp
	if [ -e Makefile ] ; then $(MAKE) distclean; fi
	dh_autotools-dev_restoreconfig
	dh_clean

patch: patch-stamp
patch-stamp:
	test -d debian/patched || install -d debian/patched
	@echo "Patches applied in the Debian version of rat:" > $@T
	@for patch in `cat debian/patches/00list`; do \
		stamp=debian/patched/$$patch.dpatch; \
		patch=debian/patches/$$patch.dpatch; \
		test -x $$patch || chmod +x $$patch; \
		if test -f $$stamp; then \
			echo "$$patch already applied."; \
			/bin/echo -e "\n$$patch:" >> $@T; \
			sed -n 's/^## *DP: */  /p' $$patch >> $@T; \
		else \
			echo "trying to apply patch $$patch..."; \
			if $$patch -patch >$$stamp.new 2>&1; then \
				mv $$stamp.new $$stamp; \
				touch $$stamp; \
				/bin/echo -e "\n$$patch:" >> $@T; \
				sed -n 's/^## *DP: */  /p' $$patch >> $@T; \
			else \
				echo "error in applying $$patch patch."; \
				exit 1; \
			fi; \
		fi; \
	done
	mv -f $@T $@

unpatch:
	@for patch in `tac debian/patches/00list`; do \
		stamp=debian/patched/$$patch.dpatch; \
		patch=debian/patches/$$patch.dpatch; \
		test -x $$patch || chmod +x $$patch; \
		if test -f $$stamp; then \
			echo "trying to revert patch $$patch..."; \
			if $$patch -unpatch 2>&1 >/dev/null; then \
				rm -f $$stamp; \
			else \
				echo "error in reverting $$patch patch."; \
				exit 1; \
			fi; \
		fi; \
	done
	rm -f patch-stamp

install: build
	dh_testdir
	dh_clean
	dh_installdirs

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

	dh_movefiles
build-indep binary-indep: build

build-arch binary-arch: build install
	dh_testdir
	dh_installdocs
#	dh_installmenu
	dh_installman
#	dh_undocumented rat-ui.1 rat-media.1
	dh_installchangelogs
	dh_fixperms
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

source diff:								  
	@echo >&2 'source and diff are obsolete - use dpkg-source -b'; false

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary
# vim:set noet ai sts=8 sw=8 tw=0:
