#=============================================================================
#
# Maintainer stuff, you don't want to mess with this.
#
# In order to use this as-is, you'll need the following utilities:
#
# GNU make
# GNU which-2.x
# GNU date
# awk
# rpm
# rcs2log
# cut
# grep
# egrep
# test
# sed
#
# This maintainer Makefile provides the following functionality:
#
# Targets
# -------
#
# - rpm
#	Generates the $(TARGET).rpm and src.rpm files.
#
# - full-check
#	Iterate over multiple configurations and compiler versions,
#	running the test suite for every combination.
#
# - documentation
#	Does a `make' in documentation/.
#
# The remaining targets can be used to test a part of the distribution process,
# but are not really useful on their own otherwise.
#
# - ChangeLog
#	Generates the `ChangeLog' file from the SVN repository logs.
#
# - test-values
#	Print debugging info about variables used in this file.
#

RPMDIR=$(top_builddir)/rpm
TARGET=@TARGET@
SPECFILE=@SPECFILE@
LSMFILE=@LSMFILE@
REL=1
TAG:=${shell echo "V@VERSION@" | sed -e 's/\./_/g'}
PREVTAG:=${shell test -f .prevtag && cat .prevtag}
SVNREADACCESS:=${shell if test -f $(srcdir)/.svn/entries; then echo "yes"; else echo "no"; fi}
AWK=@AWK@

BUILT_SOURCES = .example-project_debug.h-timestamp

test-values:
	@echo "srcdir	= \"$(srcdir)\""
	@echo "RPMDIR   = \"$(RPMDIR)\""
	@echo "PACKAGE  = \"@PACKAGE@\""
	@echo "VERSION  = \"@VERSION@\""
	@echo "SPECFILE = \"$(SPECFILE)\""
	@echo "TARGET   = \"$(TARGET)\""
	@echo "REL      = \"$(REL)\""
	@echo "SVNREADACCESS = \"$(SVNREADACCESS)\""
	@echo "TAG      = \"$(TAG)\""
	@echo "PREVTAG  = \"$(PREVTAG)\""

## Make sure the ChangeLog is up to date in maintainer mode
.PHONY: rpm ChangeLog documentation debian @PACKAGE@-@VERSION@.tar.gz

rpm: dist $(SPECFILE) $(LSMFILE)
	mv @PACKAGE@-@VERSION@.tar.gz $(RPMDIR)/SOURCES
	cd rpm; rpmbuild --rcfile=/usr/lib/rpm/rpmrc:rpmrc --sign --clean -ba ../$(SPECFILE)

@PACKAGE@-@VERSION@.tar.gz:
	RESULT=ok; \
	for f in $$(tar tzf @PACKAGE@-@VERSION@.tar.gz | sed -e 's%^[^/]*/%%'); do \
	  test -e ../libcwd/$$f && test ../libcwd/$$f -nt libcwd-1.0.4.tar.gz && echo 'WARNING: '$$f' is newer than @PACKAGE@-@VERSION@.tar.gz!' && RESULT=error; \
	done; \
	if test "$$RESULT" != "ok"; then \
	  echo "You must run: $(MAKE) dist"; \
	  exit -1; \
	fi

debian: @PACKAGE@-@VERSION@.tar.gz
	VER="$$(dpkg-parsechangelog -l$(srcdir)/debian/changelog | sed -n -e 's/-.*//;s/^Version: //p')"; \
	if test "$$VER" != "@VERSION@"; then \
	  echo "The version in debian/changelog ($$VER) does not match @VERSION@." \
	  echo "Run (cd $(srcdir) && dch -v @VERSION@-1 && dch -r) and rebuild the tar ball!"; \
	  exit 1; \
	fi
	rm -rf debian-build
	mkdir debian-build
	cd debian-build; \
	tar xzf ../@PACKAGE@-@VERSION@.tar.gz; \
	tar czf @PACKAGE@_@VERSION@.orig.tar.gz --exclude debian @PACKAGE@-@VERSION@; \
	cd @PACKAGE@-@VERSION@; \
	dpkg-buildpackage -us -uc; \
	cd ..; \
	lintian @PACKAGE@_@VERSION@-1_amd64.changes

ChangeLog:
	if test $(SVNREADACCESS) = yes; then \
	  svn log | mawk '/^-*-$$/ { start=1 } // { if (!start) { if ($$0 ~ /^$$/) print; else printf("\t%s\n", $$0); } } /^r[0-9]+ \|[^|]*\| / { split($$0,A,/\|/); split(A[3],B); gsub(/ /,"",A[1]);  gsub(/ /,"",A[2]); gsub(/libcw/,"libcw  <carlo@taryn>",A[2]); printf("%s (%s)  %s\n", B[1], A[1], A[2]); start=0 }' > ChangeLog; \
	else \
	  touch ChangeLog; \
	fi

# Allow this file to be missing.
$(srcdir)/example-project/debug.h.maintainer:

$(srcdir)/example-project/debug.h: $(srcdir)/example-project/debug.h.maintainer $(srcdir)/nodebug.h
	@mkdir -p example-project
	if test -f $(srcdir)/example-project/debug.h.maintainer; then \
	  awk '/@NODEBUGMACROS@/ { exit } { print }' $(srcdir)/example-project/debug.h.maintainer > $(srcdir)/example-project/debug.h; \
	  cat $(srcdir)/nodebug.h >> $(srcdir)/example-project/debug.h; \
	  awk 'BEGIN { after=0 } /@NODEBUGMACROS@/ { after=1; next } { if (after==1) print }' $(srcdir)/example-project/debug.h.maintainer >> $(srcdir)/example-project/debug.h; \
	fi

CCVERSVN=svn-4.6
CCVERALL=$(CCVERSVN),4.5.0,4.4.4,4.4.0,4.3.3,4.3.0,4.2.4,4.2.0,4.1.2,4.1.0,4.0.3,4.0.0,3.4.6,3.4.1,3.4.0,3.3.6
CCVERMOST=$(CCVERSVN),4.5.0,4.4.0,4.3.0,4.2.0,4.1.0,4.0.0,3.4.6,3.4.0,3.3.6

#		'--enable-maintainer-mode --with-redzone=1024 --compilers=$(CCVERSVN)' \
#		'--enable-maintainer-mode --compilers=$(CCVERALL)' \
#		'--enable-maintainer-mode --disable-pch                                                                       --compilers=$(CCVERMOST)' \
#	  	'--enable-maintainer-mode --disable-pch --enable-debugt --enable-debugm --enable-debug --disable-debug-output --compilers=$(CCVERMOST)' \
#		'--enable-maintainer-mode --disable-pch --enable-debugt                 --enable-debug --disable-debug-output --compilers=$(CCVERMOST)' \
#	  	'--enable-maintainer-mode --disable-pch                 --enable-debugm --enable-debug --disable-debug-output --compilers=$(CCVERMOST)' \
#	  	'--enable-maintainer-mode --disable-pch                                 --enable-debug --disable-debug-output --compilers=$(CCVERMOST)' \
#	  	'--enable-maintainer-mode --disable-pch                 --enable-debugm                                       --compilers=$(CCVERMOST)' \
#		'--enable-maintainer-mode --disable-pch --enable-debugt --enable-debugm --enable-debug                        --compilers=$(CCVERMOST)' \
#		'--enable-maintainer-mode --disable-pch                 --enable-debugm --enable-debug                        --compilers=$(CCVERMOST)' \

full-check: Makefile
	@( \
	  for c in \
		'--enable-maintainer-mode --with-redzone=1024 --compilers=$(CCVERSVN)' \
		'--enable-maintainer-mode --compilers=$(CCVERALL)' \
		'--enable-maintainer-mode --disable-pch                                                                       --compilers=$(CCVERMOST)' \
	  	'--enable-maintainer-mode --disable-pch --enable-debugt --enable-debugm --enable-debug --disable-debug-output --compilers=$(CCVERMOST)' \
		'--enable-maintainer-mode --disable-pch --enable-debugt                 --enable-debug --disable-debug-output --compilers=$(CCVERMOST)' \
	  	'--enable-maintainer-mode --disable-pch                 --enable-debugm --enable-debug --disable-debug-output --compilers=$(CCVERMOST)' \
	  	'--enable-maintainer-mode --disable-pch                                 --enable-debug --disable-debug-output --compilers=$(CCVERMOST)' \
	  	'--enable-maintainer-mode --disable-pch                 --enable-debugm                                       --compilers=$(CCVERMOST)' \
		'--enable-maintainer-mode --disable-pch --enable-debugt --enable-debugm --enable-debug                        --compilers=$(CCVERMOST)' \
		'--enable-maintainer-mode --disable-pch                 --enable-debugm --enable-debug                        --compilers=$(CCVERMOST)' \
		; do \
	    config=`echo $$c | sed -e 's/ --compilers=.*$$//'`; \
	    case "$$c" in \
	      *--disable-debug-output*) check=check ;; \
	      *--enable-debug\ *) check=run ;; \
	      *) check=check ;; \
	    esac; \
	    compilers=`echo $$c | sed -e 's/.* --compilers=//' -e 's/,/ /g'`; \
	    test -z "$$COMPILERS" || compilers="$$COMPILERS"; \
	    echo "*** CONFIGURATION OPTIONS: \"$$config\" (with compilers \"$$compilers\")"; \
	    for v in `echo $$compilers`; do \
	      case "$$v" in \
	      3*) cf="$$config --disable-threading" ;; \
	      *) cf="$$config" ;; \
	      esac; \
	      echo "*** COMPILER VERSION: \"$$v\""; \
	      echo -n "Configuring... "; \
	      if eval "CC=\"gcc-$$v\" CXX=\"g++-$$v\" $(srcdir)/configure $$cf >/dev/null"; then \
		echo "done"; \
	      else \
	        echo "failed" && exit -1; \
	      fi; \
	      echo -n "Compiling... "; \
	      if make -j 4 >/dev/null; then \
		echo "done"; \
	      else \
		echo "failed" && exit -1; \
	      fi; \
	      cd testsuite >/dev/null; \
	      make clean >/dev/null; \
	      echo -n "Running \`make $$check'... "; \
	      if if make RUNTESTFLAGS="" $$check 2>/dev/null; then \
	        echo "done"; \
	      else \
		echo "*** FAILURE FOR \"$$cf\" USING g++-$$v" && exit -1; \
	      fi | egrep '(FAIL|ERROR)'; then exit -1; else echo "ok"; fi; \
	      cd .. >/dev/null; \
	    done; \
	  done; \
	)

documentation: $(srcdir)/example-project/debug.h
	$(MAKE) -C documentation

.example-project_debug.h-timestamp: example-project/debug.h
	touch .example-project_debug.h-timestamp

syncwww: documentation
	@echo "Synchronizing with libcwd.sourceforge.net..."
	rsync -rlptz -e /usr/bin/ssh --delete --exclude-from=$(srcdir)/documentation/www/exclude --delete-excluded \
		`pwd`/documentation/ "libcwd-web:htdocs/"
	@echo "Synchronizing with www.xs4all.nl..."
	rsync -rlptz -e /usr/bin/ssh --delete --exclude-from=$(srcdir)/documentation/www/exclude --delete-excluded \
		`pwd`/documentation/ xs2:"~/WWW/libcwd"

svn-clean:
	$(MAKE) -C documentation maintainer-clean
	(test -f example-project/Makefile && $(MAKE) -C example-project maintainer-clean) || true
	$(MAKE) maintainer-clean
	for d in documentation/tutorial/examples5 documentation/tutorial/examples7 documentation/tutorial \
		 documentation/styles documentation/external documentation \
		 example-project testsuite tests cutee utils include/libcwd include; do \
	  test ! -d $$d || rmdir --ignore-fail-on-non-empty $$d; \
	done
