#!/usr/bin/perl
# Author: Nick Anderson <nick@cmdln.org>, Ted Zlatanov <tzz@lifelogs.com>
# locate and show bundles or bodies in a list of paths

use strict;
use warnings;
use File::Find;
use File::Basename;
use Term::ANSIColor;
use Getopt::Long;

my %options = (
               full => 0,
               help => 0,
               plain => 0,
               color => {
                         info => 'green',
                         heading => 'red',
                         body => 'yellow',
                        },
              );

GetOptions(\%options,
           "help|h!",
           "full|f!",
           "plain|p!",
           "color|c=s%",
          );

my $full = 0;
my $regex = shift @ARGV;
my $rc = 0;

unless (defined $regex)
{
    $rc = 1;
    $options{help} = 1;
}

if ($options{help})
{
    print "Syntax: $0 [-f|--full] [-p|--plain] [-h|--help] [-c|--color [info|heading|body]=color] BODY-OR-BUNDLE-REGEX PATH1 PATH2 ...\n\n";

    # try to open README.md
    if (open my $doc, '<', sprintf('%s/%s', dirname($0), 'README.md'))
    {
        while (<$doc>)
        {
            print;
        }
    }

    exit $rc;
}

if ($options{plain})
{
    $options{color} = {};
}

my @search = @ARGV;

# default to this directory
@search = ('/var/cfengine/masterfiles')
 unless scalar @search;

find(\&wanted, @search);

sub find_color
{
    my $type = shift @_;
    return unless defined $type;
    return $options{color}->{$type};
}

sub wrap_color
{
    my $type = shift @_;
    my $text = shift @_;

    my $c = find_color($type);
    return $text unless defined $c;
    return color($c) . $text . color("reset");
}

sub wanted
{
    open my $f, '<', $_;
    my $found = 0;

    while (<$f>)
    {
        my $syntax = 'body';
        if (m/^\s*(body|bundle).*$regex/)
        {
            if ($options{plain})
            {
                print "\n";
            }
            else
            {
                print wrap_color('info',
                                 "-> body or bundle matching '$regex' found in $File::Find::name:$.\n")
            }

            $found = 1;
            $syntax = 'heading';
        }

        if ($found)
        {
            print wrap_color($syntax, $_);
        }

        if (!$options{full} || m/^\s*}\s*$/)
        {
            $found = 0;
        }
    }
}
