#
# Copyright (c) 2016 Wind River Systems, Inc.
#
# SPDX-License-Identifier: Apache-2.0
#

#
# This file provides bash-completion functionality for the sw-patch CLI
#

function _swpatch()
{
    COMPREPLY=()
    local cur="${COMP_WORDS[COMP_CWORD]}"
    local prev="${COMP_WORDS[COMP_CWORD-1]}"
    local subcommand=${COMP_WORDS[1]}

    #
    #  The available sw-patch subcommands
    #
    local subcommands="
        apply
        commit
        delete
        query
        query-dependencies
        query-hosts
        remove
        show
        upload
        upload-dir
        what-requires
        drop-host
        is-applied
        report-app-dependencies
        query-app-dependencies
    "
    if [ -f /etc/platform/.initial_config_complete ]; then
        # Post-config, so the host-install commands are accessible
        subcommands="${subcommands} host-install host-install-async"
    else
        # Pre-config, so the install-local command is accessible
        subcommands="${subcommands} install-local"
    fi

    # Appends the '/' when completing dir names
    set mark-directories on

    if [ $COMP_CWORD -gt 1 ]; then
        #
        #  Complete the arguments to the subcommands.
        #
        case "$subcommand" in
            apply|delete|show|what-requires|is-applied)
                # Query the list of known patches
                local patches=$(sw-patch completion patches 2>/dev/null)
                COMPREPLY=( $(compgen -W "${patches}" -- ${cur}) )
                return 0
                ;;
            remove)
                # Query the list of known patches
                local patches=$(sw-patch completion patches 2>/dev/null)
                COMPREPLY=( $(compgen -W "--skipappcheck ${patches}" -- ${cur}) )
                return 0
                ;;
            host-install|host-install-async|drop-host)
                if [ "${prev}" = "${subcommand}" -o "${prev}" = "--force" ]; then
                    # Query the list of known hosts
                    local names=$(sw-patch completion hosts 2>/dev/null)
                    COMPREPLY=( $(compgen -W "${names}" -- ${cur}) )
                else
                    # Only one host can be specified, so no more completion
                    COMPREPLY=( $(compgen -- ${cur}) )
                fi
                return 0
                ;;
            upload)
                # Allow dirs and files with .patch extension for completion
                COMPREPLY=( $(compgen -f -o plusdirs -X '!*.patch' -- ${cur}) )
                return 0
                ;;
            upload-dir)
                # Allow dirs only for completion
                COMPREPLY=( $(compgen -d -- ${cur}) )
                return 0
                ;;
            query)
                if [ "${prev}" = "--release" ]; then
                    # If --release has been specified, provide installed releases for completion
                    local releases=$(/bin/ls -d /www/pages/feed/rel-* 2>/dev/null | sed 's#/www/pages/feed/rel-##')
                    COMPREPLY=( $(compgen -W "${releases}" -- ${cur}) )
                else
                    # --release is only completion option for query
                    COMPREPLY=( $(compgen -W "--release" -- ${cur}) )
                fi
                return 0
                ;;
            query-hosts|install-local)
                # These subcommands have no options/arguments
                COMPREPLY=( $(compgen -- ${cur}) )
                return 0
                ;;
            query-dependencies)
                # Query the list of known patches
                local patches=$(sw-patch completion patches 2>/dev/null)
                COMPREPLY=( $(compgen -W "--recursive ${patches}" -- ${cur}) )
                return 0
                ;;
            commit)
                if [ "${prev}" = "--release" ]; then
                    # If --release has been specified, provide installed releases for completion
                    local releases=$(/bin/ls -d /www/pages/feed/rel-* 2>/dev/null | sed 's#/www/pages/feed/rel-##')
                    COMPREPLY=( $(compgen -W "${releases}" -- ${cur}) )
                else
                    # Query the list of known patches
                    local patches=$(sw-patch completion patches 2>/dev/null)
                    COMPREPLY=( $(compgen -W "--all --dry-run --release ${patches}" -- ${cur}) )
                fi
                return 0
                ;;
            report-app-dependencies)
                if [ "${prev}" = "${subcommand}" ]; then
                    COMPREPLY=( $(compgen -W "--app" -- ${cur}) )
                elif [ "${prev}" = "--app" ]; then
                    COMPREPLY=
                else
                    local patches=$(sw-patch completion patches 2>/dev/null)
                    COMPREPLY=( $(compgen -W "${patches}" -- ${cur}) )
                fi
                return 0
                ;;
            query-app-dependencies)
                return 0
                ;;
            *)
                ;;
        esac
    fi

    # Provide subcommands for completion
    COMPREPLY=($(compgen -W "${subcommands}" -- ${cur}))  
    return 0
}

# Bind the above function to the sw-patch CLI
complete -F _swpatch -o filenames sw-patch

