VERSION=0.4.0
MZSCHEME=/f/Program Files/PLT/mzscheme
LAML=f:/Program Files/laml/
TOPDIR=$(PWD)
HD=doc/html
HD_R=../..
TMP=/tmp

EGG=dist/rgraph.egg
DIST=dist/rgraph-$(VERSION).tar
SRC=dist/rgraph-$(VERSION)-src.tar

# will go into egg and both .tar.gz, but have some preprocessing
GENBASE= \
	rgraph.setup \
	rgraph.scm \
	rgraph-test1.scm \

# will go into egg and both .tar.gz
EGGBASE= \
	rgraph-base.scm \
	rgraph-prop.scm \
	rgraph-vis.scm \
	rgraph-test2.scm \
	rgraph-test3.scm \

# will go into both .tar.gz
INSTALL=$(GENBASE) \
	$(EGGBASE) \
	rgraph-al.scm \
	rgraph-vl-vector.scm \
	rgraph-vl-hash.scm \
	rgraph-el-slist.scm \
	rgraph-el-hash.scm \
	COPYRIGHT \
	AUTHORS \
	NEWS \
	README \
	ChangeLog \
	INSTALL \
	TODO \

# documentation
LAMLS = rgraph.laml \
	rgraph-vis.laml \
	rgraph-prop.laml \
	rgraph-al.laml \
	rgraph-vl-vector.laml \
	rgraph-vl-hash.laml \
	rgraph-el-slist.laml \
	rgraph-el-hash.laml \
	rgraph-alg-dfs.laml \
	rgraph-alg-topsort.laml \
	rgraph-alg-part-fidmat.laml \

HTML=	$(HD)/index.html \
	$(HD)/rgraph.html \

EXTRAS=$(LAMLS) \
	Makefile index.html expand.scm

all: doc
.PHONY: dist

rgraph-doc.txt:	rgraph.scm \
		rgraph-vis.scm \
		rgraph-prop.scm \
		rgraph-vl-vector.scm \
		rgraph-vl-hash.scm \
		rgraph-el-slist.scm \
		rgraph-el-hash.scm \
		rgraph-alg-dfs.scm \
		rgraph-alg-topsort.scm \
		rgraph-alg-part-fidmat.scm
	cat $^ > $@

$(HD)/rgraph.html: rgraph-doc.txt
	@mkdir -p $(HD)
	@echo SchemeDoc\'ing code to $@
	csi -script expand.scm rgraph.laml > rgraph-doc.laml
	@"$(MZSCHEME)" --mute-banner -v --eval \
		'(define laml-dir "$(LAML)")' \
		-f rgraph-doc.laml rgraph ""
	@mv $(notdir $@) $(basename $(notdir $@)).css $(HD)
	@rm -f rgraph-doc.laml rgraph.manlsp

$(HD)/index.html: index.html Makefile
	@mkdir -p $(HD)
	sed "s/==VERSION==/$(VERSION)/" $< > $@

$(HD)/%.html: %.laml
	@mkdir -p $(HD)
	@echo SchemeDoc\'ing $< to $@
	@"$(MZSCHEME)" --mute-banner -v --eval \
		'(define laml-dir "$(LAML)")' \
		-f $< $(basename $<) ""
	@mv $(notdir $@) $(basename $(notdir $@)).css $(HD)
	@echo rm -f $(basename $<).manlsp

doc: $(HTML)

cvsdoc: $(HTML)
	cp -p $(HD)/*.html $(HD)/*.css www
	export CVS_RSH=ssh && \
	cd www && TOADD=`cvs status -l *.html *.css | grep File: | grep "Status: Unknown" | sed 's#File: \([A-Za-z0-9\._-]*\).*#\1#'` && \
	( test -z "$$TOADD" || cvs add -m "Initial import" $$TOADD ) && \
	cvs commit -l -m "Update for version $(VERSION).`date +%Y%m%d`" *.html *.css

dist: doc $(INSTALL) $(EXTRAS)
	@rm -f $(EGG) $(DIST).gz $(SRC).gz
	@mkdir -p dist
	tar cf $(EGG) $(EGGBASE)
	@mkdir -p $(TMP)
	sed "s/==VERSION==/$(VERSION)/" rgraph.setup > $(TMP)/rgraph.setup
	csi -script expand.scm rgraph.scm > $(TMP)/rgraph.scm
	csi -script expand.scm rgraph-test1.scm > $(TMP)/rgraph-test1.scm

	cd $(TMP) && tar uf $(TOPDIR)/$(EGG) $(GENBASE)
	cp -p $(EGG) $(DIST)
	cp -p COPYRIGHT $(TMP)/rgraph-COPYRIGHT.txt && cd $(TMP) && tar uf $(TOPDIR)/$(EGG) rgraph-COPYRIGHT.txt
	gzip $(EGG)

	mv $(EGG).gz $(EGG)
	cp -p $(EGG) $(HD)/
	cd $(TMP && rm $(GENBASE)
	tar uf $(DIST) $(INSTALL) $(HTML)

	tar cf $(SRC) $(INSTALL) $(EXTRAS)

	gzip $(DIST)
	cp -p $(DIST).gz $(HD)/
	gzip $(SRC)
	cp -p $(SRC).gz $(HD)/

upload: dist	
	cd dist && rm -f rgraph*.sig && \
	gpg -b rgraph.egg && \
	gpg -b rgraph-$(VERSION).tar.gz && \
	gpg -b rgraph-$(VERSION)-src.tar.gz && \
	ncftpput savannah.nongnu.org /incoming/savannah/rgraph rgraph.egg rgraph-$(VERSION).tar.gz rgraph-$(VERSION)-src.tar.gz rgraph.egg.sig rgraph-$(VERSION).tar.gz.sig rgraph-$(VERSION)-src.tar.gz.sig

tilde:
	rm -f *~

install: dist
	@mkdir -p $(TMP)
	@rm -rf $(TMP)/rgraph/*
	mkdir -p $(TMP)/rgraph
	cd $(TMP)/rgraph && cp -p $(TOPDIR)/$(EGG) . && chicken-setup rgraph