#! /bin/sh

#finds all from the current point in the directory tree
function find_repos()
{
    FOUND_REPOS=$(find . -type d | grep -e ".hg$" | sed "s/\.hg//g")
    echo -e $FOUND_REPOS
}

function print_info()
{
    echo -e "\n>> $1"
}

function update_repos()
{
    if [ -z "$1" ]
    then
        TAG="tip"
    else
        TAG=$1
    fi
    REPOS=$( cat << EOF
./
./src/VPThread_lib/
./src/VPThread_lib/VMS/
./src/VPThread_lib/VMS/DynArray/
./src/VPThread_lib/VMS/Histogram/
./src/VPThread_lib/VMS/Queue_impl/
./src/VPThread_lib/VMS/Hash_impl/
EOF
)

    BASE_DIR=$(pwd)

    for DIR in $REPOS
    do
        cd $DIR
        hg pull
        hg update $TAG
        if [ $? -ne 0 ]
        then
            echo ">> Falling back from Tag to V0 checkout"
            hg update V0
            if [ $? -ne 0 ]
            then
                echo ">> Falling back from Tag to Tip checkout"
                hg update tip
            fi
        fi
        cd $BASE_DIR
    done
}