#!/bin/bash
_trip_tab_complete() {
    local cur
    COMPREPLY=() # Array with the completion
    cur="${COMP_WORDS[COMP_CWORD]}" # Partially written argument

    # The cache file is already escaped, so we use read -r
    # and directly load all the lines that begin with $cur
    # inside the COMPREPLY array, which is the one where we put the
    # completion candidates

    if ([ $COMP_CWORD = 1 ] || [ $COMP_CWORD = 2 ]) && [ -e ~/.cache/vasttrafik-cli-stops ]; then
        while read -r i; do
            COMPREPLY+=("$i")
        done < <(grep "^$cur" ~/.cache/vasttrafik-cli-stops)
    elif [ $COMP_CWORD = 3 ]; then
        COMPREPLY=( $(compgen -W "depart arrive" -- ${cur}) )
    fi
    return 0
}

complete -F _trip_tab_complete trip-vgr
