# This won't work in general since it uses GNU extensions of various utils.

version=4.5.1

distdir=cjk-$(version)
abs-distdir=$(shell cd $(distdir); pwd)

DISTDIRS=$(shell find . -type d | grep -v CVS | grep -v $(distdir))

# expected: subdirs `dvi' and `cjk'
CJK2DVI=\
  (cd dvi; \
   TEXINPUTS=$(abs-distdir)/texinput//:$(abs-distdir)/contrib/wadalab: \
     texi2dvi -l latex ../cjk/$@.cjk; \
   rm -f $@.aux $@.log $@.toc)

# LATEX2DVI srcdir targetdir
# expected: targetdir/dvi
LATEX2DVI=\
  cp $(1)/$@.tex $(2)/dvi; \
  (cd $(2)/dvi; \
   TEXINPUTS=$(abs-distdir)/texinput//:$(abs-distdir)/contrib/wadalab: \
     texi2dvi -l latex $@.tex; \
   rm -f $@.tex $@.aux $@.log $@.toc)

# expected: subdir `ps' and `dvi'
DVI2PS=\
  dvips -P wadalab -P wansong -P cmz -o ps/$@.ps dvi/$@.dvi
  

# CONVERT converter srcdir targetdir
CONVERT=\
  cd $(distdir); \
  $(1) < $(2)/$@.tex > $(3)/cjk/$@.cjk; \
  cd $(3); \
  $(CJK2DVI); \
  $(DVI2PS)

# MULE srcdir targetdir
MULE=\
  cd $(distdir); \
  emacs --batch -l cjk-enc -f batch-cjk-write-file $(1)/$@.tex; \
  mv $(1)/$@.cjk $(2)/cjk; \
  cd $(2); \
  $(CJK2DVI); \
  $(DVI2PS)

# LATEX srcdir targetdir
LATEX=\
  cd $(distdir); \
  $(call LATEX2DVI,$(1),$(2)); \
  cd $(2); \
  $(DVI2PS)


DISTTARGETS=\
  READMEb5 \
  READMEgb \
  Big5 \
  Big5vert \
  CEF_test \
  CJKbabel \
  GB \
  JIS \
  KS \
  SJIS \
  UTF8 \
  muletest \
  pytest \
  py_test \
  rubytest \
  thai


all:
	@echo
	@echo "    Say \`make dist' to build the tarballs."
	@echo


READMEb5:
	$(call CONVERT,bg5conv,doc/chinese,doc)
READMEgb:
	$(call LATEX,doc/chinese,doc)
Big5 Big5vert:
	$(call CONVERT,bg5conv,examples,examples)
CEF_test:
	$(call CONVERT,cefconv,examples,examples)
SJIS:
	$(call CONVERT,sjisconv,examples,examples)
CJKbabel muletest rubytest thai:
	$(call MULE,examples,examples)
GB JIS KS UTF8 py_test:
	$(call LATEX,examples,examples)
pytest:
	$(call LATEX,utils/pyhyphen,examples)


dist: distsrc predistdoc distdoc

distsrc:
	-rm -rf $(distdir)
	rm -f $(distdir).tar.gz
	rm -f $(distdir)-doc.tar.gz
	mkdir $(distdir)
	srcdir=`pwd`; \
	for d in $(DISTDIRS); do \
	  mkdir -p $(distdir)/$$d; \
	  (cd $(distdir)/$$d; \
	   f=`find $$srcdir/$$d -maxdepth 1 -type f`; \
	   test -z "$$f" || ln -s $$f . || true); \
	done
	tar cfh - $(distdir) | gzip -c > $(distdir).tar.gz

predistdoc:
	mkdir -p $(distdir)/doc/{cjk,dvi,ps}
	mkdir -p $(distdir)/examples/{cjk,dvi,ps}
	mkdir -p $(distdir)/doc/man/{dvi,html,ps,txt}
	echo "This directory contains files which are \`preprocessed' with" \
	  > $(distdir)/doc/cjk/README
	echo "cjk-enc.el, bg5conv, etc." \
	  >> $(distdir)/doc/cjk/README
	echo \
	  >> $(distdir)/doc/cjk/README
	echo "You can feed them directly into LaTeX." \
	  >> $(distdir)/doc/cjk/README

distdoc: $(DISTTARGETS) manpages cweb
	for f in `find $(distdir) -type l`; do \
	  rm $$f; \
	done
	-for d in `find $(distdir) -depth -type d`; do \
	  rmdir --ignore-fail-on-non-empty $$d; \
	done
	tar cfh - $(distdir) | gzip -c > $(distdir)-doc.tar.gz
	rm -rf $(distdir)

manpages:
	for f in `find utils -type f -name '*.1'`; do \
	  groff -Tdvi -mec -man $$f > \
	    $(abs-distdir)/doc/man/dvi/`basename $$f .1`.dvi; \
	  groff -Tps -man $$f > \
	    $(abs-distdir)/doc/man/ps/`basename $$f .1`.ps; \
	  groff -Tlatin1 -man -P-bcu $$f > \
	    $(abs-distdir)/doc/man/txt/`basename $$f .1`.txt; \
	  groff -Thtml -man $$f > \
	    $(abs-distdir)/doc/man/html/`basename $$f .1`.html; \
	done

# we use the cweave from the c2cweb package (renamed to `cweavex')
cweb:
	for f in `find utils -type f -name '*.w'`; do \
	  cp $$f $(abs-distdir)/doc/dvi; \
	  (cd $(abs-distdir)/doc/dvi; \
	   F=`basename $$f .w`; \
	   cweavex +a $$F.w; \
	   tex $$F; \
	   rm -rf $$F.idx $$F.log $$F.scn $$F.tex $$F.toc $$F.w; \
	   dvips -P cms -o ../ps/$$F.ps $$F); \
	done

# EOF
