# Homepage: http://code.google.com/p/coderev
# License: GPLv2, see "COPYING"
#
# This library implements cvs operations, see comments in coderev.sh
#
# $Id: libcvs.sh 10 2008-08-23 09:02:26Z mattwyl $

CVS_OPS=(
    cvs_get_banner
    cvs_get_repository
    cvs_get_project_path
    cvs_get_working_revision
    cvs_get_active_list
    cvs_get_diff
    cvs_get_diff_opt
)

function cvs_get_banner
{
    echo "CVS"
    return 0
}

function cvs_get_repository
{
    cat CVS/Root
}

function cvs_get_project_path
{
    cat CVS/Repository
}

function cvs_get_working_revision
{
    # CVS doesn't have a global revision number, just fetch revision of the
    # first file or "."
    #
    local pathname="."
    [[ -n $1 ]] && [[ -z $2 ]] && pathname=$1
    cvs st $pathname 2>/dev/null | grep 'Working revision:.*\.' | head -1 \
        | sed 's/.*Working revision://' | awk '{print $1}'
}

function cvs_get_active_list
{
    cvs st $@ | grep File: \
        | awk '$4 != "Up-to-date" && $4 != "Unknown" {print $2}'
}

function cvs_get_diff
{
    local op diff_opt OPTIND OPTARG

    while getopts "r:" op; do
        case $op in
            r) diff_opt="-r $OPTARG" ;;
            ?) echo "Unknown option: -$op" >&2; exit 1;;
        esac
    done
    shift $((OPTIND - 1))

    cvs diff -uN $diff_opt $@
    return 0 # cvs diff return 1 when there're changes
}

function cvs_get_diff_opt
{
    echo "-r $1"
}