# Examples makefile.
#
# Type "make" to generate examples.ps containing many examples.
# Type "make <diag>.ps" to make a postscript file from <diag>.m4
# Type "make <diag>.eps" to make an .eps file from <diag>.m4
# Type "make <diag>.pdf" to make a .pdf file from <diag>.m4
# Type "make <diag>.png" to make a .png file from <diag>.m4
#   (uses ImageMagick convert)
#
# To test the file test.m4, type "make tst" if boxdims is used, otherwise
# type "make tst1".  In either case the file tst.ps is created.
#
# To debug a single diagram in a directory other than the examples
# directory of the distribution, copy this makefile and tst.tex
# to where you want to work, redefine LIBDIR (below) appropriately,
# and type "make <diag>.xxx" where xxx is one of ps, eps, png, or pdf

# Circuit_macro directory, change this if necessary:
# LIBDIR = $$HOME/lib
LIBDIR = ..

# The dpic processor:
PIC = dpic -p
# PIC = $$HOME/dpic/test/dpic -p
# PIC = $$HOME/dpic/p2c/ftp/dpic -p
MODELIB = $(LIBDIR)/pstricks.m4

# Uncomment these for gpic.  Change gpic to pic if necessary on your machine:
# PIC = gpic -t
# MODELIB = $(LIBDIR)/gpic.m4

#########################################################################
# RSH = /bin/rsh
# RCP = /bin/rcp

# Better:
RSH = ssh
RCP = scp

# M4 = m4 -B32000
M4 = m4

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

DOC = examples
PICS = \
  AmpTable.tex Antennas.tex Audio.tex Bip.tex Btree.tex \
  CanLogic.tex CctTable.tex Csource.tex Decoder.tex Diodes.tex Drive.tex \
  Emarrows.tex Flow.tex Fuses.tex Globe.tex Grounds.tex I2L.tex Incleps.tex \
  Logic.tex Loglog.tex MC.tex NLG.tex Nport.tex Opamp.tex Relay.tex \
  ShiftR.tex Sierpinski.tex Sixpole.tex Sources.tex Switches.tex \
  TTLnand.tex Three.tex Timer.tex Variable.tex Windings.tex Xform.tex \
  bistable.tex blight.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 \
  gpar.tex graysurf.tex lcct.tex pwrsupply.tex quick.tex recycle.tex rose.tex \
  sfg.tex thyristor.tex ujt.tex worm.tex yinyang.tex

MANFIGS = AmpTable Antennas Audio Bip CctTable Diodes Emarrows Fuses \
  Grounds Logic NLG Nport Opamp Relay ShiftR Sources Switches Variable \
  Windings Xform bistable fet thyristor ujt

# Files that have to be processed twice:
BOXDIMS = ex09 Incleps Loglog Opamp Windings

DVIPSOPTS = -Ppdf -G0 -t letter

M4LIBS = $(MODELIB) $(LIBDIR)/libcct.m4

DIMFILE =

.SUFFIXES: .ps .dvi .tex .m4 .pic .eps .gif .png .pdf
.dvi.ps:
	dvips $(DVIPSOPTS) $* -o $*.ps
.m4.tex:
	@if test -n "$(DIMFILE)" ; then touch $(DIMFILE) ; fi
	$(M4) $(M4LIBS) $(DIMFILE) $*.m4 > $*.pic
	$(PIC) $*.pic > $*.tex
.m4.pic:
	@if test -n "$(DIMFILE)" ; then touch $(DIMFILE) ; fi
	$(M4) $(M4LIBS) $(DIMFILE) $*.m4 > $*.pic
.m4.dvi:
	make $*.tex
	cp $(LIBDIR)/examples/eps.src F$*.tex
	printf '\\input '$*  >> F$*.tex
#	printf '\n\\box\\graph\\end{TeXtoEPS}\\end{document}\n' >> F$*.tex
	printf '\n\\end{TeXtoEPS}\\end{document}\n' >> F$*.tex
	latex F$*
	touch F$*.dim $(DIMFILE)
	$(M4) $(M4LIBS) F$*.dim $(DIMFILE) $*.m4 | $(PIC) > $*.tex; latex F$*
	$(M4) $(M4LIBS) F$*.dim $(DIMFILE) $*.m4 | $(PIC) > $*.tex; latex F$*
.m4.eps:
	make $*.dvi
	dvips -Ppdf -G0 -E F$* -o $*.eps
.m4.gif:
	make $*.dvi
	dvips -x 1440 -Ppdf -G0 -E F$* -o $*.eps
	ps2gif $*.eps
.m4.ps:
	make $*.tex && mv $*.tex test.tex; latex tst
	rm -f test.tex test.pic
	make $*.tex && mv $*.tex test.tex; latex tst
	dvips $(DVIPSOPTS) tst -o $*.ps
.m4.png:
	make $*.eps
	convert -density 100 $*.eps $*.png ; mv $*.png.0 $*.png 
.m4.pdf:
	$(M4) $(LIBDIR)/pgf.m4 $(LIBDIR)/libcct.m4 $*.m4 > $*.pic
	dpic -g $*.pic > test.tex
	sed -e '/^\\usepackage.*graphicx/s/^.*$$/\\usepackage{tikz}/' \
        -e '/pstricks/d' \
      $(LIBDIR)/examples/tst.tex > F$*.tex
	pdflatex F$*
	$(M4) $(LIBDIR)/pgf.m4 $(LIBDIR)/libcct.m4 $*.m4 > $*.pic
	dpic -g $*.pic > test.tex
	pdflatex F$*
	mv F$*.pdf $*.pdf

# Places to look for gswin32 in lieu of gs
# GSDIRS = C:/Software "C:/Program*"
# GSOPTS = -q -dNOPAUSE -dBATCH -sDEVICE=png16 -g792x792
#.m4.png:
#	make $*.eps
#	@for dir in $(GSDIRS) ; do if test ! -d $$dir ; then true ; \
#      elif test -n "$$gss" ; then true ; \
#      else gss=`find $$dir -name gswin32.exe -print | tail -1` ; \
#      fi; done ; \
#   if test -z "$$gss" ; then gss=gs ; fi ; \
#   exec $$gss $(GSOPTS) -sOutputFile=$*.png $*.eps

$(DOC).ps: doclinks $(DOC).dvi

doclinks:
	-for nm in $(MANFIGS) ; do ln -s $(LIBDIR)/doc/$$nm.m4 ; done
	printf '%% quick.m4\n' > quick.m4
	cat $(LIBDIR)/doc/quick.m4 >> quick.m4
	touch doclinks

$(DOC).dvi: $(DOC).frs
	for file in $(BOXDIMS) ; do \
          touch $${file}.m4 $(DOC).dim ; \
          make PIC="$(PIC)" DIMFILE=$(DOC).dim $${file}.tex ; done
	latex $(DOC)
	touch $(DOC).dim
	for file in $(BOXDIMS) ; do \
          touch $${file}.m4 $(DOC).dim ; \
          make PIC="$(PIC)" DIMFILE=$(DOC).dim $${file}.tex ; done
	latex $(DOC)

$(DOC).frs: doclinks $(DOC).tex files.tex header.tex
	make $(PICS)
	cp $(LIBDIR)/doc/Version.tex .
	latex $(DOC)
	touch $(DOC).frs
	rm $(DOC).dvi

tst:
	make test.tex; latex tst; rm -f test.tex test.pic
	make DIMFILE=tst.dim test.tex; latex tst
	dvips $(DVIPSOPTS) tst -o tst.ps

tst1: test.tex tst.tex
	latex tst
	dvips $(DVIPSOPTS) tst -o tst.ps

tstl: test.tex tst.tex
	latex tst
	dvips $(DVIPSOPTS) tst -o tst.ps

gtst:
	touch test.m4; make PIC="gpic -t" MODELIB="" tst

gtst1:
	touch test.m4; make PIC="gpic -t" MODELIB="" tst1

# Distribution files
NAME=Circuit_macros
TMPDIR=/tmp/$(NAME)
REMOVED = examples/test examples/xfig/info m4s examples/htmlsource \
  examples/html examples/dist examples/zips examples/SCP doc/expl-m4.pdf

dist:
	@if test ! -s $(LIBDIR)/doc/CMman.pdf ; then \
         echo ; echo "Make CMman.pdf first!!"; echo ; exit 1 ; fi
	touch test.m4; cp test.m4 /tmp/test$$$$; rm test.m4; make test.m4
	( cd $(LIBDIR); mv doc/CMman.pdf . ; make clobber; mv CMman.pdf doc )
	make Copying doclinks
	make PIC="dpic -p" MODELIB="$(LIBDIR)/pstricks.m4" ; make clean
	(cd mf; make; make clean)
	(cd xfig; make; make clean)
	(cd $(LIBDIR)/doc; make ; make clean )
	-dir=`pwd` ; if [ "`basename $$dir`" = "examples" ] ; then \
         rm -f $(LIBDIR)/examples.tar.gz ; fi
	touch dist

zips: dist
	Version=`sed -e 's/Version //' $(LIBDIR)/doc/Version.tex` ; \
      PCVersion=`echo $$Version | sed -e 's/\./_/g'` ; \
      rm -rf $(TMPDIR)$$Version ; \
      mkdir $(TMPDIR)$$Version; cp -r $(LIBDIR)/* $(TMPDIR)$$Version ; \
      (cd $(TMPDIR)$$Version ; rm -rf $(REMOVED) ) ; \
      (cd $(LIBDIR)/doc; make clobber ) ; \
      cd /tmp; tar -cf - $(NAME)$$Version | gzip > $(NAME)$$Version.tar.gz ; \
      echo "\n$(TMPDIR)$$Version and $(TMPDIR)$$Version.tar.gz created\n";\
      (cd $(TMPDIR)$$Version; zip -r $(NAME) * ;\
      mv $(NAME).zip ../$(NAME)$$PCVersion.zip ) ; \
      echo "\n$(TMPDIR)$$PCVersion.zip created\n"
	touch zips

test.m4:
	echo \
"% Test.m4\n\
.PS\n\
# Use this file to do a quick test of diagrams you are developing.\n\
# Enter the drawing commands here, and (if you have a \"make\" utility)\n\
# type "make tst".\n\
# Otherwise, to process a file called filename.m4, type \"make filename.ps\".\n\
.PE" > test.m4

# To make but not distribute everything: make dist zips htmlfiles

ECEPROXY = eceweb
ECEDESTDIR = public_html/Circuit_macros

distece: dist zips htmlfiles
	Version=`sed -e 's/Version //' $(LIBDIR)/doc/Version.tex` ; \
    $(RCP) -p $(TMPDIR)$$Version.tar.gz \
      $(TMPDIR)$$Version/README $(TMPDIR)$$Version/CHANGES \
      $(ECEPROXY):$(ECEDESTDIR) ;\
    $(RSH) $(ECEPROXY) '(cd $(ECEDESTDIR); rm -f $(NAME).tar.gz; \
      ln -s $(NAME)'$$Version'.tar.gz $(NAME).tar.gz )' ;\
	echo "\n/tmp/$(NAME)$$Version.tar.gz copied to ${ECEPROXY}" ;\
    PCVersion=`echo $$Version | sed -e 's/\./_/g'` ; \
    $(RCP) -p $(TMPDIR)$$PCVersion.zip $(ECEPROXY):$(ECEDESTDIR) ;\
    $(RSH) $(ECEPROXY) '(cd $(ECEDESTDIR); rm -f $(NAME).zip; \
      ln -s $(NAME)'$$PCVersion'.zip $(NAME).zip )' ;\
	echo "\n/tmp/$(NAME)$$PCVersion.zip copied to ${ECEPROXY}"
	make disthtml

distfile: dist zips htmlfiles
	rm -rf distfile; mkdir distfile
	Version=`sed -e 's/Version //' $(LIBDIR)/doc/Version.tex` ; \
    cp -p $(TMPDIR)$$Version.tar.gz \
      $(TMPDIR)$$Version/README $(TMPDIR)$$Version/CHANGES \
      distfile ;\
    PCVersion=`echo $$Version | sed -e 's/\./_/g'` ; \
    cp -p $(TMPDIR)$$PCVersion.zip distfile ;\
    mkdir distfile/html
	cp -rp html/* distfile/html
	(cd distfile/html; mv index.html .. ; rm -f access.log *.pl )
	rm -f distfile.tar.gz; tarlib distfile

EPSFILES=${PICS:%.tex=%.eps}
# Make an encapsulated Postscript file for each example (requires pstricks)
eps: doclinks
	make ${PICS:%.tex=%.eps}

CFILES = $(LIBDIR)/darrow.m4 $(LIBDIR)/doc/Copying Copying mf/Copying \
  mpost/Copying pdflatex/Copying xfig/Copying \
  $(LIBDIR)/gpic.m4 $(LIBDIR)/lib3D.m4 $(LIBDIR)/libcct.m4 \
  $(LIBDIR)/libgen.m4 $(LIBDIR)/liblog.m4 $(LIBDIR)/mfpic.m4 \
  $(LIBDIR)/mpost.m4 $(LIBDIR)/pgf.m4 $(LIBDIR)/postscript.m4 \
  $(LIBDIR)/pstricks.m4 $(LIBDIR)/README $(LIBDIR)/xfig.m4

Copying: $(LIBDIR)/doc/Version.tex
	sed -e "s%Version  *[0-9.]*,%`cat $(LIBDIR)/doc/Version.tex`,%" $(LIBDIR)/Copying > xxx
	sed -e "s%opyright (c) [^ ][^ ]* J%opyright (c) `date '+%Y'` J%" xxx \
          > $(LIBDIR)/Copying ; rm -f xxx
	for file in $(CFILES) ; do \
      if test "`basename $$file`" = "Copying" ; then \
        cp $(LIBDIR)/Copying $$file ; \
      else \
        ( sed -e '/opyright (c)/,$$d' $$file ; \
          cat $(LIBDIR)/Copying ; echo "" ; \
          sed -e '1,/opyright (c)/d' $$file | sed -e '1,/^ *$$/d' ) > xxx ; \
	      sed -e "s%Version  *[0-9.]*:%`cat $(LIBDIR)/doc/Version.tex`:%" xxx \
          > $$file ; \
      fi ; done

PERLFILE = Circuit_macros_access_log
disthtml: htmlfiles
	$(RSH) $(ECEPROXY) '(rm -rf $(ECEDESTDIR)/html ; mkdir $(ECEDESTDIR)/html )'
	$(RCP) -rp html/* $(ECEPROXY):$(ECEDESTDIR)/html
	$(RSH) $(ECEPROXY) \
      '(cd $(ECEDESTDIR)/html; mv index.html $(LIBDIR) ; \
        mv $(PERLFILE).pl $(LIBDIR)/$(LIBDIR)/cgi-bin/$(PERLFILE).pl )'
	$(RSH) $(ECEPROXY) \
    '(cd $(ECEDESTDIR)/html; cat access.log >>$(LIBDIR)/access.log; rm -f access.log )'
	echo "\nhtml copied to ${ECEPROXY}"

htmlfiles: html pngs html/examples.html html/index.html html/access.log
	(cd pgf; make examples.pdf; chmod 644 examples.pdf; mv examples.pdf .. )
	mv examples.pdf html/examples.pdf
	touch htmlfiles

html:
	if test ! -d html ; then mkdir html ; fi
	( cd htmlsource; cp -p $(PERLFILE).pl .htaccess $(LIBDIR)/html )

html/access.log:
	touch html/access.log

html/index.html: htmlsource/index.htmx html
	Version=`sed -e 's/Version //' $(LIBDIR)/doc/Version.tex` ; \
      sed -e 's/Version XXX/Version '$$Version'/' htmlsource/index.htmx \
      > html/index.html

html/examples.html: html htmlsource/examples.htmx files.tex
	Version=`sed -e 's/Version //' $(LIBDIR)/doc/Version.tex` ; \
      sed -e 's/Version XXX/Version '$$Version'/' htmlsource/examples.htmx \
      > html/examples.html
	cat files.tex | sed -e '/bfig[\{]/d' -e '/^ *[\}][\}]* *$$/d' \
     -e '/src[\{]/d' -e '/\\if/d' -e '/\\fi/d' -e '/includegraphics/d' \
     -e '/\%ignore\%/d' -e '/^\%/d' -e '/\\end[\{]/d' -e '/^\%/d' \
     -e '/getpic/s/\\getpic[\{]\([^\}]*\)[\}][\}]*/ <\1> /g' \
     -e '/<[^-]/s/^[^<]*<//' -e '/<[^-]/s/>[^<]*</> /g' -e '/[^-]>/s/>.*$$/>/' \
     -e 's/\([^ -][^ -]*\)>/<P><IMG SRC="\1.png"><BR><A HREF="\1.m4" T_\1.m4Y/'\
     -e 's/T_/TARGET="_blank"><P>/' -e 's/4Y/4<\/A>:/' \
     -e 's/^.*caption[\{]//' -e '/^ *$$/s/^ */<HR>/' \
      | sed \
      -e '/<P>/!s/{\\tt \([^}]*\)}/<tt>\1<\/tt>/g' \
      -e '/<P>/!s/{\\bf \([^}]*\)}/<tt>\1<\/tt>/g' \
      -e '/<P>/!s/{\\em \([^}]*\)}/<em>\1<\/em>/g' \
      -e '/<P>/!s/{\\it \([^}]*\)}/<em>\1<\/em>/g' \
      -e '/<P>/!s/{\\sl \([^}]*\)}/<em>\1<\/em>/g' \
      -e '/<P>/!s%\([^\\]\)_\(.\)%\1<sub>\2</sub>%g' \
      -e '/<P>/!s%\([^\\]\)\^\(.\)%\1<sup>\2</sup>%g' \
      -e 's%``%"%g' -e "s%''%\"%g" -e 's/\~/ /g' \
      -e 's%\\%%g'  -e 's/\$$//g' \
      | sed -e 's/HREF=Inclpdf.m4/HREF=Inclpdf.txt/' \
      >> html/examples.html
	cat htmlsource/endexamples.htmx >> html/examples.html

pngs: files.tex doclinks
	grep '\\getpic' files.tex | sed -e '/Inclpdf/d' \
      -e 's%\\getpic[\{]\([^\}]*\)[\}]% <\1> %g' \
      -e 's/^[^<]*<//' -e 's/>[^<]*</ /g' -e 's/>.*$$//' >pngs
	-for nm in `cat pngs` ; do \
      make $$nm.png ; mv $$nm.png html/$$nm.png ;\
      done
	make blight.png ; mv blight.png html/blight.png
	-for nm in `cat pngs` ; do\
      cp $$nm.m4 html ; done
	cp Inclpdf.m4 html/Inclpdf.txt
	echo "" > xxx
	cat Inclpdf.m4 >> xxx
	chmod 644 xxx
	mv xxx html/Inclpdf.m4

gifs: files.tex doclinks
	grep '\\getpic' files.tex | sed -e '/Inclpdf/d' \
      -e 's%\\getpic[\{]\([^\}]*\)[\}]% <\1> %g' \
      -e 's/^[^<]*<//' -e 's/>[^<]*</ /g' -e 's/>.*$$//' >gifs
	-for nm in `cat gifs` ; do \
      make $$nm.gif ; mv $$nm.gif html ;\
      done
	make blight.gif ; mv blight.gif html
	-for nm in `cat gifs` ; do\
      cp $$nm.m4 html ; done
	cp Inclpdf.m4 html/Inclpdf.txt
	echo "" > xxx
	cat Inclpdf.m4 >> xxx
	chmod 644 xxx
	mv xxx html/Inclpdf.m4

# Done separately to avoid the -x 1440 enlargement
lcct.gif:
	make $*.eps
	ps2gif $*.eps

# Experimental:
RPICS = rotatetext.tex rotellipse.tex
rotate:
	make DOC=rotate PICS='$(RPICS)' BOXDIMS='rotatetext rotellipse'
	touch rotatetext.m4 rotellipse.m4
	make DOC=rotate PICS='$(RPICS)' BOXDIMS='rotatetext rotellipse'

clobber:	clean
	rm -f *.ps
	-@for dir in mf mpost pdflatex xfig psfrag pgf ; \
      do (if test -d $$dir ; then cd $$dir; make clobber; fi) ; done
	rm -f *.warn quick.m4
	rm -rf html gifs pngs *.png* dist zips htmlfiles

clean:
	rm -f *.dvi *.aux *.log *.toc *.blg core snapshots *.eps eps *-mfpic*
	rm -f fig*.tex $(PICS) *.roff *.int *.pic gpic.m4 test.tex xxx* *.fig
	rm -f *.dim *.bak *.frs $(RPICS) rotate.ps tst.ps *.bbl *-pics.ps *.gif
	rm -f *.tfm *[0-9]pk quick.tex quick.pic *.png *.1
	rm -f F*.tex Version.tex doclinks
	for nm in $(MANFIGS) ; do rm -f $$nm.* ; done
	rm -rf distfile distfile.tar.gz