The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl

# Driver for App::File::Grepper.

# Author          : Johan Vromans
# Created On      : Sat Apr 28 19:50:34 2012
# Last Modified By: Johan Vromans
# Last Modified On: Tue May 14 21:13:58 2013
# Update Count    : 32
# Status          : Unknown, Use with caution!

################ Common stuff ################

use strict;
use warnings;
use App::File::Grepper;

################ Setup  ################

# Process command line options, config files, and such.
my $options = app_setup( "afg", $App::File::Grepper::VERSION );

################ Presets ################

$options->{trace} = 1   if $options->{debug};
$options->{verbose} = 1 if $options->{trace};

################ Activate ################

App::File::Grepper->main( $options, @ARGV );

################ Options and Configuration ################

use Getopt::Long 2.13;

# Package name.
my $my_package;
# Program name and version.
my ($my_name, $my_version);

sub app_setup {
    my ($appname, $appversion, %args) = @_;
    my $help = 0;		# handled locally
    my $ident = 0;		# handled locally

    # Package name.
    $my_package = $args{package};
    # Program name and version.
    if ( defined $appname ) {
	($my_name, $my_version) = ($appname, $appversion);
    }
    else {
	($my_name, $my_version) = qw( MyProg 0.01 );
    }

    my $options =
      {
       verbose		=> 0,		# verbose processing
       ### ADD OPTIONS HERE ###

       'ignorecase'	=> undef,
       'filter'		=> undef,
       'exclude'	=> undef,
       'edit-with-emacs'=> 0,
       'edit-with-vi'	=> 0,
       'view'		=> 0,
       'pattern'	=> undef,

       # Development options (not shown with -help).
       debug		=> 0,		# debugging
       trace		=> 0,		# trace (show process)

       # Service.
       _package		=> $my_package,
       _name		=> $my_name,
       _version		=> $my_version,
       _stdin		=> \*STDIN,
       _stdout		=> \*STDOUT,
       _stderr		=> \*STDERR,
       _argv		=> [ @ARGV ],
      };

    # Colled command line options in a hash, for they will be needed
    # later.
    my $clo = {};

    Getopt::Long::Configure('noignorecase');
    # Sorry, layout is a bit ugly...
    if ( !GetOptions
	 ($clo,

	  ### ADD OPTIONS HERE ###

	 'ignorecase|i',
	 'filter|G=s',
	 'exclude|E=s',
	 'edit-with-emacs|e',
	 'edit-with-vi|v',
	 'pattern=s',
	 'view',

	  # Standard options.
	  'ident'		=> \$ident,
	  'help|?'		=> \$help,
	  'verbose',
	  'trace',
	  'debug',
	 ) )
    {
	# GNU convention: message to STDERR upon failure.
	app_usage(\*STDERR, 2);
    }
    # GNU convention: message to STDOUT upon request.
    app_usage(\*STDOUT, 0) if $help;
    app_ident(\*STDOUT) if $ident;

    unless ( defined $clo->{pattern} ) {
	$clo->{pattern} = shift( @ARGV );
    }
    app_usage(\*STDERR, 1) unless @ARGV;
    if ( $ENV{LANG} =~ /\.utf-?8$/i ) {
	require Encode;
	$clo->{pattern} = Encode::decode( "utf8", $clo->{pattern} );
    }

    # Plug in command-line options.
    @{$options}{keys %$clo} = values %$clo;

    if ( $options->{debug} ) {
	use Data::Dumper;
	warn Dumper $options;
    }

    $options;
}

sub app_ident {
    my ($fh) = @_;
    print {$fh} ("This is ",
		 $my_package
		 ? "$my_package [$my_name $my_version]"
		 : "$my_name version $my_version",
		 "\n");
}

sub app_usage {
    my ($fh, $exit) = @_;
    app_ident($fh);
    print ${fh} <<EndOfUsage;
Usage: $0 [options] pattern directories...

Application options:
    --ignorecase  -i	ignore case while matching
    --filter=PAT  -G	filter filenames according to PAT
    --exclude=PAT -E	reject filenames according to PAT
    --edit-with-emacs  -e   pass each file that matches to emacs
    --edit-with-vi  -v	pass each file that matches to vi
    --pattern=PAT	explicitly specify the pattern
    --view		view each file that matches

Miscellaneous options:
    --help		this message
    --ident		show identification
    --verbose		verbose information
EndOfUsage
    exit $exit if defined $exit;
}

=head1 NAME

afg - Greps files for pattern

=head1 SYNOPSIS

Runs a File::Find on the specified directories, and greps all text
files for a pattern.

This script is a driver for L<App::File::Grepper>, which see.

=head1 COMMAND LINE OPTIONS

=over 4

=item B<--pattern=>I<pattern>

App::File::Grepper option: B<pattern>.

If no B<--pattern> option is supplied, it is taken from the command line.

=item B<--ignorecase> or B<-i>

App::File::Grepper option: B<ignorecase>.

=item B<--edit-with-emacs> or B<-e>

App::File::Grepper option: B<edit-with-emacs>.

=item B<--edit-with-vi> or B<-v>.

App::File::Grepper option: B<edit-with-vi>.

=item B<--view>.

App::File::Grepper option: B<view>.

=item B<--filter=>I<pattern> or B<-G> I<pattern>

App::File::Grepper option: B<filter>.

=item B<--exclude=>I<pattern> or B<-E> I<pattern>

App::File::Grepper option: B<exclude>.

=back

=head1 AUTHOR

Johan Vromans, C<< <jv at cpan.org> >>

=head1 SEE ALSO

L<App::File::Grepper>.

=head1 COPYRIGHT & LICENSE

Copyright 2012 Johan Vromans, all rights reserved.

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

=cut