# $Id$
# Makefile for Eplain.  This file is public domain.

version     = 3.5
dist_name   = eplain-$(version)

.PHONY: default alldoc doc dvi ps pdf html info btxdoc dist
.PHONY: $(dist_name) $(dir_main) $(dir_doc) $(dir_source) $(dir_zsource)
.PHONY: clean mostlyclean distclean maintainer-clean
.DELETE_ON_ERROR:


default: eplain.tex
all: default doc

eplain.tex e: util/mkdist-merge $(sources)
	version=$(version) util/mkdist-merge


alldoc:
	cd doc && $(MAKE)

doc:
	cd doc && $(MAKE) doc

dvi:
	cd doc && $(MAKE) dvi

ps:
	cd doc && $(MAKE) ps

pdf doc/eplain.pdf doc/eplain.man1.pdf:
	cd doc && $(MAKE) pdf

html doc/eplain.html $(jpeg_demos):
	cd doc && $(MAKE) html

info doc/eplain.info:
	cd doc && $(MAKE) info

btxdoc:
	cd doc && $(MAKE) btxdoc

INSTALL:
	cd doc && $(MAKE) ../INSTALL

# we need to make a variety of things:
# - eplain-VER.tds.zip (for easy installation).
# - eplain-VER.browse.zip (for CTAN main tree; TDS is rejected there).
# - eplain-VER.zip (combination of above two for single-file CTAN upload)
# - eplain-VER.tar.gz (same as .tds.zip but in .tar.gz, for tug.org).
#
dist_tds_zip = $(dist_name).tds.zip
dist_browse_zip = $(dist_name).browse.zip
dist_upload_zip = $(dist_name).combined.zip
dist_tgz = $(dist_name).tar.gz
#
dist: $(dist_upload_zip) $(dist_tgz)

$(dist_upload_zip): $(dist_browse_zip) $(dist_tds_zip)
	rm -f $@
	zip $@ $^
	zipinfo -l $(dist_upload_zip)
	@echo

$(dist_tgz): $(dist_tds_zip)
	rm -rf $(dist_name) $@
	mkdir $(dist_name)
	unzip -q -d $(dist_name) $^
	tar czf $@ $(dist_name) # includes dir
	rm -rf $(dist_name)
	ls -l $@

browse_dir = $(dist_name)-browse
$(dist_browse_zip): $(dist_tds_zip)
	rm -rf $(browse_dir) $@
	unzip -q -d $(browse_dir) $^
# for browsing, get rid of eplain level:
	cd $(browse_dir) && for dir in *; do \
	  mv -v $$dir/eplain/* $$dir; \
	  rmdir -v $$dir/eplain; done
	mv -v $(browse_dir)/doc/doc/* $(browse_dir)/doc/  # skip extra level
	rmdir -v $(browse_dir)/doc/doc
	cd $(browse_dir) && zip -qr ../$@ *
	rm -rf $(browse_dir)
	#zipinfo -l $@
	ls -l $@
	@echo

#  The .tds.zip is the main thing.  Its directories and files follow.
# The idea of zsource is that we bundle up the various files we reuse into
# a zip so that our slave copies are not found by searches.

tds_dir     = $(dist_name)-tds
#
dir_main    = $(tds_dir)/tex/eplain
dir_doc     = $(tds_dir)/doc/eplain
dir_source  = $(tds_dir)/source/eplain
dir_zsource = $(dir_source)/eplain-source-$(version)
#
# Categories of the files in the source tree.
sources     = arrow.tex btxmac.tex ifpdf.sty path.sty texnames.sty xeplain.tex
support     = Makefile PROJECTS util/mkdist-*
manuals     = $(patsubst %,doc/%,Makefile *.texi btxdoc.bib btxdoc.tex texinfo.tex)
tests       = $(patsubst %,test/%,*.tex *.bib *.map *.mst *.eps)
utils       = util/idxuniq util/trimsee
demos       = demo/Makefile demo/*.tex
jpeg_demos  = doc/xhyper.jpg doc/lscommnt.jpg
#
# Files to go under the $(dir_*) directories.
files_main   = eplain.tex arrow.tex btxmac.tex eplain.ini
files_doc    = AUTHORS COPYING ChangeLog INSTALL NEWS README \
               doc/eplain.info doc/eplain.html doc/eplain.pdf doc/eplain.1 \
               doc/eplain.man1.pdf $(demos) $(jpeg_demos) $(utils)
files_source = xeplain.tex
files_zsource= $(sources) $(support) $(manuals) $(tests)
#
cp = cp --parents
#
$(dist_tds_zip): force
	rm -rf $(tds_dir) $@
	install -d $(dir_main) $(dir_doc) $(dir_source) $(dir_zsource)
	@echo -e "\
% This eplain.aux file is installed for the sake of initex.  Running\n\
% initex without this file will produce eplain.fmt which will never\n\
% issue warning messages about undefined labels.\n" >$(dir_main)/eplain.aux
	#
	$(cp) $(files_main)    $(dir_main)
	$(cp) $(files_doc)     $(dir_doc)
	$(cp) $(files_source)  $(dir_source)
	$(cp) $(files_zsource) $(dir_zsource)
	chmod -R a+rwX $(tds_dir)
	#
	# make zsource zip:
	cd `dirname $(dir_zsource)` \
	  && zip -qr `basename $(dir_zsource)`.zip `basename $(dir_zsource)`
	rm -rf $(dir_zsource)
	#
	# make tds zip:
	cd $(tds_dir) && zip -qr ../$(dist_tds_zip) *
	rm -rf $(tds_dir)
	#zipinfo -l $@
	ls -l $@
	@echo

force:

define clean-cmds
rm -rf $(dist_name) $(browse_dir) $(tds_dir)
endef

define distclean-cmds
$(clean-cmds)
rm -f $(dist_name).tar.gz $(dist_upload_zip) $(dist_tgz)
rm -f $(dist_browse_zip) $(dist_tds_zip)
rm -f eplain.fmt eplain.log
endef

define maintainer-clean-cmds
$(distclean-cmds)
rm -f eplain.tex
endef

clean mostlyclean:
	$(clean-cmds)
	cd doc && $(MAKE) $@
	cd demo && $(MAKE) $@

distclean:
	$(distclean-cmds)
	cd doc && $(MAKE) distclean
	cd demo && $(MAKE) distclean

maintainer-clean:
	$(maintainer-clean-cmds)
	cd doc && $(MAKE) maintainer-clean
	cd demo && $(MAKE) maintainer-clean
