#
#	this makefile contains general rules and definitions for
#	producing formatted texts by automatic note insertion using kref.
#
#	local makefiles in each chapter or article directory provide
#	manuscript-specific information.
#
#	this makefile is called by the shell script "tmake".
#	
#	the basic steps are:
#	
#	(1)	check the text files for spelling errors;
#	
#	(2)	check the text files for non-terminated constructs such as
#		.NS and .NE, .QS and .QE, etc.
#	
#	(3)	scan the texts and insert citations matching key words given
#		on .bs lines;
#	
#	(4)	insert formatting commands for the matched citations;
#	
#	(5)	produce final formatted output.
#	
#	*	sorting is not necessary for the raw bibliography.
#		spelling checks on it are assumed to be managed
#		elsewhere.
#
KREF=kref
KAWK=kawk
#
#	.BS is interpreted as a font change by kroff.
#	the file "kroffkludge" contains sed script that
#	fixes the output to avoid font changes
#
SPLLOG=spelling.log
NOTLOG=note.log
USDBIB=usedbibl.tmp	 	#	bibl cards actually used
#	FMTBIB=					#	script to format bibliography
#	BIBORD=					#	sort script for bibliography
PRBLOG=problem.log
TXTLOG=textlist.log		#	list of source texts
NOTFND=notfound.log		#	sources not found.
USDFMT=usedbibl.fmt
TXTOUT=textbibl.tmp		#	final output.
SETUPS=setup			#	setup file.
KRFOPT=-a1 -u1 -s31		#	kref insert options
SRCTMP=insert.tmp		#	temp file with sources inserted
SRCFMT=format.tmp		#	temp file with formatted sources
#	FMTSRC=					#	script to format sources (notes)
TXTPRC=kroff			#	text processing program (final)
NFD=notfound.tmp
#	TXTLST=					#	tests to process.
#	BIBLST=					#	source bibliographies.
#
#	check for nested and not terminated constructs
#
$(NOTLOG): $(TXTLST)
	@echo "use noter on each file";
	@ex $?;
	@> $@ $(SPLLOG);
	@beepo $@
#
#	check for spelling errors (text only); because make will not
#	continue if a program it calls terminates abnormally, it is
#	necessary to write a script to do the spell check, perform
#	it, and then restart make.
#
#	[this may not be true of all versions of make]
#
SPLCMD=spelling.cmd
$(SPLLOG): $(TXTLST)
	echo 'krect -t $?; > $@ ; touch $(NOTLOG) $(SPLCMD)';
	echo 'use "sh $(SPLCMD)" to check spelling';
	sh -c "exit 69";	#	force a stop of make
	@beepo $@
#
#	insert citations into texts after appropriate spelling and
#	format command checks; create a log of items that could not
#	be matched.  kref also produces a file (??) containing only
#	the raw bibliographic entries for matched citations.
#
$(SRCTMP):: $(NOTLOG)
$(SRCTMP):: $(SPLLOG)
$(SRCTMP):: $(TXTLST) $(BIBLST)
	$(KREF) $(KRFOPT) -b $(BIBLST) -t $(TXTLST) > $@;
	-grep '^\.mo [AN]' $@ | tee $(NOTFND);
	tinsert $(NOTFND);
	@beepo $@
#
#	text with citations inserted is passed to kawk for additional
#	processing to add commands to format sources (notes)
#	according to an appropriate style sheet.
#
$(SRCFMT): $(FMTSRC) $(SRCTMP) $(KK)
	$(KAWK) -f $(FMTSRC) $(SRCTMP) > $@;
	@beepo $@
#
#	produce a formatted version of the text and bibliography.
#
$(TXTOUT): $(SRCFMT) $(USDBIB) $(SETUPS)
	-touch $(USDBIB);	#	in case there was no bibliography
	$(TXTPRC) -M $(SETUPS) $(SRCFMT) $(USDBIB) > $@;
	@beepo $@
#
#	make a bibliography of items used; $(USDBIB) is produced by
#	kref.  it must be sorted and formatted.
#
$(USDFMT): $(BIBLST) $(TXTLST) $(SPLLOG)
	-kord < $(BIBORD) -O $(USDBIB) $(USDBIB);
	-$(KAWK) -f $(FMTBIB) $(USDBIB) > $(USDFMT);
	@beepo $@
$(PRBLOG):	$(TXTLST)
	-$(KREF) -a19 -u2 -b $(BIBLST) -t $? > $@
	@beepo $@
$(TXTLOG):	$(TXTLST)
	@echo $(TXTLST) > $@
lists:
	echo "TXTLST $(TXTLST)
