SRCDIRS		= bin make schema xsl
DOCDIRS		= example
DTXDIRS 	= sty 
MAKEDIRS 	= $(SRCDIRS) $(DOCDIRS) $(DTXDIRS)
TDSCOLL 	?= $(shell basename $$PWD)

all clean distclean: 
	@for d in $(MAKEDIRS); do (cd $$d && $(MAKE) -$(MAKEFLAGS) $@) done

package doc filedate checksum enablechecksum disablechecksum: 
	@for d in $(DTXDIRS); do (cd $$d && $(MAKE) -$(MAKEFLAGS) $@) done


# make a zip for CTAN submission including the TeX directory structure
SVNEXP 		= svn export --quiet
TDSURL		= https://svn.kwarc.info/repos/stex/trunk
EXPORTED	= /tmp/$(TDSCOLL)
TDSDIR 		= /tmp/$(TDSCOLL).tds
TDSZIP 		= /tmp/$(TDSCOLL).tds.zip
CTANDIR 	= /tmp/$(TDSCOLL).ctan
CTANZIP 	= /tmp/$(TDSCOLL).ctan.zip
TDS.tex = 
TDS.doc = README
TDS.src = 

# this target makes the file CTANZIP by creating and populating the directory 
# CTANDIR and zipping it. To be current, we first make all, and update 
# the filedates and the checksums. To get rid of all the junk we commit and 
# export a clean copy EXPORTED, on which we run the target lctan below,  
# which generates a directory CTANDIR, which we zip and move into place. 
ctan:  #all filedate checksum
	svn ci -m'draining just to be sure for CTAN distribution'
	rm -Rf $(EXPORTED)
	$(SVNEXP) $(TDSURL) $(EXPORTED)
	find $(EXPORTED) -name ".svn" -type d -exec rm -Rf {} \;
	cd $(EXPORTED);	$(MAKE) -$(MAKEFLAGS) lctan
	cd $(CTANDIR); zip -r -q $(CTANZIP) .; cd -; cp $(CTANZIP) .
#	rm -Rf $(EXPORTED) $(CTANDIR) $(CTANZIP)

# this target is run on EXPORTED. It first makes the target ltds below, 
# and then copies all necessary stuff into CTANDIR. 
lctan: ltds
	rm -Rf $(CTANDIR) $(CTANZIP)
	mkdir -p $(CTANDIR) 
	cp $(TDSZIP) $(CTANDIR)
	@for d in $(DTXDIRS); do (cd $$d && $(MAKE) -$(MAKEFLAGS) lctan) done
	@for d in $(SRCDIRS) $(DOCDIRS); do (cp -R $$d $(CTANDIR)) done; 
	cp $(TDSZIP) README Makefile $(CTANDIR)

# this target makes the file TDSZIP by creating and populating the directory 
# TDSDIR and zipping it. 
ltds: 
	rm -Rf $(TDSDIR) $(TDSZIP)
	$(MAKE) -$(MAKEFLAGS) enablechecksum
	@for d in $(DTXDIRS); do (cd $$d && $(MAKE) -$(MAKEFLAGS) ltds) done
	@for d in $(SRCDIRS); do (cp -R $$d $(TDSDIR)/source/latex/$(TDSCOLL)) done; 
	@for d in $(DOCDIRS); do (cp -R $$d $(TDSDIR)/doc/latex/$(TDSCOLL)) done; 
	cp $(TDS.doc) $(TDSDIR)/doc/latex/$(TDSCOLL)
	cd $(TDSDIR); zip -r -q $(TDSZIP) .; cd -
#	rm -Rf $(TDSDIR) $(TDSZIP)
echo:
	echo $(PWD)
