VERSION = 1.3

PREFIX = /usr/local
BINDIR = $(PREFIX)/bin
DATADIR = $(PREFIX)/share/scmail
SITELIBDIR = `gauche-config --sitelibdir`

DIST =	AUTHORS \
	COPYING \
	ChangeLog \
	Makefile \
	README \
	NEWS \
	scmail-refile.in \
	scmail-deliver.in \
	scbayes.in \
	scmail.scm \
	codeconv.scm \
	check-gauche.scm 

TARGET = scmail-deliver scmail-refile scbayes \
	 scmail/bayesian-filter.scm \
	 dot.scmail/deliver-rules.sample

all: check-gauche $(TARGET) update-doc

update-doc:
	cd doc && make

check-gauche:
	gosh -I. check-gauche.scm 

scmail/bayesian-filter.scm: scmail/bayesian-filter.scm.in
	rm -f scmail/bayesian-filter.scm
	gosh codeconv.scm scmail/bayesian-filter.scm.in > \
			  scmail/bayesian-filter.scm
	chmod -w scmail/bayesian-filter.scm

dot.scmail/deliver-rules.sample: dot.scmail/deliver-rules.sample.in
	rm -f dot.scmail/deliver-rules.sample
	gosh codeconv.scm dot.scmail/deliver-rules.sample.in > \
			  dot.scmail/deliver-rules.sample
	chmod -w dot.scmail/deliver-rules.sample

scmail-deliver: scmail-deliver.in Makefile
	rm -f scmail-deliver
	GOSH=`which gosh` && \
	sed -e "s!@GOSH@!$$GOSH!g" \
	    scmail-deliver.in > scmail-deliver.tmp
	mv scmail-deliver.tmp scmail-deliver
	chmod 555 scmail-deliver

scmail-refile: scmail-refile.in Makefile
	rm -f scmail-refile
	GOSH=`which gosh` && \
	sed -e "s!@GOSH@!$$GOSH!g" \
	    scmail-refile.in > scmail-refile.tmp
	mv scmail-refile.tmp scmail-refile
	chmod 555 scmail-refile

scbayes: scbayes.in Makefile
	rm -f scbayes
	GOSH=`which gosh` && \
	sed -e "s!@GOSH@!$$GOSH!g" \
	    scbayes.in > scbayes.tmp
	mv scbayes.tmp scbayes
	chmod 555 scbayes

clean:
	rm -f $(TARGET)

check: all
	cd tests && make check

install:
	mkdir -p $(BINDIR)
	mkdir -p $(DATADIR)
	mkdir -p $(SITELIBDIR)/scmail
	mkdir -p $(DATADIR)/dot.scmail
	cp -fp scmail-deliver scmail-refile scbayes $(BINDIR)
	cp -fp dot.scmail/*.sample $(DATADIR)/dot.scmail
	cp -fp scmail.scm $(SITELIBDIR)
	cp -fp scmail/*.scm $(SITELIBDIR)/scmail

dist: all
	rm -rf scmail-$(VERSION)
	rm -f scmail-$(VERSION).tar.gz

	mkdir scmail-$(VERSION)
	mkdir scmail-$(VERSION)/doc
	mkdir scmail-$(VERSION)/scmail
	mkdir scmail-$(VERSION)/dot.scmail
	mkdir scmail-$(VERSION)/tests
	cp -p $(DIST) scmail-$(VERSION)
	cp -p doc/embed.scm doc/Makefile doc/scmail*.html.in doc/scbayes*.html \
		scmail-$(VERSION)/doc
	cp -p scmail/*.scm scmail/*.in scmail-$(VERSION)/scmail
	cp -p dot.scmail/*.sample dot.scmail/*.in \
		scmail-$(VERSION)/dot.scmail
	cp -p tests/Makefile tests/*.scm tests/[1-9] \
		tests/*.in tests/scmail-commands \
		scmail-$(VERSION)/tests 
	for i in `find scmail-$(VERSION) -type f -print`; do \
		test -e $$i -a -e $$i.in && rm -f $$i; true; \
	done
	tar zvcf scmail-$(VERSION).tar.gz  scmail-$(VERSION)
	rm -rf scmail-$(VERSION)

