#!/bin/sh
# usage: rip-completion
#
# Completion script for rip program names.
#
# In ~/.bashrc:
#   eval `rip-completion --bash`
#
# In ~/.zshrc
#   eval `rip-completion --zsh`
#
# Consider changing your PS1 to display your active ripenv:
#   PS1='[\u@\h \W$(_rip_ps1 " env:%s")]\$ '
#
# The argument to _rip_ps1 will be displayed only if there
# is an active ripenv.  The %s token will be the name of
# the current env.

cat <<'ripps1'
function _rip_ps1 ()
{
  active=$(readlink $RIPDIR/active);
  if [ "$?" -eq "0" ]; then
    ripenv=$(basename $active);
    printf "${1:- env:%s}" ${ripenv};
  fi;
}
;
ripps1

case $1 in
  --zsh )
    cat <<'completer'
function _rip_completion () {
  reply=(`rip commands`);
};
compctl -K _rip_completion rip
completer
    ;;
  * )
    cat <<'completer'
_rip_complete () {
  COMPREPLY=();
  if [ $COMP_CWORD = 1 ]; then
    completes=`rip commands`;
    COMPREPLY=(`compgen -W "$completes" -- $2`);
  fi;
};
complete -F _rip_complete rip
completer
    ;;
esac
