 
# bash-completion add-on for rpmlint
# http://bash-completion.alioth.debian.org/

__abf_opts()
{

  if [[ ${cur} == -* ]] ; then
      COMPREPLY=( $(compgen -W "${@}" -- "${cur}") )
      return 0
  fi
  return 1
}

__abf_get()
{
  __abf_opts "--branch --verbose --help"
}

__abf_create()
{
  __abf_opts "--branch --no-def-branch --verbose --help"
}

__abf_put()
{
  __abf_opts "--verbose --help --message --minimal-file-size --do-not-remove-files"
}

__abf_show()
{
  __abf_opts "--project --verbose --help"
  shows="build-repos build-platforms save-to-repos save-to-platforms"
  if [[ ${cur} != -* ]] ; then
      if [[ ${prev} == -* ]] ; then
         return 0;
      fi
      COMPREPLY=( $(compgen -W "${shows}" -- "${cur}") )
      return 0
  fi
}

__abf_locate()
{
  __abf_opts "--project --directory --verbose --help"
  actions="update update-recursive"
  if [[ ${cur} != -* ]] ; then
      if [[ ${prev} == -* ]] ; then
         return 0;
      fi
      COMPREPLY=( $(compgen -W "${actions}" -- "${cur}") )
      return 0
  fi
}

__abf_mock_urpm()
{
  __abf_opts "--config --verbose --help"
  configs_dir='/etc/abf/mock-urpm/configs'
  if [ ${prev} == -c ] || [ ${prev} == --config ] ; then
    COMPREPLY=( $(compgen -W "` ls -1 ${configs_dir}/*.cfg | xargs -l basename  | sed s/[.]cfg//g`" -- "${cur}") )
  fi
}

__abf_rpmbuild()
{
  __abf_opts "--verbose --help --build"
}

__abf_build()
{
  __abf_opts "--branch --build-list --tag --commit --target-platform --arch --repository --save-to-repository --auto-publish-status --auto-publish --update-type --skip-spec-check --auto-create-container --no-extra-tests --no-cached-chroot --save-chroot --testing --external-nodes"
  update_types="security bugfix enhancement recommended newpackage"
  external_nodes_vals="owned everything"
  auto_publish_status="none default testing"
  if [ ${prev} == -r ] || [ ${prev} == --repository ] ; then
      COMPREPLY=( $(compgen -W "`abf show build-repos`" -- "${cur}") )
      return 0
  fi
  if [ ${prev} == -s ] || [ ${prev} == --save-to-repository ] ; then
      proj=""
      next=0
      for i in ${COMP_WORDS[@]}
      do
        if [[ $next == 1 ]] ; then
            proj=$i;
            next=0;
        fi
        if [[ "$i" == "-p" || "$i" == "--project" ]] ; then
            next=1;
        fi;
      done

      if [ -n "${proj}" ] ; then
        COMPREPLY=( $(compgen -W "`abf show save-to-repos -p ${proj}`" -- "${cur}") )
      fi
      return 0
  fi

  if [ ${prev} == --update-type ] ; then
      COMPREPLY=( $(compgen -W "${update_types}" -- "${cur}") )
      return 0
  fi

  if [ ${prev} == --external-nodes ] ; then
      COMPREPLY=( $(compgen -W "${external_nodes_vals}" -- "${cur}") )
      return 0
  fi

  if [ ${prev} == --auto-publish-status ] ; then
      COMPREPLY=( $(compgen -W "${auto_publish_status}" -- "${cur}") )
      return 0
  fi

}

__abf_chain_build()
{
  __abf_opts "--branch --build-list --tag --infile --commit --target-platform --arch --repository --save-to-repository --auto-publish-status --auto-publish --skip-spec-check --auto-create-container --no-extra-tests --no-cached-chroot --testing"
  auto_publish_status="none default testing"

  if [ ${prev} == --auto-publish-status ] ; then
      COMPREPLY=( $(compgen -W "${auto_publish_status}" -- "${cur}") )
      return 0
  fi
}

__abf_publish()
{
  __abf_opts "--verbose --help"
}

__abf_copy()
{
  __abf_opts "--pack --verbose --help"

  if [[ ${cur} != -* ]] ; then
      branches=`git branch --no-color | sed 's/^..//' | xargs echo`
      COMPREPLY=( $(compgen -W "${branches}" -- "${cur}") )
      return 0
  fi
}

__abf_status()
{
  __abf_opts "--verbose --help"
}

__abf_help()
{
  __abf_opts "--verbose --help"
}

__abf_search()
{
  __abf_opts "--verbose --help"
  types="users groups platforms projects"
  if [ "$COMP_CWORD" == "2" ] ; then
      COMPREPLY=( $(compgen -W "${types}" -- "${cur}") )
      return 0
  fi
}


__abf_test()
{
  __abf_opts "--verbose --help"
}


__abf_fetch()
{
  __abf_opts "--only --verbose --help"
}

__abf_store()
{
  __abf_opts "--verbose --help"
  if [ $? == 0 ]
  then 
    return 0;
  fi
  _filedir
  return 0
  
}

__abf_clean()
{
  __abf_opts "--auto-remove --verbose --help"
}

__abf_alias()
{
  __abf_opts "--verbose --help"
  types="add remove list"
  if [ "$COMP_CWORD" == "2" ] ; then
      COMPREPLY=( $(compgen -W "${types}" -- "${cur}") )
      return 0
  fi

}


__abf() 
{
    local opts modes
    modes="help get put show build chain_build publish status locate search test fetch mock-urpm rpmbuild clean copy create store alias add remove fork destroy pullrequest info"
    COMPREPLY=()
    mode="${COMP_WORDS[1]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    cur="${COMP_WORDS[COMP_CWORD]}"

    if [ "$COMP_CWORD" == "1" ] || ( [ "$COMP_CWORD" == "2" ] && [ "$mode" == "help" ] ); then
        COMPREPLY=( $(compgen -W "${modes}" -- ${cur}) )
        return 0
    fi

    for i in ${modes}
    do
        if [[ $i == $mode ]] ; then
            j=`echo ${mode}| sed s/-/_/`
            eval __abf_${j};
        fi
    done

}


complete -F __abf abf


