#!/bin/bash

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

_car() {

    local cur prev words cword cline opts arg args
    _init_completion || return

    _count_args "" "--cmd"
    _get_first_arg
    COMPREPLY=()

	if [[ $args -eq 1 ]]; then

        if [[ $cur == -* ]]; then
            opts="-h --help"
        else
            opts="run stop rm clean exec mirror list"
        fi


	elif [[ "$arg" == "run" ]]; then

        if [[ $cur == -* || $args -ge 3 ]]; then
            opts="-h --help --rm --rebuild"
        else
            opts=$(car list)
        fi
	

	elif [[ "$arg" == "exec" ]]; then

        if [[ $prev == "--cmd" ]]; then
            return 0
        elif [[ $cur == -* || $args -ge 3 ]]; then
            opts="-h --help --it --cmd"
        else
            opts=$(car list)
        fi


	elif [[ "$arg" == "list" ]]; then
        opts="-h --help"


	elif _comp_contains "stop mirror rm clean" $arg; then

        if [[ $cur == -* || $args -ge 3 ]]; then
            opts="-h --help"
        else
            opts=$(car list)
        fi


    else
        return 1
	fi

    _comp_filter "opts"
    _comp_filter_shorts "opts"

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

complete -F _car car
