#							vim: ft=make
# A makefile to 
#   - process ChemDraw figures into eps (or pdf) files, for inclusion in 
#       LaTeX documents.
#   - emulate LaTeXit
# 
# H.A.Trujillo,
# Wilkes University
#
# 19-28 May 2013
# Last Change:  5 Aug 2014


VPATH = /tmp
.PHONY: help

help:
	@ printf "\n\
    1. Converts ChemDraw output to an eps (or pdf) file for LaTeX.\n\n\
		Usage:   1. save CD output as \`/tmp/foo.ps\'\n\
			 2. make target    (from this directory)\n\
			 3. mv \`foo.eps\' to desired name\n\n\
    2. Processes equations in LaTeX similarly to LaTeXiT.\n\n\
		Usage:   1. save LaTeX file as \`/tmp/foo.ltx\'\n\
			 2. make eqn\n\n\n\
	    Targets:\n\
		chm 	old ChemDraw, [4mwithout[m Superpaint figures\n\
		chm+	old ChemDraw, [4mwith[m    Superpaint figures\n\
		cdx 	new ChemDraw\n\n\
		pdf	generate a cropped pdf file\n\
			   --> combine this with one of the options above,\n\
			           eg:   make chm+ pdf\n\n\
		spt	raw superpaint images\n\n\
		eqn	A replacement for LaTeXit: \n\
			    Converts "foo.ltx" into a new-ChemDraw pdf object.\n\
			      nb/ - Best to set default bond-length of CD file into\n\
			            which drawing will be pasted to 10 pt, lest CD\n\
				    rescale the pasted eqn.\n\
				  - Close ChemDraw window before reprocessing.\n\n\
		help	print this message\n\n"
	
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

# Old ChemDraw
# 	for figures containing only ChemDraw content.
chm: foo.ps
	tr \\r \\n  <$^  >/tmp/bar.ps
	ps2eps -l /tmp/bar.ps
	mv /tmp/bar.eps ./foo.eps
#	rm /tmp/foo.ps

chm-: chm


# Old ChemDraw, with intermediate ps2ps processing
#      nb/ this generates a smaller eps files for figs containing SuperPaint
#          drawings, but larger for figs containing nothing but ChemDraw.
chm+: foo.ps
	tr \\r \\n  <$^  >/tmp/bar.ps
	ps2ps /tmp/bar.ps /tmp/zap.ps
	ps2eps -l /tmp/zap.ps
	mv /tmp/zap.eps ./foo.eps
#	rm /tmp/foo.ps


# New ChemDraw
cdx: foo.ps
	ps2ps $< /tmp/bar.ps
	ps2eps -l /tmp/bar.ps
	mv /tmp/bar.eps ./foo.eps
#	rm /tmp/foo.ps

# Superpaint alone
spt:  chm+

# PDF files
pdf: foo.eps
	ps2pdf -dEPSCrop $<
	@ printf "\n   Output saved as foo.pdf\n"
	@ rm $<

# LaTeXiT replacement -- resulting ChemDraw files are much smaller when
# 			 this is used with CD-13 than when LaTeXit is used.
#   Caveat w/ CD-for-OsX:
#   		CD scales pasted objects, so must compensate.
#		(foo.pdf opens at 30 bondlength)

/tmp/foo.dvi: /tmp/foo.ltx
	cd /tmp ; latex /tmp/foo.ltx

eqn: /tmp/foo.dvi
	dvips -o /tmp/foo.ps /tmp/foo 
	ps2eps -l /tmp/foo.ps
	ps2pdf -dEPSCrop -o/tmp/foo.pdf /tmp/foo.eps
	open -a"ChemDraw 13.0.app" /tmp/foo.pdf
	rm /tmp/foo.{ps,eps,dvi,log,aux} 

