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

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

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

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

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

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

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

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

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

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

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

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

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

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

_clufter() {
	local commands="ccs2pcscmd
	pcs2pcscmd
	ccs2pcs
	ccs2pcs-needle
	ccs-obfuscate
	ccs-revitalize
	cib-revitalize
	pcs2pcscmd-needle
	cmd-wrap
	ccs2pcs-flatiron
	cib2pcscmd
	ccs2pcscmd-flatiron
	ccs-artefacts
	ccs2pcscmd-needle
	pcs2pcscmd-flatiron
	ccs-disable-rg"
	local opts_common="--sys --sys=
	--dist --dist=
	-q --quiet
	--color --color=
	-d --debug
	--logfile --logfile=
	--loglevel --loglevel="
	local opts_main="-h -h= --help --help=
	-H -H= --help-full --help-full=
	-l --list
	--list-dists --list-dists=
	-v --version
	-e --ext
	-s --skip-ext
	--completion-bash"
	local opts_nonmain="-h -h= --help --help=
	-H -H= --help-full --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
	    ccs2pcscmd) cur="ccs2pcscmd-needle";;
	    pcs2pcscmd) cur="pcs2pcscmd-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
