_setmanctl()
{
    local cur first

    cur=${COMP_WORDS[COMP_CWORD]}
    first=${COMP_WORDS[1]}

    if [[ "${COMP_CWORD}" -eq 1 ]]; then
        compopt +o nospace
        COMPREPLY=($(compgen -W 'init default backup restore restore-on-boot status list-packages list-package-names list-setting-items wait-service-startup' -- ${cur}))
    else
        case ${first} in
            backup)
                if [[ "${COMP_CWORD}" -eq 2 ]]; then
                    COMPREPLY=( $( compgen -o nospace -d -S / -- ${cur} ))
                else
                    return 0
                fi
                ;;
            restore|restore-on-boot)
                COMPREPLY=( $( compgen -o nospace -d -S / -- ${cur}; compopt +o nospace; compgen -W '-f --force' -- ${cur} ))
                ;;
            wait-service-startup)
                packages=$(setmanctl list-package-names | tr '\n' ' ')
                COMPREPLY=( $( compgen -W "${packages}" -- ${cur} ))
                ;;
        esac
    fi
    return 0
}

complete -o nospace -F _setmanctl setmanctl
