#compdef ppdb

! test -z $PUPPET_PATH || export PUPPET_PATH=$(grep "\(local\|base\)path" $HOME/.config/hieracles/config.yml | cut -d' ' -f2 | sed -e "s/\\\"//g")

! test -z $PUPPET_PATH || export PUPPET_PATH=.

if [ -d "$PUPPET_PATH/enc" ]
then
  _ppdb() {
    local curcontext="$curcontext" state line
    typeset -A opt_args
    _arguments -C \
      ':command:->command' \
      '*::options:->options'
    case $state in
      (command)
        local -a endpoints
        endpoints=(
          'node:query information about given node.'
          'facts:query facts about given node.'
          'same:find node that have the same value for a given fact than a given node.'
          'resources:query information about resources.'
        )
        _describe -t commands 'ppdb' endpoints
      ;;
      (options)
        case $line[1] in
          (node)
            __ppdb-node
          ;;
          (facts)
            __ppdb-facts
          ;;
          (same)
            __ppdb-same
          ;;
          (factnames)
          ;;
          (resources)
          ;;
        esac
      ;;
    esac
  }
  __ppdb-node () {
    local curcontext="$curcontext" state line
    typeset -A opt_args
    _arguments -C \
      ':command:->command' \
      ':nodes:->nodes'
    case $state in
      (command)
        local -a subcommands
        subcommands=(
          'info:gives node information.'
          'facts:lists facts for the node.'
          'resources:list resoruces recorded for the node.'
        )
        _describe -t commands 'ppdb node' subcommands
      ;;
      (nodes)
        local expl
        declare -a nodes
        nodes=(${$(find $PUPPET_PATH/enc -print | xargs -- basename -s | sed -e 's~\.yaml~~')})
        _wanted nodes expl 'nodes' compadd $nodes
      ;;
    esac
  }
  __ppdb-same() {
    local curcontext="$curcontext" state line
    typeset -A opt_args
    _arguments -C \
      ':name:->name' \
      ':nodes:->nodes'
    case $state in
      (nodes)
        local expl
        declare -a nodes
        nodes=(${$(find $PUPPET_PATH/enc -print | xargs -- basename -s | sed -e 's~\.yaml~~')})
        _wanted nodes expl 'nodes' compadd $nodes
      ;;
    esac
  }
  _ppdb
fi