#
################################################################
# Copyright (C) 2002 Robert Anderson
# Copyright (C) 2003 Walter Landry and the Regents of the University
#                                          of California
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.

# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA
# 

_arx()
{
    _archives()
    {
	archives=`arx archives | grep "^[a-zA-Z]" | tr "\n" " "`
    }

    _configs()
    {
	configs=`find configs/ -type f | cut -d/ -f2-`
    }

    cachedir=~/.arx-cmd-opts
    subcmds=`cat $cachedir/arx-subcmds`

    local cur pwd

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    if [ "$prev" = "arx" ]; then
	arx_opts=`cat $cachedir/arx-options`
	COMPREPLY=( $( compgen -W "$subcmds $arx_opts" -- $cur ))
	return 0
    fi

    subcmd=""

    # prev is not arx.  See if $prev is a subcmd, and if so, add the
    # options of the subcommand to the list of completions.

    for try_subcmd in $subcmds; do
	if [ "$prev" = "$try_subcmd" ]; then
	    subcmd=$try_subcmd
	    subopts=`cat $cachedir/$subcmd-options`
	    COMPREPLY=( $( compgen -W "$subopts" -- $cur) )
	fi
    done

    # Here, there may be additional things you could put after the
    # subcmd other than an option.  For example, prepare-branch could
    # complete branches, if somehow we knew what category was
    # intended.

    case $subcmd in
	prepare-branch)
	    ;;
	update-config|build-config|replay-config|record-config|show-config)
	    _configs
	    COMPREPLY=( ${COMPREPLY[@]} $( compgen -W "$configs" -- $cur ) )
	    ;;
	push-mirror|my-default-archive)
	    _archives
	    COMPREPLY=( ${COMPREPLY[@]} $( compgen -W "$archives" -- $cur ) )
	    ;;
    esac

    # If $prev is not arx and is not a subcommand, it is either an
    # option or an argument.  See if it matches options we know how to
    # do completion for.

    case $prev in
	-A|--archive)
	    _archives
	    COMPREPLY=( $( compgen -W "$archives" -- $cur ) )
	    return 0
	    ;;
	-d|--dir)
	    COMPREPLY=( $( compgen -d -- $cur ) )
	    return
	    ;;
	*)
	    ;;
    esac

    # If we've gotten here, $prev is not arx, is not a subcommand,
    # and is not an option we know how to complete for.

    return 0
}
complete -F _arx -o default arx

###  Local Variables:
###  mode: shell-script
###  End:

# tag: Robert Anderson Sat Nov 16 22:29:18 PST 2002 (larch_bash_complete)
