# Completion for the vserver command. Source this file (or on some systems
# add it to ~/.bash_completion and start a new shell) and bash's completion
# mechanism will know all about vserver's options!
#
# Copyright (C) Thomas Champagne <lafeuil@gmail.com>
#
# 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; either version 2, or (at your option)
# any later version.
#
# 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.
#
# The latest version of this software can be obtained here:
#
# http://linux-vserver.org/Vserver+Completion
#
# version 0.4.0

have vserver-info && {
: ${UTIL_VSERVER_VARS:=$(/usr/sbin/vserver-info - SYSINFO |grep prefix: | awk '{ print $2}')/lib/util-vserver/util-vserver-vars}

test -e "$UTIL_VSERVER_VARS" && {
	
if [ -z "$_VS_NEWLINE" -o -z "$VS_ALLVSERVERS_ARGS" ]
then
        . "$UTIL_VSERVER_VARS"
        . "$_LIB_FUNCTIONS"
fi

_vserver() {
    local cur cmds cmdOpts cmdMethodOpts helpCmds names names_pipe func i j method
    
    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    
    # find available v-servers: 
    # call function getAllVservers in vserver library
    getAllVservers names
    names_pipe=`echo ${names[@]} | sed 's/ /|/g'`
    
    # available commands
    cmds='start stop restart condrestart suexec exec enter chkconfig \
	running status unify pkg apt-get apt-config apt-cache \
	rpm pkgmgmt delete'
    
    # options (long and short name)
    cmdOpts='--help --version --debug --defaulttty -s --sync -v \
	--verbose --silent --'
    
    cmdMethodOpts='-m -n --context --confdir --lockfile \
	--hostname --netdev --netbcast --netmask \
	--netprefix --interface --cpuset \
	--cpusetcpus --cpusetmems --cpusetvirt \
	--initstyle --flags --help --'
    
    # if the previous option is a single option 
    helpCmds='--help|--version'
    if [[ ${COMP_WORDS[1]} == @($helpCmds) ]] 
    then
	return 0
    fi
    
    # search a verser's name
    for (( i=0; i < ${#COMP_WORDS[@]}-1; i++ ))
    do
	if [[ ${COMP_WORDS[i]} == @($names_pipe) ]]
	then
          # it's found
	    br                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    