# bash completion for shadow utils

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

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _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
            COMPREPLY=( $( compgen -W '${COMPREPLY[@]} $( compgen -g )' \
                -- "$cur" ) )
            return 0
            ;;
        -G|--groups)
            COMPREPLY=( $( compgen -g -- "$cur" ) )
            return 0
            ;;
        -s|--shell)
            _shells
            return 0
            ;;
    esac

    $split && return 0

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--base-dir --comment --home-dir --defaults \
            --expiredate --inactive --gid --groups --help --skel --key \
            --no-log-init --create-home --no-create-home --no-user-group \
            --non-unique --password --system --shell --uid --user-group \
            --selinux-user' -- "$cur" ) )
        return 0
    fi
} &&
complete -F _useradd useradd

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

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _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
            COMPREPLY=( $( compgen -W '${COMPREPLY[@]} $( compgen -g )' \
                -- "$cur" ) )
            return 0
            ;;
        -G|--groups)
            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 '--append --comment --home --expiredate \
            --inactive --gid --groups --help --login --lock --move-home \
            --non-unique --password --shell --uid --unlock --selinux-user' \
            -- "$cur" ) )
        return 0
    fi

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

have userdel &&
_userdel()
{
    local cur

    COMPREPLY=()
    _get_comp_words_by_ref cur

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

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

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

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _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 '--lastday --expiredate --help --inactive \
            --list --mindays --maxdays --warndays' -- "$cur" ) )
        return 0
    fi

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

have passwd &&
_passwd()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    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=()
    _get_comp_words_by_ref cur prev

    _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 '--crypt-method --encrypted \
            --help --md5 --sha-rounds' -- "$cur" ) )
        return 0
    fi
} &&
complete -F _chpasswd chpasswd

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

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _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 '--crypt-method --help --system \
            --sha-rounds' -- "$cur" ) )
        return 0
    fi

    _filedir
} &&
complete -F _newusers -o filenames newusers

have pwck &&
_pwck()
{
    local cur

    COMPREPLY=()
    _get_comp_words_by_ref cur

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

    _filedir
} &&
complete -F _pwck -o filenames pwck

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

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _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 '--force --gid --help \
            --key --non-unique --password --system' -- "$cur" ) )
        return 0
    fi
} &&
complete -F _groupadd groupadd

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

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _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 '--gid --help --new-name \
            --non-unique --password' -- "$cur" ) )
        return 0
    fi

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

complete -g groupdel

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

have gpasswd &&
_gpasswd()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    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

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

have groupmems &&
_groupmems()
{
    local cur prev

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    case $prev in
        -a|-d)
            COMPREPLY=( $( compgen -u -- "$cur" ) )
            return 0
            ;;
        -g)
            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=()
    _get_comp_words_by_ref cur

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

    _filedir
} &&
complete -F _grpck -o filenames grpck

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

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

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

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

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

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _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 '--all --help --lock-time \
            --maximum --reset --time --user' -- "$cur" ) )
        return 0
    fi
} &&
complete -F _faillog faillog

have lastlog &&
_lastlog()
{
    local cur prev split=false

    COMPREPLY=()
    _get_comp_words_by_ref cur prev

    _split_longopt && split=true

    case $prev in
        -b|--before|-h|--help|-t|--time)
            return 0
            ;;
        -u|--user)
            COMPREPLY=( $( compgen -u -- "$cur" ) )
            return 0
            ;;
    esac

    $split && return 0

    if [[ "$cur" == -* ]]; then
        COMPREPLY=( $( compgen -W '--before --help --time --user' -- "$cur" ) )
        return 0
    fi
} &&
complete -F _lastlog lastlog

# Local variables:
# mode: shell-script
# sh-basic-offset: 4
# sh-indent-comment: t
# indent-tabs-mode: nil
# End:
# ex: ts=4 sw=4 et filetype=sh
