#!/bin/bash

type _comp_filter &> /dev/null || return
type _comp_contains &> /dev/null || return
type _comp_filter_shorts &> /dev/null || return

function _tricot() {

    local cur prev opts

    _init_completion || return
    COMPREPLY=()

    # No completion
	if _comp_contains "--continue-from --exclude-ids --exclude-groups --groups --ids --positionals --skip-until --variables" $prev; then
		return 0

    # Complete template modes
	elif _comp_contains "--template" $prev; then
        opts="plugin tester validator extractor"

    # Options with filename completions
	elif _comp_contains "--load --logfile" $prev; then
        _filedir
        return 0

	# Current word starts with -*
    elif [[ "$cur" == -* ]]; then
		opts="--help"
		opts="$opts --continue-from"
		opts="$opts --debug"
		opts="$opts --exclude-ids"
		opts="$opts --exclude-groups"
		opts="$opts --groups"
		opts="$opts --ids"
		opts="$opts --logfile"
		opts="$opts --load"
		opts="$opts --positionals"
		opts="$opts --quite"
		opts="$opts --skip-until"
		opts="$opts --template"
		opts="$opts --variables"
		opts="$opts --verbose"

    # Default to filename completion
    else
        _filedir
        return 0
    fi

    _comp_filter "opts"
    _comp_filter_shorts "opts"

	mapfile -t COMPREPLY < <(compgen -W "${opts}" -- "${cur}") 
	return 0
}

complete -F _tricot tricot
