#
# Copyright 2012 Canonical Ltd.
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License version 3,
#    as published by the Free Software Foundation.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

ARGS_REQUIRE_FILE="--material --ssl-ca-file --cloud-config-file"

_landscape_api_commands()
{
    commands=$(landscape-api --help | sed -e '1,/^Actions:/d' -e '/^Type/Q' -e "s/^[ \t]\+\([a-z|]\+\)[ \t]\+.*/\1/g" -e 's/|/ /g' | uniq)
    echo "$commands"
}

_landscape_api_global_args()
{
    global_args=$( landscape-api help | sed -ne "s/^[[:space:]]*\(--[^[:space:]:]*\).*/\1/p")
    echo "$global_args"
}

_landscape_cmd_arguments()
{
    # Report only optional arguments and global arguments. Intentionally avoid
    # tab-completing positional argument names as the argument names can't be
    # specified on the command line.
    optional_args=$( landscape-api help $1 | sed -ne "s/^[[:space:]]*\(--[^[:space:]:]*\).*/\1/p")
    global_args=$(_landscape_api_global_args) 

    # Intentional trailing space for _filter_used_words()
    echo "$optional_args $global_args "
}

_filter_used_words()
{
  # Create an array of all_valid_args from string
  valid_args=$1
  shift
  declare -a words=${!1}
  for used in ${words[@]}; do 
      # Filter out each of the words we have already used
      valid_args=${valid_args/$used[^-[:alnum:]]/}
  done
  # report what hasn't been used
  echo "$valid_args"
}

_landscape_api()
{
    _get_comp_words_by_ref cur prev words
    global_args=$(_landscape_api_global_args)
    api_commands=$(_landscape_api_commands)

    # Have we already specified our command?
    command_specified=""
    for used in ${words[@]:1}; do
       if [ ! $command_specified ]; then
           for command in $api_commands; do
               if [ "$used" == "$command" ]; then
                   command_specified="$command"
                   break
               fi
           done
       fi
    done
    if [[ "$cur" == -* ]]; then
        valid_args="$global_args"
    else
        if [[ "$prev" == -* ]] && [[ "$prev" != "--json" ]]; then
            for filename_required in $ARGS_REQUIRE_FILE; do
                if [ "$prev" == "$filename_required" ]; then
                   # Tab complete any file or directory
                   _filedir 
                fi
            done
            # All remaining optional/global args require an additional value
            # before trying to auto-complete the next argument.
            return
        elif [ "${COMP_WORDS[1]}" != "help" ]; then 
            valid_args="$global_args $api_commands"
        fi
    fi
    if [ $COMP_CWORD -eq 1 ]; then
        valid_args+=" help"
    fi
 
    if [ $command_specified ]; then
        valid_args=$(_landscape_cmd_arguments $command_specified)
    fi
    filtered_args=$(_filter_used_words "$valid_args" words[@])
    COMPREPLY=($( compgen -W "$filtered_args" -- $cur ))
}

_have landscape-api && complete -F _landscape_api landscape-api

