

%.html : %.py
	mmpydoc $*.py

ifndef INSTALL_ROOT
INSTALL_ROOT=/usr/local
endif

ifndef DOCPATH
DOCPATH=$(INSTALL_ROOT)/doc/dopy
endif

ifndef PYTHON_PREFIX
PYTHON_LIB=$(shell python -c 'import sys, os; \
		print os.path.join(sys.prefix, "lib", \
                "python" + sys.version[:3])')
endif

ifndef MODPATH
MODPATH=$(PYTHON_LIB)/site-packages/dopy
endif

MODULES=dopy.py naming.py pos.py tb.py rsh.py tcp.py minicl.py __init__.py

FILES=$(MODULES) dopyserver dopyclient README.txt README.html Manual.nml \
   Manual.html

# documentation generated from modules
MOD_DOCS=dopy.html naming.html pos.html tb.html rsh.html tcp.html minicl.html

# documentation written in NML
NML_DOCS=README.html Manual.html

ALL: docs setup.py __init__.py

docs: $(NML_DOCS) $(MOD_DOCS)

README.html: README.txt
	nml2html README.txt

Manual.html: Manual.nml
	nml2html Manual.nml

__init__.py: Makefile __init__.py.in
	if [ "$(VER)" = "" ]; \
		then echo "Error: you must provide a VER parameter"; \
                exit 1; \
	fi
	sed 's/@VERSION@/"$(VER)"/' <__init__.py.in >__init__.py 

setup.py: setup.py.in Makefile
	if [ "$(VER)" = "" ]; \
		then echo "Error: you must provide a VER parameter"; \
                exit 1; \
	fi
	python setup.py.in $(VER) $(MODULES)

distrib: ALL
	if [ "$(VER)" = "" ]; \
		then echo "Error: you must provide a VER parameter"; \
                exit 1; \
	fi
	cvs export -r dopy-$(subst .,-,$(VER)) dopy
	cd dopy; $(MAKE) VER=$(VER)
	-cd dopy; $(MAKE) dist_test
	mv dopy dopy-$(VER)
	zip -r dopy-$(VER).zip dopy-$(VER)
	rm -rf dopy-$(VER)

# makes the test directory in the distribution tree
dist_test:
	mkdir test
	cp dopyserver dopyclient rshserver rshclient test

testbed: __init__.py
	mkdir -p tmp/dopy
	cp $(MODULES) tmp/dopy
	cp dopyserver dopyclient tmp
	
clean:
	rm -rf $(MOD_DOCS:.html=)
	rm -f $(MOD_DOCS) $(NML_DOCS)

install: install_docs install_code

install_code:
	install -d -m 755 $(MODPATH)
	install -m 644 $(MODULES) $(MODPATH)
	python -c "import compileall; compileall.main()" $(MODPATH)

install_docs:
	install -d -m 755 $(DOCPATH)
	install -m 644 $(NML_DOCS) $(MOD_DOCS) $(DOCPATH)
	for x in $(MOD_DOCS:.html=); do \
		if [ -e $$x ]; then cp -r $$x $(DOCPATH); fi; \
	done

uninstall: uninstall_docs uninstall_code
	
uninstall_docs:
	rm -rf $(DOCPATH)

uninstall_code:
	rm -rf $(MODPATH)
