# this can do multipage
#go5:
#	dot -Tps server.dot > server.ps
#	evince server.ps

#backshift_filesystem.py
#fses.py
#hardlinks.py
#test_backshift_filesystem.py
modules=varblock_mod.py

python_version=2.6

files=backshift main.py rolling_checksum_py_mod.py path_mod.py file_repo_mod.py gdbm_ctypes.py min_max_chunker_mod.py

#go: rolling_checksum_py_mod.py
#	./this-pypy ./test-rolling_checksum_py_mod.ut

first-rule: treap.py tags unit-tests system-tests
	/bin/true

treap.py: treap/treap.m4
	cd treap; make; cp treap.py py_treap_node.py ..
	sed -i 's/pyx_treap_node/py_treap_node/g' treap.py

unit-tests: test-rolling_checksum_py_mod.uto test-gdbm_ctypes.uto test-cacher_mod.uto test-min_max_chunker_mod.uto
	/bin/true

pick:
	./this-pylint pick-rolling_checksum
	./pick-rolling_checksum

tags:
	ctags-exuberant $(files)
	
system-tests: $(files)
	./this-pylint $(files)
	cd tests; make

test-gdbm_ctypes.uto: test-gdbm_ctypes gdbm_ctypes.py
	./this-pylint test-gdbm_ctypes gdbm_ctypes.py
	if ! ./test-gdbm_ctypes > test-gdbm_ctypes.uto 2>&1; then cat test-gdbm_ctypes.uto; rm test-gdbm_ctypes.uto; exit 1; fi

test-rolling_checksum_py_mod.uto: test-rolling_checksum_py_mod rolling_checksum_py_mod.py
	./this-pylint test-rolling_checksum_py_mod rolling_checksum_py_mod.py
	if ! [ -f rcm-input-data ]; then dd if=/dev/urandom of=rcm-input-data bs=1024k count=32; fi
	./run-ut rolling_checksum_py_mod

test-min_max_chunker_mod.uto: test-min_max_chunker_mod min_max_chunker_mod.py
	./this-pylint test-min_max_chunker_mod min_max_chunker_mod.py
	./run-ut min_max_chunker_mod

test-cacher_mod.uto: test-cacher_mod cacher_mod.py
	./this-pylint test-cacher_mod cacher_mod.py
	./run-ut cacher_mod
	rm -f a.gdbm b.gdbm c.gdbm

rcms: test-rolling_checksum_py_mod.uto rolling_checksum_pyx_mod.pyx
	#gcc -O3 -c -I/usr/local/cpython-$$pyvers/include/python$$pyvers rolling_checksum_pyx_mod.c; \
	#gcc -shared rolling_checksum_pyx_mod.o -o rolling_checksum_pyx_mod.so; \
	#
	set -xeu; \
	for pyvers in 2.5 2.6 2.7 3.1 3.2rc1; \
	do \
		rm -f rolling_checksum_pyx_mod.c; \
		rm -f rolling_checksum_pyx_mod.o; \
		rm -f rolling_checksum_pyx_mod.so; \
		/usr/local/cpython-$$pyvers/bin/python \
			/usr/local/cpython-$$pyvers/bin/cython \
			rolling_checksum_pyx_mod.pyx; \
		gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC \
			-I/usr/local/cpython-$$pyvers/include/python$$pyvers -c rolling_checksum_pyx_mod.c; \
		gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions rolling_checksum_pyx_mod.o \
			-o rolling_checksum_pyx_mod.so; \
		cp rolling_checksum_pyx_mod.so /usr/local/cpython-$$pyvers/lib/python$$pyvers/site-packages/.; \
	done
	rm -f rolling_checksum_pyx_mod.so
	touch rcms
#		gcc -shared rolling_checksum_pyx_mod.o -o rolling_checksum_pyx_mod.so \
#			-L/usr/local/cpython-$$pyvers/lib/python$$pyvers/config \
#			-lpython$$pyvers; \
#

rolling_checksum_py_mod.py: rolling_checksum_mod.m4
	m4 -Dpy=1 rolling_checksum_mod.m4 > rolling_checksum_py_mod.py

rolling_checksum_pyx_mod.pyx: rolling_checksum_mod.m4
	m4 -Dpyx=1 rolling_checksum_mod.m4 > rolling_checksum_pyx_mod.pyx

clean:
	rm -f *.pdf input-file output-file *.o *.so *.pyc *-pylint.html tags *.class *.uto treap.py py_treap_node.py
	cd tests; make clean
	cd treap; make veryclean
	rm -rf __pycache__
	rm -f rolling_checksum_py_mod.py
	rm -f rolling_checksum_pyx_mod.pyx
	rm -f *.c
	rm -f rcms
	rm -f rcm-input-data