#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG 7.1				#
#							#
# Licensed under GNU LESSER GENERAL PUBLIC LICENSE v3	#
#							#
# Original Bash Completion Functions are licensed under	#
# 	   the GNU GENERAL PUBLIC LICENSE v2		#
#							#
# Copyright 2007 - 2008 Christopher Bratusek		#
#							#
#########################################################

shopt -s progcomp
shopt -s extglob

# Only list directories as possible matches for cd

# Only directories for mkdir and rmdir
complete -d mkdir rmdir -pushd cd

# Only Users for user-commands
complete -u su usermod userdel newusers

# Only groups for group-commands
complete -g groupmod groupdel newgr

# Stopped jobs for bg
complete -A stopped -P '"%' -S '"' bg

# Other job-commands
complete -j -P '"%' -S '"' fg jobs disown

# Only variables for variable-commands
complete -v readonly unset

# Completion for shopt
complete -A shopt shopt

# Aliases for unalias
complete -a unalias

# Commands for command-commands (takes some time on first completion)
complete -c command type which

# Only builtins for builtin
complete -b builtin

# Original Bash-Completion Functions

_get_cword()
{
	if [[ "${#COMP_WORDS[COMP_CWORD]}" -eq 0 ]] || [[ "$COMP_POINT" == "${#COMP_LINE}" ]]; then
		echo "${COMP_WORDS[COMP_CWORD]}"
	else
		local i
		local cur="$COMP_LINE"
		local index="$COMP_POINT"
		for (( i = 0; i <= COMP_CWORD; ++i )); do
			while [[ "${#cur}" -ge ${#COMP_WORDS[i]} ]] && [[ "${cur:0:${#COMP_WORDS[i]}}" != "${COMP_WORDS[i]}" ]]; do
				cur="${cur:1}"
				index="$(( index - 1 ))"
			done
			if [[ "$i" -lt "$COMP_CWORD" ]]; then
				local old_size="${#cur}"
				cur="${cur#${COMP_WORDS[i]}}"
				local new_size="${#cur}"
				index="$(( index - old_size + new_size ))"
			fi
		done
		
		if [[ "${COMP_WORDS[COMP_CWORD]:0:${#cur}}" != "$cur" ]]; then
			echo "${COMP_WORDS[COMP_CWORD]}"
		else
			echo "${cur:0:$index}"
		fi
	fi
}

_filedir()
{
	local IFS=$'\t\n' xspec

	_expand || return 0

	local toks=( ) tmp
	while read -r tmp; do
		[[ -n $tmp ]] && toks[${#toks[@]}]=$tmp
	done < <( compgen -d -- "$(quote_readline "$cur")" )
	
	if [[ "$1" != -d ]]; then
		xspec=${1:+"!*.$1"}
		while read -r tmp; do
			[[ -n $tmp ]] && toks[${#toks[@]}]=$tmp
		done < <( compgen -f -X "$xspec" -- "$(quote_readline "$cur")" )
	fi

	COMPREPLY=( "${COMPREPLY[@]}" "${toks[@]}" )
}

_expand()
{
	if [[ "$cur" == \~*/* ]]; then
		eval cur=$cur
	elif [[ "$cur" == \~* ]]; then
		cur=${cur#\~}
		COMPREPLY=( $( compgen -P '~' -u $cur ) )
		return ${#COMPREPLY[@]}
	fi
}

# O.K. Back to our own Functions

_tar()
{
	local cur

	COMPREPLY=()
	cur=`_get_cword`

	if [ $COMP_CWORD -eq 1 ]; then
		COMPREPLY=( $( compgen -W 'a A b B c C d f F g G h i j k K l L m M N o O p P r R s S t T u v V w W x X z Z' -- $cur ) )
	else
		_filedir '@(tar|tgz|tbz2|tbz|tar.bz2|tar.Z|tar.gz|tar.lzma)'
	fi

	return 0

}
complete -F _tar -o filenames tar

_bzip2()
{

	local cur
	
	COMPREPLY=()
	cur=`_get_cword`
	
	if [ $COMP_CWORD -eq 1 ]; then
		COMPREPLY=( $( compgen -W '1 2 3 4 5 6 7 8 9 c d f k L q s t v z' ) )
	else
		_filedir '@tar'
	fi
	
	return 0
}
complete -F _bzip2 -o filenames bzip2

_bunzip2()
{

	local cur
	
	COMPREPLY=()
	cur=`_get_cword`
	
	if [ $COMP_CWORD -eq 1 ]; then
		COMPREPLY=( $( compgen -W '1 2 3 4 5 6 7 8 9 c d f k L q s t v z' ) )
	else
		_filedir '@(tar.bz2|tbz2|tbz|bz2|bzip|bzip2|bz)'
	fi
	
	return 0

}
complete -F _bunzip2 -o filenames bunzip2 bzcat bzgrep bzmore bzdiff

_gzip()
{
	local cur
	
	COMPREPLY=()
	cur=`_get_cword`
	
	if [ $COMP_CWORD -eq 1 ]; then
		COMPREPLY=( $( compgen -W '1 2 3 4 5 6 7 8 9 c d f h l L n N q r S t v V' ) )
	else
		_filedir '@tar'
	fi
	
	return 0

}
complete -F _gzip -o filenames gzip

_gunzip()
{

	local cur
	
	COMPREPLY=()
	cur=`_get_cword`
	
	if [ $COMP_CWORD -eq 1 ]; then
		COMPREPLY=( $( compgen -W '1 2 3 4 5 6 7 8 9 c d f h l L n N q r S t v V' ) )
	else
		_filedir '@(tgz|tar.gz|tar.Z|gz|gzip)'
	fi
	
	return 0

}
complete -F _gunzip -o filenames gunzip
