#!/bin/sh
#
# $Id: bashrc,v 1.1 2004/11/08 00:09:46 blais Exp $
# $Source: /u/blais/cvsroot/oversion/etc/bashrc,v $
#
# Author: Martin Blais
# Date: 2004-11-07
#

_svn_common ()
{
    exe=$1
    local cur cmds cmdOpts pOpts mOpts rOpts qOpts nOpts optsParam opt
    local helpCmds optBase i
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    
    # Possible expansions, without unambiguous abbreviations such as "up".
    cmds=$($exe help| egrep '^   ' | sed -e 's/(//;s/)//;s/,//g')

    if [[ $COMP_CWORD -eq 1 ]] ; then
	COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
	return 0
    fi

    # find the current command
    cmd=${COMP_WORDS[1]}

    # options that require a parameter
    helpOut=$($exe $cmd --help | egrep '^  .* : ' | \
	      sed -e 's/[ ]*:.*//;s/\[//;s/\]//;')

    optsParam=$(echo "$helpOut" | grep arg$)
    optsParam=$(echo $optsParam | sed -e 's/ arg//g;s/ /|/g')

    # if not typing an option, or if the previous option required a
    # parameter, then fallback on ordinary filename expansion
    helpCmds='help|--help|h|\?'
    if [[ ${COMP_WORDS[1]} != @($helpCmds) ]] && \
       [[ "$cur" != -* ]] || \
       [[ ${COMP_WORDS[COMP_CWORD-1]} == @($optsParam) ]] ; then
    	return 0
    fi

    cmdOpts=$(echo $helpOut | sed -e 's/ arg//g')

    COMPREPLY=( $( compgen -W "$cmdOpts" -- $cur ) )

    return 0
}

_svn_new ()
{
    _svn_common "svn"
}
complete -F _svn_new -o default svn

_svnadmin_new ()
{
    _svn_common "svnadmin"
}
complete -F _svnadmin_new -o default svnadmin
