#! /usr/bin/env perl

use warnings;
use strict;

use YAML::Tiny;
use Data::Dumper;

my @contents = <>;
chomp @contents;
my $info;

# a group is defined as 10+ pound signs, some text, then 10+ pound signs
my $currentGroup = 'Unknown';
my $record;
my $host = '';
my $currentLine = 0;
# skip anything up until we find the first entry
# first entry is defined as
# constant 'host' in first column
# 1 or more spaces
# a token (word) of alphanumeric, period and dash
# no trailing blank space
while ( $currentLine < @contents ) {
   last if $contents[$currentLine] =~ m/^host\s+[a-z0-9.-]+$/i;
   $currentLine++;
}

while ( $currentLine < @contents ) {
   if ( $contents[$currentLine] =~ m/^#{10,}\s([^#]+)#{10,}$/ ) {
      # special kind of comment used for grouping
      $currentGroup = $1;
      $currentGroup =~ s/^\s+|\s+$//g
   } elsif ( $contents[$currentLine] =~ m/^\s*#?\s*$/ ) { # and, not a blank line
      $currentLine++;
      next;
   } elsif ( $contents[$currentLine] =~ m/^#/ ) {
      $currentLine++;
      next;
   } elsif ( $contents[$currentLine] =~ m/([a-z]+)[\s=]+([a-z0-9.-]+)/i ) {
      my $key = lc $1;
      my $value = lc $2;
      if ( $key eq 'host' ) {
         # if host already defined, this is a new record
         $host = $value;
      } else {
         $info->{$currentGroup}->{$host}->{$key} = $value;
      }
   }
   $currentLine++;
}
#die Dumper( $info );

print Dump( $info );

#print STDERR scalar( keys @$info ) . " Entried Processed\n";
1;
