#! /bin/sh

# waf configure wrapper

# Fancy colors used to beautify the output a bit.
#
NORMAL=""
BOLD=""
RED=""
YELLOW=""
GREEN=""

EXIT_SUCCESS=0
EXIT_FAILURE=1
EXIT_ERROR=2
EXIT_BUG=10

CUR_DIR=$PWD

#possible relative path
WORKINGDIR=`dirname $0`
cd $WORKINGDIR
#abs path
WORKINGDIR=`pwd`
cd $CUR_DIR

WAF="${WORKINGDIR}/tools/waf"

# Checks for WAF. Honours $WAF if set. Stores path to 'waf' in $WAF.
# Requires that $PYTHON is set.
#
checkWAF()
{
	printf "Checking for WAF\t\t\t:  "
	#installed miniwaf in sourcedir
	if [ -z "$WAF" ] ; then
	    if [ -f "${WORKINGDIR}/waf" ] ; then
		WAF="${WORKINGDIR}/waf"
		if [ ! -x "$WAF" ] ; then
		    chmod +x $WAF
		fi
	    fi
	fi
	if [ -z "$WAF" ] ; then
	    if [ -f "${WORKINGDIR}/waf-light" ] ; then
		${WORKINGDIR}/waf-light --make-waf
	        WAF="${WORKINGDIR}/waf"
	    fi
	fi
	#global installed waf with waf->waf.py link
	if [ -z "$WAF" ] ; then
	    WAF=`which waf 2>/dev/null`
	fi
	# neither waf nor miniwaf could be found
	if [ ! -x "$WAF" ] ; then
	    printf $RED"not found"$NORMAL"\n"
	    echo "Go to http://code.google.com/p/waf/"
	    echo "and download a waf version"
	    exit $EXIT_FAILURE
	else
	  printf $GREEN"$WAF"$NORMAL"\n"
	fi
}

# Generates a Makefile. Requires that $WAF is set.
#
generateMakefile()
{
	cat > Makefile << EOF
#!/usr/bin/make -f
# Waf Makefile wrapper
WAF_HOME=$CUR_DIR

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;
 
test: all
	python tools/test.py --mode=release
  
test-all: all
	python tools/test.py --mode=debug,release

test-debug: all
	python tools/test.py --mode=debug

benchmark: all
	build/default/node benchmark/run.js

website: website/api.html website/index.html

website/api.html: website/api.txt
	asciidoc --unsafe              \\
		-a theme=pipe                \\
		-a toc                       \\
		-a linkcss                   \\
		-o website/api.html website/api.txt

#		-a stylesdir=website/   \\
#		-a scriptsdir=website/   \\

website-upload: website
	scp website/* linode:~/tinyclouds/node/

clean:
	@$WAF clean

distclean:
	@$WAF distclean
	@-rm -rf _build_
	@-rm -f Makefile
	@-rm -f *.pyc

check:
	@$WAF check

dist:
	@$WAF dist

.PHONY: benchmark clean dist distclean check uninstall install all test test-all website website-upload

EOF
}

checkWAF

generateMakefile

"${WAF}" configure $*

exit $?
