#!/bin/bash
_stops_tab_complete() {
    local cur
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"

    # 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 ] && [ -e ~/.cache/vasttrafik-cli-stops ]; then
        while read -r i; do
            COMPREPLY+=("$i")
        done < <(grep "^$cur" ~/.cache/vasttrafik-cli-stops)
    fi
    return 0
}

complete -F _stops_tab_complete stops
