# Makefile for building UnixCenter.RU materials
# Please install modified todos package first
# Which input charset is used? 
CHARDCL=/usr/share/sgml/koi8r.dcl
#PREFIX='<osfile encoding=iso-8859-5>'
PREFIX='<osfile bctf=identity>'
# What DSSSL stylesheet to use
STYLE=ru-test
# What is the main SGML file
MAIN=ru-test



all: 	cvs ps pdf rtf html

cvs:	eps2gif gif2eps eps2pdf

ps: cvs 
	if [ -d ps ] ; then rm -rf ps; fi
	mkdir ps
	cp *.sgml ps
	cat ${CHARDCL} ${STYLE}.dsl > ps/${STYLE}.dsl
	cp jadetex.cfg ps
	cd ps;ln -s ../images images
	
	cd ps; jade -V %graphic-default-extension%=eps -V tex-backend -t tex\
	            -d ${PREFIX}${STYLE}.dsl#print ${CHARDCL} ${PREFIX}${MAIN}.sgml
	
	cd ps; jadetex ${MAIN};exit 0
	cd ps;if egrep '^LaTeX Warning: There were undefined references.' ${MAIN}.log >/dev/null 2>&1; then jadetex ${MAIN}; jadetex ${MAIN}; fi; exit 0
	cd ps;dvips -j0 ${MAIN}.dvi -o ${MAIN}.ps
	cp -f ps/${MAIN}.ps ./
	rm -rf ps

pdf: cvs 
	if [ -d pdf ] ; then rm -rf pdf; fi
	mkdir pdf
	cp *.sgml pdf
	cat ${CHARDCL} ${STYLE}.dsl > pdf/${STYLE}.dsl
	cp jadetex.cfg pdf
	cd pdf;ln -s ../images images
	
	cd pdf;jade -V %graphic-default-extension%=pdf -t tex \
	            -d ${PREFIX}${STYLE}.dsl#print ${CHARDCL} ${PREFIX}${MAIN}.sgml
	
	cd pdf;pdfjadetex ${MAIN};exit 0
	cd pdf;if egrep '^LaTeX Warning: There were undefined references.' ${MAIN}.log >/dev/null 2>&1; then pdfjadetex ${MAIN}; pdfjadetex ${MAIN}; fi; exit 0
	cp -f pdf/${MAIN}.pdf ./
	rm -rf pdf

pdf2ps:
	acroread -toPostScript -pairs pdf/${MAIN}.pdf pdf/${MAIN}.ps

rtf: cvs
	if [ -d rtf ] ; then rm -rf rtf; fi
	mkdir rtf
	cp *.sgml rtf
	cat ${CHARDCL} ${STYLE}.dsl > rtf/${STYLE}.dsl
	cd rtf;mkdir images; cp -f ../images/*.gif images
	cd rtf;jade -V %graphic-default-extension%="gif" -t rtf \
	            -d ${PREFIX}${STYLE}.dsl#print ${CHARDCL} ${PREFIX}${MAIN}.sgml
	cat rtf/${MAIN}.rtf|sed -e s/charset0/charset204/ |sed -e s/\ CYR\;/\;/>rtf/1.tmp
	mv -f rtf/1.tmp ${MAIN}.rtf
	rm -rf rtf 

html: cvs
	if [ -d html ] ; then rm -rf html; fi
	mkdir html
	cp *.sgml html
	cat ${CHARDCL} ${STYLE}.dsl > html/${STYLE}.dsl
	cd html;mkdir images; cp -f ../images/*.gif images
	cd html; jade -V %graphic-default-extension%=gif -t sgml -i html \
	              -d ${PREFIX}${STYLE}.dsl#html ${CHARDCL} ${PREFIX}${MAIN}.sgml
	rm -f html/libs html/*.sgml html/*.dsl html/*.ent

gif2eps:
	cd images; for ff in *.gif;do bn=`echo $$ff | sed -e s/.gif$///`; if [ ! -f $$bn.eps ] ; then echo -n "Converting $$ff to EPS ... "; giftopnm $$ff > /tmp/$$bn.pnm;pnmtops -noturn -dpi 100 -scale 1 -width 5 -height 6 /tmp/$$bn.pnm >$$bn.eps; rm /tmp/$$bn.pnm; echo " Ok"; fi; done

eps2gif:
	cd images; for ff in *.eps;do bn=`echo $$ff | sed -e s/.eps$///`; if [ ! -f $$bn.gif ] ; then echo -n "Converting $$ff to GIF ... "; convert -density 100x100 $$ff $$bn.gif; echo " Ok"; fi; done

eps2pdf:
	cd images; for ff in *.eps;do bn=`echo $$ff | sed -e s/.eps$///`; if [ ! -f $$bn.pdf ] ; then echo -n "Converting $$ff to PDF ... "; epstopdf $$ff; echo " Ok"; fi; done

clean:
	if [ -d rtf ] ; then rm -rf rtf; fi
	if [ -d html ] ; then rm -rf html; fi
	if [ -d pdf ] ; then rm -rf pdf; fi
