# bash completion start
# add me to ~/.profile persistently or eval on-the-fly in bash

_clufter_ccs2pcscmd_flatiron() {
	local opts="--input -i --output -o --force -f --noauth -n --silent -s --tmp-cib -t --dry-run -d --enable -e --start-wait -w --noguidance -g --text-width -x --nocheck --batch --editor --raw --noop --dump --profile"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_ccs2pcscmd_needle() {
	local opts="--input -i --output -o --force -f --noauth -n --silent -s --tmp-cib -t --dry-run -d --enable -e --start-wait -w --noguidance -g --text-width -x --nocheck --batch --editor --raw --noop --dump --profile"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_pcs2pcscmd_needle() {
	local opts="--coro -c --cib -i --output -o --force -f --noauth -n --silent -s --tmp-cib -t --dry-run -d --enable -e --start-wait -w --noguidance -g --text-width -x --nocheck --batch --editor --raw --noop --dump --profile"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_ccs_obfuscate() {
	local opts="--input -i --output -o --skip -s --noop --dump"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_cib_revitalize() {
	local opts="--input -i --output -o --noop --dump"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_pcs2pcscmd_flatiron() {
	local opts="--ccs -c --cib -i --output -o --force -f --noauth -n --silent -s --tmp-cib -t --dry-run -d --enable -e --start-wait -w --noguidance -g --text-width -x --nocheck --batch --editor --raw --noop --dump --profile"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_ccs_disable_rg() {
	local opts="--input -i --output -o --noversionbump -n --noop --dump"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_cmd_wrap() {
	local opts="--input -i --output -o --text-width -t --noop --dump"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_ccs2pcs_needle() {
	local opts="--input -i --coro -c --cib --nocheck --batch --editor --raw --noop --dump --profile"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_ccs2pcs_flatiron() {
	local opts="--input -i --ccs-pcmk -c --cib --nocheck --batch --editor --raw --noop --dump --profile"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_ccs_artefacts() {
	local opts="--input -i --output -o --noop --dump"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_ccs_revitalize() {
	local opts="--input -i --output -o --noop --dump"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter_cib2pcscmd() {
	local opts="--input -i --output -o --force -f --noauth -n --silent -s --tmp-cib -t --dry-run -d --enable -e --text-width -x --nocheck --batch --editor --raw --noop --dump --profile"
	
	[[ "$1" =~ -.* ]] && compgen -W "${opts}" -- $1
}

_clufter() {
	local commands="pcs2pcscmd ccs2pcscmd ccs2pcs ccs2pcscmd-flatiron ccs2pcscmd-needle pcs2pcscmd-needle ccs-obfuscate cib-revitalize pcs2pcscmd-flatiron ccs-disable-rg cmd-wrap ccs2pcs-needle ccs2pcs-flatiron ccs-artefacts ccs-revitalize cib2pcscmd"
	local opts_common="--sys --dist -q --quiet --color -d --debug --logfile --loglevel"
	local opts_main="-h --help -H --help-full -l --list --list-dists -v --version -e --ext -s --skip-ext --completion-bash"
	local opts_nonmain="-h --help -H --help-full"
	
	local cur fnc i=${COMP_CWORD}
	while true; do
	    test ${i} -eq 0 && break || let i-=1
	    cur=${COMP_WORDS[${i}]}
	    [[ "${cur}" =~ ^-.* ]] && continue
	    # handle aliases
	    case ${cur} in
	    pcs2pcscmd) cur="pcs2pcscmd-needle";;
	    ccs2pcscmd) cur="ccs2pcscmd-needle";;
	    ccs2pcs) cur="ccs2pcs-needle";;
	    esac
	    fnc=_clufter_${cur/-/_}
	    declare -f ${fnc} >/dev/null \
	     && COMPREPLY+=( $(${fnc} $2) ) \
	     || continue
	    [[ "$2" =~ ^-.* ]] \
	     && COMPREPLY+=( $(compgen -W "${opts_common} ${opts_nonmain}" -- $2) )
	    return
	done
	
	case "$2" in
	-*) COMPREPLY=( $(compgen -W "${opts_common} ${opts_main}" -- $2) );;
	*)  COMPREPLY=( $(compgen -W "${commands}" -- $2) );;
	esac
}

complete -o default -F _clufter clufter
