#!/usr/bin/env perl

#bundle-type (agent)
#    promise type (files)
#        body template (copy_from)
#            attribute (source, servers)
        

use strict;
use JSON;
my $attributearg = $ARGV[0];

my $cf_report = '/opt/cfengine/3.3.9/bin/cf-report';
my $cf3_Reference_info = '/home/cmdln/src/cfengine/core2/docs/reference/cf3-Reference-3.3.9.info';

my $cf_report_syntax_export = `$cf_report --syntax-export`;

my $json = new JSON->pretty->allow_nonref;

#my $json_text = $json->decode($cf_syntax_tree);
my $syntax = $json->decode($cf_report_syntax_export);


# Get all attributes for "copy_from in files" "files in agent promises" 
#while ((my $key, my $value) = each($json_text->{"bundle-types"}->{"agent"}->{"files"}->{"copy_from"})) {
#      print "$key\n";
#}

# Bundle Types
my @bundle_types = ();
my @body_templates = ();
my @attributes = ();

while ((my $bundle_type, my $value) = each($syntax->{"bundle-types"})) {
    print "$bundle_type\n";
  push(@bundle_types, "$bundle_type");
  
  # Body Templates
  while ((my $body_template, my $value) = each($syntax->{"bundle-types"}->{$bundle_type})) {
      #print "\t$body_template\n";
      print "\t$body_template in $bundle_type promises\n";
    push(@body_templates, "$body_template");

    # attributes
    while ((my $attribute, my $value) = each($syntax->{"bundle-types"}->{$bundle_type}->{$body_template})) {
        #print "\t\t$attribute\n";
        print "\t\ttexinfo nodename: \"$attribute in $body_template\"\n";
        if ($attributearg =~ m/$attribute/) {
            print "RUN: info -f $cf3_Reference_info -n \"$attribute in $body_template\"\n";
            system("info -f $cf3_Reference_info -n \"$attribute in $body_template\"");
        };
      push(@attributes, "$attribute");
    }
  }
}
