#!/usr/bin/env perl
use v5.18;

use Getopt::Long;
use App::p5find qw(p5_doc_iterator);

sub print_usage {
    print <<USAGE;
Usage: p5find-regex [switches] [--] [dir...]
  -h    show this help message.
  -o    Print only the matching regex
USAGE
}

my %opts;
GetOptions(
    \%opts,
    "h", # No op
    "o", # Print only the Regexp, not the entire line.
);

if ($opts{h}) {
    print_usage();
    exit(0);
}

my @args = @ARGV;
@args = (".") unless @args;

for my $dir (@args) {
    my $iter = p5_doc_iterator($dir);
    while ( defined ( my $doc = $iter->() ) ) {
        my $regexps = $doc->find("PPI::Token::Regexp") or next;
        my $file = $doc->filename;

        if ($opts{o}) {
            for my $it (@$regexps) {
                my $ln = $it->line_number;
                print "${file}:${ln}:\t$it\n";
            }

        } else {
            my %matched;
            for my $it (@$regexps) {
                my $ln = $it->line_number;
                $matched{$ln} = 1;
            }

            my $line_number = 0;
            open my $fh, "<", $file;
            while (my $line = <$fh>) {
                $line_number++;
                if ($matched{$line_number}) {
                    print "${file}:${line_number}:${line}";
                }
            }
            close($fh);
        }
    }
}
