#!/bin/sh

# waf configure wrapper

EXIT_FAILURE=1


# Checks for Python interpreter. Honours $PYTHON if set. Stores path to
# interpreter in $PYTHON.
checkPython()
{
	if [ -z "$PYTHON" ] ; then
		PYTHON="python"
	fi
	printf "Checking for Python\t\t\t : "
	("$PYTHON" --version) < /dev/null > /dev/null 2>&1 || {
		printf "not found!\n"
		echo "Please make sure that the Python interpreter is available in your PATH"
		echo "or invoke configure using the PYTHON flag, e.g."
		echo "$ PYTHON=/usr/local/bin/python ./waf configure "$@
		echo "Alternatively, you can run ./autogen.sh to use the autotools chain"
		echo "to build and install the sources."
		exit $EXIT_FAILURE
	}
	printf "ok\n"
}

# Generates a Makefile.
generateMakefile()
{
	cat > Makefile << EOF
#!/usr/bin/make -f
# Waf Makefile wrapper

all:
	@./waf build

all-debug:
	@./waf -v build

all-progress:
	@./waf -p build

install:
	@if test -n "\$(DESTDIR)"; then \\
		./waf install --destdir="\$(DESTDIR)"; \\
	else \\
		./waf install; \\
	fi;

uninstall:
	@if test -n "\$(DESTDIR)"; then \\
		./waf uninstall --destdir="\$(DESTDIR)"; \\
	else \\
		./waf uninstall; \\
	fi;

clean:
	@./waf clean

distclean:
	@./waf distclean
	@-rm -rf _build_
	@-rm -f Makefile

distcheck:
	@./waf distcheck

check:
	@./waf check

dist:
	@./waf dist

sign:
	@./waf --sign

.PHONY: clean dist distclean check uninstall install all

EOF

	cat > src/Makefile << EOF
#!/usr/bin/make -f
# Waf Makefile wrapper

all:
	cd .. && ./waf build

all-debug:
	cd .. && ./waf -v build

clean:
	cd .. && ./waf clean

EOF
}

checkPython $@
generateMakefile

./waf configure $@

exit $?
