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

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

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

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

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

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

_clufter() {
	local commands="ccs2pcs ccs-obfuscate ccs-revitalize ccs-artefacts ccs2pcs-needle ccs2pcs-flatiron"
	local opts_common="-q --quiet --color --loglevel --logfile -d --debug --sys --dist"
	local opts_main="-h --help -H --help-full -v --version -s --skip-ext -e --ext -l --list --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
	    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
