# $Id: Makefile 29 2015-02-05 18:41:49Z karl $
# Makefile for Eplain.  This file is public domain.

version     = 3.7
dist_name   = eplain-$(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 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

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

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

# 
# we need to make a variety of things:
# - eplain-VER.tds.zip (for easy installation).
# - eplain-VER.upload.zip (combination of TDS and directory for main tree)
# - eplain-VER.tar.gz (same as .tds.zip but in .tar.gz, for tug.org).
#
dist_tds_zip = $(dist_name).tds.zip
dist_upload_zip = $(dist_name).upload.zip
dist_tgz = $(dist_name).tar.gz
#
dist: $(dist_upload_zip) $(dist_tgz)

# The tgz is just the tds.zip in a different format, with top-level dir.
$(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 $@

# The upload zip is the tds.zip (as a file) and an unpacked tree for browsing.
dist_upload_dir = $(dist_name)-upload
browse_dir = $(dist_upload_dir)/eplain
$(dist_upload_zip): $(dist_tds_zip)
	rm -rf $(dist_upload_dir) $@
	mkdir $(dist_upload_dir)
# unpack .tds.zip in destdir/eplain:
	unzip -q -d $(browse_dir) $^
# for browsing, get rid of eplain level:
	cd $(browse_dir) && for dir in *; do \
	  mv $$dir/eplain/* $$dir; \
	  rmdir $$dir/eplain; done
# and make top-level README symlink.
	ln -s doc/README $(browse_dir)/
# now also copy .tds.zip into dest dir, without version number:
	cp $^ $(dist_upload_dir)/eplain.tds.zip
# make upload zip as combination, including top-level directory:
	zip -qr $@ $(dist_upload_dir)
	rm -rf $(dist_upload_dir)
	zipinfo -1 $@ >/tmp/zu
	@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)
#
# 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)
	@printf "%s" "\
% This eplain.aux file is installed for the sake of initex.  Running\n\
% initex without this file would produce an eplain.fmt which never\n\
% issues 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)
	#
	mv $(dir_doc)/doc/* $(dir_doc)/  # skip extra level
	rmdir $(dir_doc)/doc             # which leaves empty 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 -1 $@ >/tmp/zt
	@ls -l $@
	@echo

force:

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

define distclean-cmds
$(clean-cmds)
rm -f $(dist_name).tar.gz $(dist_upload_zip) $(dist_tgz) $(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

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