#!perl
use 5.010;
use strict;
use warnings;

package csvgrep;

our $VERSION = '0.01';

package main;

use Text::CSV_XS;
use Text::Table::Tiny qw/ generate_table /;
use Getopt::Long;

my $usage_string     = "usage: $0 <pattern> <file>\n       $0 -d <dir> <pattern>\n";
my $case_insensitive = 0;
my $column_spec;


my ($pattern, $filename) = process_command_line();
show_matching_rows($pattern, $filename);
exit 0;


sub show_matching_rows
{
    my ($pattern, $filename) = @_;
    my @col_indices;
    my @rows;

    open(my $fh, '<:encoding(utf8)', $filename)
        || die "can't read $filename: $!\n";

    my $parser  = Text::CSV_XS->new();
    my @headers = @{ $parser->getline($fh) };

    if ($column_spec) {
        @col_indices = split(/,/, $column_spec);
        @headers    = @headers[@col_indices];
    }

    push(@rows, \@headers);

    while (<$fh>) {
        next unless ($case_insensitive && /$pattern/io)
                 || (!$case_insensitive && /$pattern/o);
        my $status = $parser->parse($_);
        my @columns = $parser->fields;
        if ($column_spec) {
            @columns = @columns[@col_indices];
        }
        push(@rows, [@columns]);
    }

    die "no match\n" unless @rows > 1;

    print generate_table(rows => \@rows, header_row => 1), "\n";
}


sub process_command_line
{
    my $dirpath;

    GetOptions(
        'ignore-case|i'     => \$case_insensitive,
        'columns|c=s'       => \$column_spec,
        'directory|d=s'     => \$dirpath,
    ) || die $usage_string;

    die $usage_string unless @ARGV == 2
                          || $dirpath;

    my $pattern  = shift @ARGV;

    my $filename = $dirpath
                 ? find_newest_file($dirpath)
                 : shift @ARGV;

    return ($pattern, $filename);
}

sub find_newest_file
{
    my $dirpath = shift;

    opendir(my $DIR, $dirpath)
        || die "can't read directory $dirpath: $!\n";

    my @files = map { $_->[0] }
                sort { $b->[1] <=> $a->[1] }
                map { [ $_, (stat("$dirpath/$_"))[9] ] } # mtime
                grep { /\.csv$/ } readdir($DIR);

    closedir($DIR);

    die "no .csv files found in $dirpath\n" unless @files > 0;

    return "$dirpath/$files[0]";
}

__END__

=head1 NAME

csvgrep - search for patterns in a CSV and display results in a table

=head1 SYNOPSIS

 csvgrep <pattern> <file>
 csvgrep -d <directory> <pattern>

=head1 DESCRIPTION

B<csvgrep> is a script that lets you look for a pattern in a CSV file,
and then displays the results in a text table.

The simplest usage is to look for a word in a CSV:

 csvgrep snorlax pokemon.csv

As with regular grep, you can use the B<-i> switch to make it
case-insensitive:

 csvgrep -i snorlax pokemon.csv

This assumes that the first row in the CSV is a header row,
and uses that as the header in the output.

=head1 AUTHOR

Neil Bowers E<lt>neilb@cpan.orgE<gt>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2017 by Neil Bowers <neilb@cpan.org>.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

