#=============================================================================
#
# Maintainer stuff; don't even LOOK at this - and when you E-mail me
# about it you'll get blacklisted }:-).
#
# I added it only here for other GNU maintainers to look at.
# In order to use this as-is, you'll need the following utilities:
#
# GNU make
# GNU which-2.x
# GNU awk
# GNU date
# GNU groff
# rpm-4.x (RedHat-7.x)
# texinfo2man.c (I'll make this available via my home page)
# texi2html
# cvs2html
# rcs2log
# cut
# grep
# egrep
# test
# sed
# head
# tail
#
# This maintainer Makefile provides the following functionality:
#
# Targets
# -------
#
# - release
#	Creates everything and puts it all in the release directory $(WWWDIR)
#       This target calls all of the below targets.
#	Note: You must `cvs commit' *before* calling this target!
#
# The remaining targets can be used to test a part of the distribution process,
# but are not really useful on their own otherwise.
#
# - cvslog
#	Generates html-ized ChangeLog from the CVS repository log.
#
# - html
#	Generates html-ized man page(s) (from which.texinfo) and
#	the release front page index.html. index.html is generated by
#	replacing "!VERSION!" in index.html.in by 2.16 and the
#	word "CHANGELOGLIST" by a list of links the cvslog change logs
#	(also from previous releases).
#
# - which.lsm
#	Generates an LSM file for distribution via the web page.
#	It does this by simply replacing "!VERSION!" by 2.16.
#
# - rpm
#	Generates the $(TARGET).rpm and src.rpm files.
#	This target calls all of the below targets.
#
# - $(SPECFILE)
#	Generates the rpm spec file from the file `$(SPECFILE).in' by
#	replacing "!VERSION!" by 2.16 and "!CHANGELOG!" by an
#	rpm %changelog list generated from the CVS repository log.
#
# - tar
#	Generates the tar.gz distribution file.
#	This target calls all of the below targets (provided you added
#	README, which.1 and ChangeLog to BUILT_SOURCES).
#
# - README
#	Generate README file by replacing "!VERSION!" in README.in by
#	2.16.
#
# - which.1
#	Generate man page from which.texinfo.
#
# - ChangeLog
#	Generates the `ChangeLog' file from the CVS repository logs.

WWWDIR=/home/carlo/www/which
REDHAT:=$(shell grep '^%_topdir' rpm/macros | cut -d \  -f 2)
SPECFILE=which-${shell echo "2.16" | cut -d. -f1}.spec
RPMDIR=$(top_builddir)/rpm
TARGET=i386
REL=1
AUTHOR=Carlo Wood <carlo@gnu.org>
TAG:=${shell echo "V2.16" | sed -e 's/\./_/g'}
PREVTAG:=${shell test -f .prevtag && cat .prevtag}
CVSREADACCESS:=${shell if test -f $(srcdir)/CVS/Entries; then echo "yes"; else echo "no"; fi}

test-values:
	@echo "WWWDIR        = \"$(WWWDIR)\""
	@echo "REDHAT        = \"$(REDHAT)\""
	@echo "SPECFILE      = \"$(SPECFILE)\""
	@echo "TARGET        = \"$(TARGET)\""
	@echo "REL           = \"$(REL)\""
	@echo "AUTHOR        = \"$(AUTHOR)\""
	@echo "TAG           = \"$(TAG)\""
	@echo "PREVTAG       = \"$(PREVTAG)\""
	@echo "PACKAGE       = \"which\""
	@echo "VERSION       = \"2.16\""
	@echo "CVSREADACCESS = \"$(CVSREADACCESS)\""

## Make sure the ChangeLog is up to date in maintainer mode
.PHONY: release tar rpm cvslog html ChangeLog

which.1: which.1.in which.texinfo texinfo2man configure.in
	texinfo2man which.1.in which.texinfo > which.1

texinfo2man: ../indent/texinfo2man.c
	$(CC) -g -o texinfo2man ../indent/texinfo2man.c

which.lsm: which.lsm.in configure.in
	sed -e 's%!VERSION!%'`grep 's,@VERSION' config.status | cut -d, -f3`'%g' which.lsm.in > which.lsm

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

reconfig: $(top_builddir)/config.status
	$(top_builddir)/config.status --recheck
	$(top_builddir)/config.status

cvslog:
	@( \
	   D1=`cat .release_second`; \
	   D2=`date +%s`; \
	   DD=`echo "$$D1 $$D2" | awk '{ printf("%d\n", ($$2 - $$1)/86400) }'`; \
	   echo "Last release was $$DD days ago."; \
	   cvs2html -e -r$(PREVTAG):HEAD -o cvslog-2.16; \
	 )

$(SPECFILE): $(SPECFILE).in configure.in
	cvs commit $(SPECFILE).in
	sed -e 's/!VERSION!/2.16/g' $(SPECFILE).in | grep -B2000 '!CHANGELOG!' | grep -v '!CHANGELOG!' > $(SPECFILE)
	echo -n "%changelog" >> $(SPECFILE)
	cvs log $(SPECFILE).in | \
	  grep -A2000 '^-----' | \
	  egrep -v '^-----|^=====|^revision' | \
	  awk -F'[ ;]' --assign lastdate="" '{ if ($$0~/^date: /) { \
	    if (lastdate != $$2) { \
	      lastdate=$$2; \
	      printf("\n* "); \
	      system("echo -n `date --date \""$$2" "$$3"\" \"+%a %b %d %Y\"`"); \
	      printf(" $(AUTHOR)\n"); \
	    } \
	  } else if ($$0~/^[A-Z]/) { printf("- %s\n", $$0); } else if ($$0~/./) { printf("  %s\n", $$0); } else {print} }' >> $(SPECFILE)
	grep -A2000 '!CHANGELOG!' $(SPECFILE).in | grep -v '!CHANGELOG!' >> $(SPECFILE)

ChangeLog:
	if test $(CVSREADACCESS) = yes; then \
	echo "`which --skip-dot cvs` \`echo \"\$$*\" | sed -e 's%1970%1990%'\`" > cvs; \
	chmod 755 cvs; \
	  here=`pwd`; \
	  (cd $(srcdir); PATH="$$here:$$PATH" rcs2log) > ChangeLog; \
	  rm cvs; \
	else \
	  touch ChangeLog; \
	fi

release: cvslog html which.lsm rpm NEWS
	install -m 644 $(REDHAT)/SRPMS/which-2.16-$(REL).src.rpm $(WWWDIR)
	install -m 644 $(REDHAT)/RPMS/$(TARGET)/which-2.16-$(REL).$(TARGET).rpm $(WWWDIR)
	install -m 644 $(REDHAT)/SOURCES/which-2.16.tar.gz $(WWWDIR)
	install -m 644 index.html $(WWWDIR)
	install -m 644 which.lsm $(WWWDIR)
	install -m 644 `ls cvslog-2.16*.html | grep -v cvslog-2.16_rpm` $(WWWDIR)
	#install -m 644 which*.html $(WWWDIR)
	install -m 644 NEWS $(WWWDIR)
	rm cvslog-2.16*.html
	cvs tag $(TAG)
	echo "$(TAG)" > .prevtag
	date +%s > .release_second

##-----------------------------------------------------------------------------
## `which' specific generation:

html: index.html.in configure.in
	grep -B2000 '^CHANGELOGLIST' index.html.in \
	    | sed -e 's%!VERSION!%2.16%g' \
	    | grep -v '^CHANGELOGLIST' > index.html
	( \
	  VER=`echo "2.16" | cut -d. -f1`; \
	  PATCHLEVEL=`echo "2.16" | cut -d. -f2`; \
	  while test "$$PATCHLEVEL" != "-1" ; do \
	    echo "<LI><A HREF="cvslog-$$VER.$$PATCHLEVEL.html">Version $$VER.$$PATCHLEVEL</A>" >> index.html; \
	    PATCHLEVEL=`echo "$$PATCHLEVEL" | awk '{ printf("%d", $$1 - 1); }'`; \
	  done \
	)
	grep -A2000 '^CHANGELOGLIST' index.html.in | grep -v '^CHANGELOGLIST' | grep -B2000 '^MANPAGE' | grep -v '^MANPAGE' >> index.html
	groff -man -Tascii which.1 | sed -e 's/.//g' | head -n 61 | tail -n 58 >> index.html
	groff -man -Tascii which.1 | sed -e 's/.//g' | head -n 123 | tail -n 51 | grep -B2000 '^ ' >> index.html
	grep -A2000 '^MANPAGE' index.html.in | grep -v '^MANPAGE' | grep -B2000 '^EXAMPLES' | grep -v '^EXAMPLES' >> index.html
	cat EXAMPLES >> index.html
	grep -A2000 '^EXAMPLES' index.html.in | grep -v '^EXAMPLES' >> index.html

EXAMPLES: which
	( cd test; whichtest check; ) || exit -1;
	( cd test; whichtest generate; )

README: which.1 README.in configure.in
	sed -e 's%!VERSION!%'`grep 's,@VERSION' config.status | cut -d, -f3`'%g' README.in > README
	groff -man -Tascii which.1 | sed -e 's/.//g' | head -n 61 | tail -n 58 >> README
	groff -man -Tascii which.1 | sed -e 's/.//g' | head -n 123 | tail -n 51 | grep -B2000 '^ ' >> README
