#! /usr/bin/perl
use warnings;
use strict;
use integer;
use FindBin;
use lib $FindBin::RealBin;
use Def;

# This script scans the Package, Provides and Sources fields of a
# Packages file for words containing the words you list on the command
# line.
#
# You probably won't use this script much, but it may prove useful if
# you want a full list of words to search for when searching
# dependencies.  For example, when searching for Perl dependencies, you
# might not think to include "perlapi" but to exclude "hyperlatex" in
# your search.  This script will at least remind you of both words.
#
# The script scans case-insensitively.  It is insensitive to the cases
# of the words you supply.

our $usage = <<END;
usage: $0 [-h] [Packages file] [word]...
    -h print this usage message
END

# Read command-line arguments and options.
my @opt;
my @arg;
{
  my $stopopt = '';
  for ( @ARGV ) {
    if    ( $stopopt  ) { push @arg, $_        }
    elsif ( $_ eq '-' ) { $stopopt = '1'       }
    else  { push @{ /^-/ ? \@opt : \@arg }, $_ }
  }
}
my %opt = map { my @o = split ''; shift @o; map {$_=>1} @o; } @opt;
if ( $opt{'?'} || $opt{h} || @arg < 1 ) { print $usage; exit 0; }

my %word;
my $fn_packages = shift @arg;
$_ = quotemeta for @arg;

open  P, '<', $fn_packages;
  while (<P>) {
    my( $text ) = /^(?:Package|Source|Provides):(.*)$/ or next;
    for my $word ( $text =~ /\b\w+\b/g ) {
      for my $w ( @arg ) {
        $word{$word} = 1 if $word =~ /$w/i;
      }
    }
  }
close P;

print "$_\n" for sort keys %word;

