#!/usr/bin/make -f

PACKAGE	 = ccbuild

include debian/debian-save-restore.mk

# autoreconf(1) changes few
FILE_LIST_PRESERVE = \
 depcomp \
 INSTALL \
 Makefile.in \
 aclocal.m4 \
 configure \
 configure.in \
 doc/ccbuild/Makefile

export DEB_BUILD_MAINT_OPTIONS  = hardening=+all
export DEB_CXXFLAGS_MAINT_APPEND  = -Wall -pedantic
export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

# 2012-05-15 No longer used. Bobcat 2.22 is working on hurd again.
control:
	# target: control -- Generate debian/control from *.in
	# 2011-11-30 Exclude due to problems in package
	# bobcat (hurd-i386).
	# https://buildd.debian.org/status/package.php?p=bobcat&suite=sid

	arch=$$(dpkg-architecture -L | grep -Ev 'hurd' | sort); \
	sed "s/@arch@/$$(echo $$arch)/g" debian/control.in > debian/control

override_dh_clean:
	dh_clean

override_dh_auto_clean:
	$(file-state-save)

	# Do all manually. Makefile would run ./config.status --recheck
	find .	-type f \
		-name $(PACKAGE) \
		-o -name Makefile \
		-o -name "*.o" \
		-o -name "config.*" \
		-o -name .dirstamp \
		| xargs --no-run-if-empty rm

	find . -type d -name .deps \
		| xargs --no-run-if-empty rm -r

	# Due to autoreconf(1)
	rm -f	INSTALL Makefile.in aclocal.m4 config.* configure \
		src/SourceScanner/yylex.cc

override_dh_auto_configure:
	# See 20. We have to regenerate ./configure
	cp -vf /usr/share/misc/config.sub /usr/share/misc/config.guess .
	autoreconf -vfi
	dh_auto_configure

override_dh_auto_build:
	# Fix -pedantic: "flex adds the declaration of isatty in the
	# generated source file but this declaration is already
	# present in the included /usr/include/unistd.h file."
	#
	# see http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=488274
	$(MAKE) src/sourceScanner/yylex.cc
	sed --in-place "s,^\(extern.*isatty.*\),/* \1 */," \
	    src/sourceScanner/yylex.cc
	grep -nH isatty src/sourceScanner/yylex.cc

	# For some reason, the only way to pass flags is in CXX
	@echo "# environment: CXX $(CXX) CFLAGS $(CFLAGS) CXXFLAGS $(CXXFLAGS)"
	$(MAKE) CXX="g++ $(CXXFLAGS)" DESTDIR=$(CURDIR)/debian/ccbuild install

override_dh_installchangelogs:
	$(file-state-restore)
	dh_installchangelogs ChangeLog

%:
	dh  $@

# End of file
