##########################################################################
#
#  All latex stuff is in .tip files (tex input). If we're formatting chapter
# by chapter, you'll say 'make chapter.ps', which will look for chapter.tex,
# which it will generate by running the .tip though the filter, and then
# embedding it in a dummy document
#
# If you're building the book, however, it uses 'getDepends' to extract
# the \input's from the book source. These all are called chapter.txi,
# so the filtering gets done but the result is not embedded.
#
##########################################################################

UTIL   = ../util

# Command to remove stupid logging from latex

CLEANUP=| $(UTIL)/tidyop.pl

# Latex environment

LTXENV = TEXINPUTS=:latex/{crop,titlesec,.}

# And where to find the Bibliography

BIBENV = BIBINPUTS=.

# Latex command

LATEX = latex
PDFLATEX = pdflatex

# Take a .tip and produce a .tex

RUBY = /tc/usr/ruby161/bin/ruby

FILTER = $(RUBY) -I $(UTIL) $(UTIL)/code.rb -name $< | pic -t

%.txi:		%.tip $(UTIL)/code.rb
		cat $< | $(FILTER) >$@

%.tex:		%.tip $(UTIL)/code.rb
		cat $< | $(FILTER) | \
			cat dummyfront.tip - end.tip >$@

%.dvi:		%.tex *.sty
		@-$(LTXENV) $(LATEX) $* $(CLEANUP)
		@-echo "Building bibliography"
		@-$(BIBENV) bibtex $*
		@-echo "Reformatting to include bibliography"
		@-$(LTXENV) $(LATEX) $* >/dev/null
		@-if [ -f $*.idx ];			\
		then 					\
	          $(RUBY) -i.bak $(UTIL)/tidyIndex.rb $*.idx; 	\
	          cp $*.idx $*.idd;			\
		  makeindex -s rubybook.ist $*.idx;	\
		fi
		@-$(LTXENV) $(LATEX) $* $(CLEANUP)

%.ps:		%.dvi
		dvips -q -f <$*.dvi >$*.ps

%.ps1200:	%.dvi
		dvips -mode vtftzzlo -D 1200 -q -f <$*.dvi >$*.ps1200

%.pdf:		%.tex
		@-$(LTXENV) $(PDFLATEX) $* $(CLEANUP)
		@-echo "Building bibliography"
		@-$(BIBENV) bibtex $*
		@-echo "Reformatting to include bibliography"
		@-$(LTXENV) $(PDFLATEX) $* >/dev/null
		@-makeindex -s rubybook.ist $*.idx
		@-$(LTXENV) $(PDFLATEX) $* $(CLEANUP)


# Book dependencies

BDEPENDS:=	ext_ruby.tex exception.tex $(shell $(UTIL)/getDepends.rb book.tip builtins.tip)

book:		samples ext_ruby.tex tempfile.ps book.ps

# Shipping

.PHONY:		ship_dir ship

ship_dir:
		[ -d ship ] || mkdir ship

ship:		book.ps1200  ship_dir
		perl -I$(UTIL) $(UTIL)/splitup.pl book.ps1200 ship
		ls -l ship


#
# Special processing for individual chapters
#

builtins.txi builtins.tex:			\
			classlist.tex modulelist.tex \
			$(patsubst %.tip, %.txi, $(wildcard ref_c_*.tip)) \
			$(patsubst %.tip, %.txi, $(wildcard ref_m_*.tip))

lib_standard.txi lib_standard.tex:		\
			lib_complex.txi		\
			lib_date2.txi		\
			lib_english.txi		\
			lib_find.txi		\
			lib_ftools.txi		\
			lib_getoptlong.txi 	\
			lib_mkmf.txi		\
			lib_parsedate.txi	\
			lib_profile.txi		\
			lib_pstore.txi		\
			lib_tempfile.txi	\
			lib_thread.txi		\
			lib_timeout.txi		\
			lib_weakref.txi

lib_network.txi lib_network.tex:		\
			lib_socket.txi 		\
			lib_net_ftp.txi		\
			lib_net_http.txi	\
			lib_net_pop.txi		\
			lib_net_smtp.txi	\
			lib_net_telnet.txi	\
			lib_cgi.txi		\
			lib_cgi_session.txi

lib_patterns.txi lib_patterns.tex:				\
			lib_delegate.txi	\
			lib_observable.txi	\
			lib_singleton.txi

lib_windows.txi lib_windows.tex:		\
			ext_c_win32ole.txi	\
			ext_c_win32ole_event.txi\
			ext_c_winapi.txi

rdtool.tex rdtool.txi:	tempfile.ps

tempfile.tex:		code/tempfile.rd
			$(RUBY)  -I $(UTIL) $(UTIL)/rd2 -rrd/rd2latex-lib.rb \
				code/tempfile.rd >tempfile.tex

#
# Preprocess a .doc file to a .tex
#

book.tex:	*.tip $(UTIL)/code.rb samples  $(BDEPENDS)
		cat $< | $(FILTER) >$@

classlist.tex:	ref_c_*.tip  $(UTIL)/genLibIndex.rb
		$(RUBY) $(UTIL)/genLibIndex.rb ref_c*.tip >classlist.tex

modulelist.tex:	ref_m_*.tip $(UTIL)/genLibIndex.rb
		$(RUBY) $(UTIL)/genLibIndex.rb ref_m*.tip >modulelist.tex

exception.tex:  exception.lst
		pic -t <exception.lst >exception.tex


####################

clean:
		rm -f *.ps *.dvi *.log *.idx *.glx *.gxg *.gxs *.ilg *.toc \
			*.ind *.bbl *.blg *.aux *.txi *.tex *.idd 	\
			*.lof *.lot f1 f2 newfile out testfile1 posterity *~ 
		rm -rf testdir
		rm -rf tests
		rm -f classlist.tex modulelist.tex
		cd code/cdjukebox; make clean
		cd code/ext ; make clean

.PHONY:		samples

samples:
		[ -d testdir ]          || mkdir testdir
		[ -f testdir/config.h ] || touch testdir/config.h
		[ -f testdir/main.rb ]  || touch testdir/main.rb

.PHONY:		basetests Testdir

ext_ruby.tex::	ext_ruby.tip
		cd code/cdjukebox && make
		cd code/ext; \
		$(RUBY) extconf2.rb -v --with-cdjukebox-include=../cdjukebox \
				    --with-cdjukebox-lib=../cdjukebox && \
		make

check: check_alpha check_spell check_ccm

check_alpha:
	@echo "Checking for alphabetic order of all methods..."
	$(RUBY) $(UTIL)/alpha.rb

check_spell:
	@echo "Spellchecking all tips..."
	perl $(UTIL)/spellall.pl *.tip

check_ccm:
	@echo "Checking for correct macro use of method types..."
	$(RUBY) $(UTIL)/verifyCCM.rb *.tip

