# /usr/share/bash-completion/completions/ppa-purge
# Debian ppa-purge(1) completion -*- shell-script -*-

while true; do

# Hack for given strings $2,$3,... possibly being in $1 and $COMP_WORDBREAKS
# Only the part after each match is listed as a completion.
_ltrim_completions ()
{
	local cur=$1; shift
	while [[ ${1+x} ]]; do
		if [[ "$cur" == *$1* && "$COMP_WORDBREAKS" == *$1* ]]; then
			local x_word=${cur%"${cur##*$1}"}
			local i=${#COMPREPLY[*]}
			while [[ $((--i)) -ge 0 ]]; do
				COMPREPLY[$i]=${COMPREPLY[$i]#"$x_word"}
			done
		fi
		shift
    done
} || break

_ppa_purge_update_args(){
	local i word last=$((${#words[@]}-1)) prevword=
	for (( i=1; i < last; i++ )) ; do
		word=${words[i]}
		[[ $word != $cur ]] &&
		case "$word" in
			http://*) url=$word ;;
			ppa:*) ppa=$word ;;
			-*) ;;
			?*) [[ $prevword =~ ^-([^-]*v|-verbose)$ ]] || dist=$word
		esac
		prevword=$word
	done
} || break

_repo_dist(){
	[[ $url ]] &&
	 sed -rn \
's#^[[:blank:]]*deb(-src)?([[:blank:]]+\[.+\])?[[:blank:]]+'"$url"'/?[[:blank:]]+([^[:blank:]]+).*#\3#p' \
/etc/apt/sources.list.d/*.list /etc/apt/sources.list ||
	 sed -rn \
's#^[[:blank:]]*deb(-src)?([[:blank:]]+\[.+\])?[[:blank:]]+http://[^[:blank:]]+[[:blank:]]+([^[:blank:]]+).*#\3#p' \
/etc/apt/sources.list.d/*.list /etc/apt/sources.list
} || break

_repo_list(){
	[[ $dist ]] &&
	 sed -rn \
's#^[[:blank:]]*deb(-src)?([[:blank:]]+\[.+\])?[[:blank:]]+(http://[^[:blank:]]+)[[:blank:]]+'"$dist"'[[:blank:]]+.*#\3#p' \
/etc/apt/sources.list.d/*.list /etc/apt/sources.list | sed -e '/http:\/\/ppa\.launchpad\.net/d' -e 's#/$##' ||
	 sed -rn \
's#^[[:blank:]]*deb(-src)?([[:blank:]]+\[.+\])?[[:blank:]]+(http://[^[:blank:]]+).*#\3#p' \
/etc/apt/sources.list.d/*.list /etc/apt/sources.list | sed -e '/http:\/\/ppa\.launchpad\.net/d' -e 's#/$##'
} || break

_ppa_list(){
	sed -rn \
's#^[[:blank:]]*deb(-src)?([[:blank:]]+\[.+\])?[[:blank:]]+http://ppa\.launchpad\.net/([^/[:blank:]]+/[^/[:blank:]]+).*#\3#p' \
/etc/apt/sources.list.d/*.list /etc/apt/sources.list |
	sed -e 's#^[^/]*$#&/#' -e 's#.*#ppa:&#'
} || break

_ppa_purge(){
	local cur prev words cword opts url= ppa= dist= wordlist
	# Do not treat : and = as word breaks even if they are in $COMP_WORDBREAKS:
	_init_completion -n : -n = || return

	opts=" --help --simulate --no-initial-update --yes --version --remove \
--verbose=0 --verbose=1 --verbose=2 --figure-soname-bumps"
#  short options are redundant and not that informative
	opts+=' -h -s -u -y -r -v0 -v1 -v2 -V'

	[[ $prev =~ ^-([^-]*v|-verbose)$ ]] && wordlist='0 1 2' || {
		[[ $cur = -* ]] && wordlist=$opts || {
			_ppa_purge_update_args
			if [[ $ppa ]]; then
				wordlist='$opts'
			elif [[ $url ]]; then
				[[ $dist ]] && wordlist='$opts' || wordlist='$(_repo_dist)'
			elif [[ $dist ]]; then
				wordlist='$(_repo_list)'
			else
				wordlist='$(_ppa_list) $(_repo_list) $(_repo_dist)'
			fi
		}
	}
	COMPREPLY=( $( compgen -W "$wordlist" -- "$cur" ) )
	_ltrim_completions "$cur" : =
	return 0
} && complete -F _ppa_purge ppa-purge
break
done
