#!/bin/bash

# COMP_WORDS: an array of all the words typed after the name of the program the compspec belongs to
# COMP_CWORD: an index of the COMP_WORDS array pointing to the word the current cursor is at - in other words, the index of the word the cursor was when the tab key was pressed
# COMP_LINE: the current command line

# Path to root scripts directory
ROOT_DIR=${BASH_SOURCE[0]%/*/*}
SCRIPTS_DIR="$ROOT_DIR/scripts"

_rn_utils_completions()
{
    PATH_TO_COMPLETION="$SCRIPTS_DIR"
    
    for ((i=1;i<=COMP_CWORD-1;i++)); do
        PATH_TO_COMPLETION="$PATH_TO_COMPLETION/${COMP_WORDS[i]}"
    done
    
    if [ -e "$PATH_TO_COMPLETION/.completion" ];
    then
        GENERAL_SCRIPTS_AND_DIRS=$(sh $PATH_TO_COMPLETION/.completion)
        COMPREPLY+=($(compgen -W "$GENERAL_SCRIPTS_AND_DIRS" "${COMP_WORDS[COMP_CWORD]}"))
    fi
}

complete -F _rn_utils_completions rn-utils