# Makefile for the pgf subdirectory

# Type "make" to produce the examples.ps file
# Type "make examples.pdf" to invoke pdflatex
# Type "make tst" to process the file test.m4 and produce tst.ps
# Type "make tst.pdf" to process the file test.m4 and produce tst.pdf

# Where the .m4 libraries are installed.  Change if necessary:
LIBDIR = $$HOME/lib

#---------------------------------------------

# The .m4 circuit_macro libraries used for the examples:
M4MACROS = pgf.m4
M4 = m4 -I $(LIBDIR)

#---------------------------------------------
DOC = examples

FIGS = Adder.tex Alogix.tex ABlogix.tex Antennas.tex Audio.tex AmpTable.tex \
  Bip.tex Btree.tex Byte.tex \
  CanLogic.tex CctTable.tex Conn.tex Counting.tex Crossbar.tex Crow.tex \
  Csource.tex \
  Decoder.tex Demultiplexer.tex Diodes.tex Drive.tex \
  Emarrows.tex Flow.tex Fuses.tex \
  Globe.tex Grounds.tex Headers.tex Heathkit.tex Jack.tex \
  I2L.tex Incleps.tex \
  Logic.tex Loglog.tex MC.tex MotorControl.tex Multiplexer.tex \
  NLG.tex NPDT.tex Nport.tex \
  Opamp.tex Optoiso.tex Pconn.tex Rectifiers.tex Relay.tex Rotbox.tex \
  Schottky.tex Sierpinski.tex ShiftR.tex Sixpole.tex Sources.tex SQUID.tex \
  Switches.tex \
  Tgate.tex Three.tex Timer.tex TTLnand.tex \
  Windings.tex Variable.tex Xform.tex \
  bistable.tex control.tex csc.tex diamond.tex \
  ex00.tex ex01.tex ex02.tex ex03.tex ex04.tex \
  ex05.tex ex06.tex ex07.tex ex08.tex ex09.tex ex10.tex ex11.tex ex12.tex \
  ex15.tex ex16.tex ex17.tex ex18.tex ex19.tex ex21.tex exp.tex \
  fet.tex graysurf.tex lcct.tex pwrsupply.tex quick.tex \
  recycle.tex rose.tex sfg.tex thyristor.tex ujt.tex worm.tex yinyang.tex

# Files using boxdims macros:
BOXDIMS = ex09 Crow Incleps Loglog TTLnand Opamp Windings

# It is advisable to do `make clean' between test examples.
#---------------------------------------------

DPIC = dpic
# DPIC = $$HOME/dpic/test/dpic

TR = | tr '=' '\134'

.SUFFIXES: .tex .m4 .eps .ps .dim .pdf
.tex.ps:
	latex $*
	dvips -Ppdf $*
.m4.tex:
	$(M4) $(M4MACROS) $*.m4 | $(DPIC) -g > $*.tex
#	$(M4) $(M4MACROS) $*.m4 > $*.pic
#	$(DPIC) -g $*.pic > $*.tex
.m4.pdf:
	$(M4) $(M4MACROS) $*.m4 | $(DPIC) -g > test.tex
	pdflatex tst
	mv tst.pdf $*.pdf
.m4.dim:
	printf '\\documentclass[11pt]{article}\n' > F$*.tex
	printf '\usepackage{times,boxdims,tikz}\n' >> F$*.tex
	printf '\\begin{document}\\input '$*  >> F$*.tex
	printf '\n\\end{document}\n' >> F$*.tex
	make $*.tex
	touch F$*.dim
	$(M4) $(M4MACROS) F$*.dim $*.m4 | $(DPIC) -g > $*.tex; latex F$*
	$(M4) $(M4MACROS) F$*.dim $*.m4 | $(DPIC) -g > $*.tex; latex F$*

EXL = exlinks
$(DOC).ps: $(DOC).dvi
	dvips -Ppdf $(DOC)

$(DOC).dvi: Incl.eps.gz $(DOC).tex
	-if test "$(DOC)"="examples" ; then make exlinks ; fi
	make dims $(FIGS)
	latex $(DOC)

dims:
	-if test -n "$(BOXDIMS)" ; then \
       for box in $(BOXDIMS) ; do touch F$$box.dim; make $$box.dim ; done; fi
	touch dims

$(DOC).pdf: Incl.pdf $(DOC).tex
	-if test "$(DOC)"="examples" ; then make exlinks ; fi
	-if test -n "$(BOXDIMS)" ; then \
       for box in $(BOXDIMS) ; do touch F$$box.dim; make $$box.dim ; done; fi
	make $(FIGS)
	pdflatex $(DOC)

Incl.eps.gz: ../Incl.eps.gz ../Incl.eps.bb
	cp ../Incl.eps.gz .
	cp ../Incl.eps.bb .

Incl.pdf: ../Incl.pdf
	cp ../Incl.pdf .

figs: $(EXL) $(FIGS)

exlinks: Incl.eps.gz
	@echo ""
	@echo 'Ignore "cannot create ... exists" warnings.'
	-@if test ! -f ex11.m4 ; then \
      ls ../*.m4 | sed -e 's/^/ln -s /' -e '/test.m4/d' -e '/quick.m4/d' | sh;\
      ls ../../doc/*.m4 ../*.gz | sed -e 's/^/ln -s /' | sh;\
      fi
	touch $(EXL)

clobber: clean
	rm -f $(DOC).ps test.m4 *.pdf

tst.ps: tst
tst: test.m4
	touch tst.dim
	$(M4) pgf.m4 libcct.m4 tst.dim test.m4 \
           > test.pic
	$(DPIC) -g test.pic > test.tex
	latex tst
	$(M4) pgf.m4 libcct.m4 tst.dim test.m4 \
           > test.pic
	$(DPIC) -g test.pic > test.tex
	latex tst
	dvips tst

tst.pdf: test.m4
	touch tst.dim
	$(M4) pgf.m4 libcct.m4 tst.dim test.m4 \
           > test.pic
	$(DPIC) -g test.pic > test.tex
	pdflatex tst
	$(M4) pgf.m4 libcct.m4 tst.dim test.m4 \
           > test.pic
	$(DPIC) -g test.pic > test.tex
	pdflatex tst

test.tex: test.m4
	touch tst.dim
	$(M4) pgf.m4 libcct.m4 tst.dim test.m4 > test.pic
	$(DPIC) -g test.pic > test.tex

clean:
	rm -f stamp-* $(FIGS) *.dvi *.bb *.gz tst.ps tst.pdf
	rm -f *.out *.pfg Incl.* *.pic
	-if /usr/bin/test -f exlinks ; then \
     ls -l *.m4 | grep '\->' | \
       sed -e 's/^.* \([^ ][^ ]*\) ->.*$$/rm -f \1/' | sh; fi
	rm -f *.log *.ps *.aux *.mp *.[0-9] *.[0-9][0-9] *.mpx *.eps *.dim
	rm -f core log mpxerr* *links figs.* *.dim dims Ftest.* setdim.tex
	rm -f man[0-9]*.pic man[0-9]*.m4 figs.m4 test.1 test.mp test.pic test.tex
	-if test -n "$(BOXDIMS)" ; then for box in $(BOXDIMS) ; do \
      rm -f F$$box.tex $$box.tex ; done; fi
