# -*- mode: shell-script; sh-basic-offset: 8; indent-tabs-mode: t -*-
# ex: ts=8 sw=8 noet filetype=sh
#
# bash completion for shadow utils

have useradd &&
_useradd()
{
	local cur prev split=false

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_split_longopt && split=true

	# TODO: if -o/--non-unique is given, could complete on existing uids
	#       with -u/--uid

	case "$prev" in
		-c|--comment|-h|--help|-e|--expiredate|-f|--inactive|-k|--key|-p|--password|-u|--uid|-Z|--selinux-user)
			return 0
			;;
		-b|--base-dir|-d|--home|-k|--skel)
			_filedir -d
			return 0
			;;
		-g|--gid)
			_gids
			[ -n "$bash205" ] && \
				COMPREPLY=( "${COMPREPLY[@]}" $( compgen -g ) )
			COMPREPLY=( $( compgen -W '${COMPREPLY[@]}' -- $cur ) )
			return 0
			;;
		-G|--groups)
			[ -n "$bash205" ] && \
				COMPREPLY=( $( compgen -g -- $cur ) )
			return 0
			;;
		-s|--shell)
			_shells
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-b --base-dir -c --comment -d --home\
			-D --defaults -e --expiredate -f --inactive -g --gid \
			-G --groups -h --help -k --skel -K --key -l -M \
			-m --create-home -N --no-user-group -o --non-unique \
			-p --password -r --system -s --shell -u --uid \
			-U --user-group -Z --selinux-user' -- $cur ) )
		return 0
	fi
} &&
complete -F _useradd useradd

have usermod &&
_usermod()
{
	local cur prev split=false

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_split_longopt && split=true

	# TODO: if -o/--non-unique is given, could complete on existing uids
	#       with -u/--uid

	case "$prev" in
		-c|--comment|-d|--home|-e|--expiredate|-f|--inactive|-h|--help|-l|--login|-p|--password|-u|--uid|-Z|--selinux-user)
			return 0
			;;
		-g|--gid)
			_gids
			[ -n "$bash205" ] && \
				COMPREPLY=( "${COMPREPLY[@]}" $( compgen -g ) )
			COMPREPLY=( $( compgen -W '${COMPREPLY[@]}' -- $cur ) )
			return 0
			;;
		-G|--groups)
			[ -n "$bash205" ] && \
				COMPREPLY=( $( compgen -g -- $cur ) )
			return 0
			;;
		-s|--shell)
			_shells
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		# TODO: -U/--unlock, -p/--password, -L/--lock mutually exclusive
		COMPREPLY=( $( compgen -W '-a --append -c --comment -d --home \
			-e --expiredate -f --inactive -g --gid -G --groups \
			-h --help -l --login -L --lock -o --non-unique \
			-p --password -s --shell -u --uid -U --unlock \
			-Z --selinux-user' -- $cur ) )
		return 0
	fi

	COMPREPLY=( $( compgen -u -- $cur ) )
} &&
complete -F _usermod usermod

have userdel &&
_userdel()
{
	local cur

	COMPREPLY=()
	cur=`_get_cword`

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-f --force -h --help -r --remove' \
			-- $cur ) )
		return 0
	fi

	COMPREPLY=( $( compgen -u -- $cur ) )
} &&
complete -F _userdel userdel

have chage &&
_chage()
{
	local cur prev split=false

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_split_longopt && split=true

	case "$prev" in
		-d|--lastday|-E|--expiredate|-h|--help|-I|--inactive|-m|--mindays|-M|--maxdays|-W|--warndays)
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-d --lastday -E --expiredate \
			-h --help -I --inactive -l --list -m --mindays \
			-M --maxdays -W --warndays' -- $cur ) )
		return 0
	fi

	COMPREPLY=( $( compgen -u -- $cur ) )
} &&
complete -F _chage chage

have passwd &&
_passwd()
{
	local cur prev

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case "$prev" in
		-n|-x|-w|-i|-\?|--help|--usage)
			return 0
			;;
	esac

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-k -l --stdin -u -d -n -x -w -i -S \
			-? --help --usage' -- $cur ) )
		return 0
	fi

	COMPREPLY=( $( compgen -u -- $cur ) )
} &&
complete -F _passwd passwd

have chpasswd &&
_chpasswd()
{
	local cur prev split=false

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_split_longopt && split=true

	case "$prev" in
		-c|--crypt)
			COMPREPLY=( $( compgen -W 'DES MD5 NONE SHA256 SHA512' \
				-- $cur ) )
			return 0
			;;
		-s|--sha-rounds)
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-c --crypt-method -e --encrypted \
			-h --help -m --md5 -s --sha-rounds' -- $cur ) )
		return 0
	fi
} &&
complete -F _chpasswd chpasswd

have newusers &&
_newusers()
{
	local cur prev split=false

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_split_longopt && split=true

	case "$prev" in
		-c|--crypt)
			COMPREPLY=( $( compgen -W 'DES MD5 NONE SHA256 SHA512' \
				-- $cur ) )
			return 0
			;;
		-s|--sha-rounds)
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-c --crypt-method --r --system \
			-s --sha-rounds' -- $cur ) )
		return 0
	fi

	_filedir
} &&
complete -F _newusers $filenames newusers

have pwck &&
_pwck()
{
	local cur

	COMPREPLY=()
	cur=`_get_cword`

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-q -r -s' -- $cur ) )
		return 0
	fi

	_filedir
} &&
complete -F _pwck $filenames pwck

have groupadd &&
_groupadd()
{
	local cur prev split=false

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_split_longopt && split=true

	# TODO: if -o/--non-unique is given, could complete on existing gids
	#       with -g/--gid

	case "$prev" in
		-g|--gid|-K|--key|-p|--password)
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-f --force -g --gid -h --help \
			-K --key -o --non-unique -p --password -r --system' \
			-- $cur ) )
		return 0
	fi
} &&
complete -F _groupadd groupadd

have groupmod &&
_groupmod()
{
	local cur prev split=false

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_split_longopt && split=true

	# TODO: if -o/--non-unique is given, could complete on existing gids
	#       with -g/--gid

	case "$prev" in
		-g|--gid|-h|--help|-n|--new-name|-p|--password)
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-g --gid -h --help -n --new-name \
			-o --non-unique -p --password' -- $cur ) )
		return 0
	fi

	[ -n "$bash205" ] && COMPREPLY=( $( compgen -g -- $cur ) )
} &&
complete -F _groupmod groupmod

[ -n "$bash205" ] && complete -g groupdel

have newgrp &&
_newgrp()
{
	COMPREPLY=()
	if [[ "`_get_cword`" == "-" ]]; then
		COMPREPLY=( - )
	fi
} &&
complete ${bash205:+-g} -F _newgrp newgrp

have gpasswd &&
_gpasswd()
{
	local cur prev

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case "$prev" in
		-a|-d|-A|-M)
			COMPREPLY=( $( compgen -u -- $cur ) )
			return 0
			;;
	esac

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-a -d -r -R -A -M' -- $cur ) )
		return 0
	fi

	[ -n "$bash205" ] && COMPREPLY=( $( compgen -g -- $cur ) )
} &&
complete -F _gpasswd gpasswd

have groupmems &&
_groupmems()
{
	local cur prev

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case "$prev" in
		-a|-d)
			COMPREPLY=( $( compgen -u -- $cur ) )
			return 0
			;;
		-g)
			[ -n "$bash205" ] && \
				COMPREPLY=( $( compgen -g -- $cur ) )
			return 0
			;;
	esac

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-a -d -p -g -l' -- $cur ) )
		return 0
	fi
} &&
complete -F _groupmems groupmems

have grpck &&
_grpck()
{
	local cur

	COMPREPLY=()
	cur=`_get_cword`

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-r -s' -- $cur ) )
		return 0
	fi

	_filedir
} &&
complete -F _grpck $filenames grpck

have vipw || have vigr &&
_vipw()
{
	local cur prev

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	case "$prev" in
		-h|--help)
			return 0
			;;
	esac

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-g --group -h --help -p --passwd \
			-q --quiet -s --shadow' -- $cur ) )
		return 0
	fi
} &&
complete -F _vipw vipw vigr

have faillog &&
_faillog()
{
	local cur prev split=false

	COMPREPLY=()
	cur=`_get_cword`
	prev=${COMP_WORDS[COMP_CWORD-1]}

	_split_longopt && split=true

	case "$prev" in
		-h|--help|-l|--lock-time|-m|--maximum|-t|--time)
			return 0
			;;
		-u|--user)
			COMPREPLY=( $( compgen -u -- $cur ) )
			return 0
			;;
	esac

	$split && return 0

	if [[ "$cur" == -* ]]; then
		COMPREPLY=( $( compgen -W '-a --all -h --help -l --lock-time \
			-m --maximum -r --reset -t --time -u --user' -- $cur ) )
		return 0
	fi
} &&
complete -F _faillog faillog
