#!/usr/bin/perl -w
#
# Copyright (C) 2008, Joshua D. Abraham (jabra@spl0it.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
# use strict;
#
#
use strict;
use File::Find;
use Getopt::Long;
no warnings 'File::Find';
use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;    # Truncate calling path from the prog name
my $AUTH    = 'Joshua D.Abraham';  # author
my $EMAIL   = 'jabra@spl0it.org';   # email
my $VERSION = '1.00';               # version
my %options;                        # getopt option hash
my (@dirs, @exts, @files);

#
# help:
# display help information
#
sub help {
    print "Usage: $PROG [Input Option] [Option] 
    -d  --dirs \"str,str\"  Directories to check for file extensions
                            (comma seperated list)
    Search Options: 
    -f  --files \"str,str\" Files to check for 
                            (comma seperated list)
    -e  --exts \"str,str\"  Extensions to check for
                            (comma seperated list)
    
    -v  --version           Display version
    -h  --help              Display this information
Send Comments to $AUTH ( $EMAIL )\n";
    exit;
}

#
# print_version:
# displays version
#
sub print_version {
    print "$PROG version $VERSION by $AUTH ( $EMAIL )\n";
    exit;
}

sub search_exts  {
    foreach my $e (@exts) {
        if ( /\.$e$/){
            print "Found: $File::Find::name\n";
        }
    }
}

sub search_files {
    foreach my $f (@files) {
        if (/^$f$/){
            print "Found: $File::Find::name\n";
        }
    }
}

if ( @ARGV == 0 ) {
    help;
    exit;
}
GetOptions(
    \%options,
    'dirs|d=s', 'exts|e=s', 'files|f=s', 
    'help|h'    => sub { help(); },
    'version|v' => sub { print_version(); },
) or exit 1;

if ($options{dirs}){
   @dirs =  split(',',$options{dirs});
}
else {
    help();
}

if ($options{files}) {
   @files =  split(',',$options{files});
}
elsif ($options{exts}){
   @exts =  split(',',$options{exts});
}
else { }


if (scalar(@files) > 0) {
    find(\&search_files, @dirs);
}
elsif(scalar(@exts) > 0) {
    find(\&search_exts, @dirs);
}
else {
    help();
}

