#!/bin/bash

# This script will recreate the "wcb" Waf script used to build the
# Wire Cell Toolkit.  Normally only developers exercise it.  

output="$(pwd)/wcb"
wafurl="https://gitlab.com/ita1024/waf.git"
wafdir="$(pwd)/waf"
waftag="waf-2.0.19"


toolsurl="https://github.com/WireCell/waf-tools.git"
tooldir="$(pwd)"
# initial defaults from waf
tools="doxygen boost bjam"
# add any in cwd, assuming we are sitting in waf-tools
tools="$tools $(echo *.py| sed 's/.py//g')"



usage () {
    echo "refresh-wcb [options] [tools]"
    echo "Default tools: $tools"
    echo "Likely call:"
    echo "./refresh-wcb -o /path/to/wire-cell-toolkit/wcb"
    exit 1;
}

while getopts "v:w:t:o:W:T:" opt; do
    case "$opt" in
        v)
            waftag="waf-$OPTARG"
            ;;
	w)
	    wafdir="$(readlink -f $OPTARG)"
	    ;;
	t)
	    tooldir="$(readlink -f $OPTARG)"
	    ;;
	o)
	    output=$OPTARG
	    ;;
	W)
	    wafurl=$OPTARG
	    ;;
	T)
	    toolsurl=$OPTARG
	    ;;
	*)
	    usage
	    ;;
    esac
done
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
if [ -n "$1" ] ; then
    tools="$@"
fi

if [ -d "$wafdir" ] ; then
    pushd $wafdir
    git fetch
    git checkout $waftag
    popd
else
    git clone "$wafurl" "$wafdir"
    pushd $wafdir
    git checkout $waftag
    popd
fi
if [ ! -d "$tooldir" ] ; then
    git clone "$toolsurl" "$tooldir"
fi
if [ ! -f "$tooldir/smplpkgs.py" ] ; then
    echo "Tooldir does not look correct: $tooldir"
    exit 1
fi

toolflags="compat15"
for tool in $tools ;
do
    maybe="$tooldir/${tool}.py"
    if [ -f $maybe ] ; then
	toolflags="$toolflags,$maybe"
	continue
    fi
    maybe="$wafdir/waflib/Tools/${tool}.py"
    if [ -f $maybe ] ; then
	toolflags="$toolflags,$tool"
	continue
    fi
    maybe="$wafdir/waflib/extras/${tool}.py"
    if [ -f $maybe ] ; then
	toolflags="$toolflags,$tool"
	continue
    fi
    
    echo "Failed to find tool $tool"
    exit 1
done

pushd "$wafdir" > /dev/null
python waf-light --tools=$toolflags
popd > /dev/null
echo "built waf with tools: $toolflags"
mv "$wafdir/waf" $output
echo "$output"
