#!/usr/bin/perl

# Created on: 2014-11-07 16:40:29
# Create by:  Ivan Wills
# $Id$
# $Revision$, $HeadURL$, $Date$
# $Revision$, $Source$, $Date$

use strict;
use warnings;
use version;
use Getopt::Long;
use Pod::Usage;
use Data::Dumper qw/Dumper/;
use English qw/ -no_match_vars /;
use FindBin qw/$Bin/;
use Term::ANSIColor qw/:constants/;
use Path::Tiny;
use File::TypeCategories;

our $VERSION = version->new('0.8.1');
my ($name)   = $PROGRAM_NAME =~ m{^.*/(.*?)$}mxs;
my $REVERSE  = REVERSE;
my $RESET    = RESET;
my $BLUE     = BLUE;
my $BOLD     = BOLD;
my $ON_RED   = ON_RED;
my $ON_GREEN = ON_GREEN;

my %option = (
    ignore => ['ignore'],
);

if ( !@ARGV ) {
    pod2usage( -verbose => 1 );
}

main();
exit 0;

sub main {

    Getopt::Long::Configure('bundling');
    GetOptions(
        \%option,
        'sre_smart|smart|m',
        'path|p=s@',
        'file_symlinks|links|l!',
        'file_recurse|R!',
        'file_include|include|n=s@',
        'file_include_type|include-type|int|N=s@',
        'file_exclude|exclude|x=s@',
        'file_exclude_type|exclude-type|ext|X=s@',
        'file_ignore|ignore=s@',
        'file_ignore_add|ignore-add|d=s@',
        'file_ignore_remove|ignore-remove|I=s@',
        'types|t!',
        'verbose|v+',
        'man',
        'help',
        'VERSION!',
    ) or pod2usage(2);

    if ($option{file_ignore_add}) {
        push @{$option{file_ignore}}, @{$option{file_ignore_add}};
        delete $option{file_ignore_add};
    }
    if ($option{file_ignore_remove}) {
        for my $remove (@{ $option{file_ignore_remove} }) {
            @{$option{file_ignore}} = grep {$_ ne $remove} @{$option{file_ignore}};
        }
        delete $option{file_ignore_remove};
    }

    if ( $option{'VERSION'} ) {
        print "$name Version = $VERSION\n";
        exit 1;
    }
    elsif ( $option{'man'} ) {
        pod2usage( -verbose => 2 );
    }
    elsif ( $option{'help'} ) {
        pod2usage( -verbose => 1 );
    }
    elsif ( $option{types} ) {
        print File::TypeCategories->new(params('file', %option))->show_types(@ARGV);
        exit 0;
    }
    elsif ( !@ARGV ) {
        warn "No search term specified\n";
        pod2usage( -verbose => 1 );
    }

    # do stuff here
    my @files = map { path $_ } map {glob $_} map {split /:/, $_} $option{path} ? @{$option{path}} : ('.');

    my $lines = 80;
    if ($option{sre_smart}) {
        ($lines) = split /\s+/, `stty size` || 40;
        if (( !$option{file_include_type} || !@{ $option{file_include_type} }) && grep {$_ eq $ARGV[0]} qw/n b ss/) {
            $option{file_include_type}[0] = 'programing';
        }
        if ( !exists $option{ignore} ) {
            my $re = join ' ', @ARGV;
            if ( $re =~ /[A-Z]/ && $re =~ /[a-z]/ ) {
                $option{ignore} = 0;
            }
            else {
                $option{ignore} = 1;
            }
        }
    }

    my $tc = File::TypeCategories->new(params('file', %option));

    if ($option{bw}) {
        $REVERSE = '';
        $RESET   = '';
        $BLUE    = '';
        $BOLD    = '';
        $ON_RED  = '';
    }

    my $re = join ' ', @ARGV;
    $re = qr/$re/;

    while ( my $file = shift @files ) {
        next if !$tc->file_ok($file);

        if (-d $file) {
            eval { push @files, $file->children };
        }

        if ($file =~ /$re/) {
            print "$file\n";
        }
    }

    return;
}

sub params {
    my ($name, %var) = @_;
    my %params;

    VAR:
    for my $key (keys %var) {
        next VAR if $key !~ /^ $name _ /xms;
        my $new_key = $key;
        $new_key =~ s/^ $name _ //xms;
        $params{$new_key} = $var{$key};
    }

    return %params;
}

__DATA__

=head1 NAME

tfind - Find files based on file types

=head1 VERSION

This documentation refers to tfind version 0.8.1

=head1 SYNOPSIS

   # search for files
   tfind [option] regex
   # list available types (or detail specific type)
   tfind (-t|--types) [type]

 OPTIONS:
   -m --smart    Smart regular expression
   -p --path[=]path
                  Don't use the current directory for searching use path
   -l --follow-symlinks
                  Follow symlinks to directories
      --no-follow-symlinks
                  Don't follow symlinks to directories
      --recurse   Recurse into subdirectories (Default)
      --no-recurse
                  Turns off recursing into subdirectories
   -n --file-include[=]string
                  Only include files mathcing the regex (Multiple)
   -N --int[=]string
      --include-type[=]string
                  Only include files the specified type (Mulitple)
                  see perldoc File::TypeCategories available types
   -x --file-exclude[=]string
                  Don't include files mathcing the regex (Multiple)
   -X --ext[=]string
      --exclude-type[=]string
                  Don't include files the specified type (Mulitple)
                  see perldoc File::TypeCategories available types
       --ignore[=]type
                  Add type to the types to be ignored (multiples ok, overwrites default types)
    -d --ignore_add[=]type
                  Add extra type to default types (multiples ok)
    -I --ignore_remove[=]type
                  Remove type from default types (multiples ok)
    -t --types    Show all types (or details of a type if type also passed)

     -v --verbose Show more detailed option
        --VERSION Prints the version information
        --help    Prints this help information
        --man     Prints the full documentation for cs with example usage

=head1 DESCRIPTION

This program provides a quick way to search for files using regular
expressions and optionally limiting the results based on file types.

=head1 SUBROUTINES/METHODS

=head1 DIAGNOSTICS

=head1 CONFIGURATION AND ENVIRONMENT

=head1 DEPENDENCIES

=head1 INCOMPATIBILITIES

=head1 BUGS AND LIMITATIONS

There are no known bugs in this module.

Please report problems to Ivan Wills (ivan.wills@gmail.com).

Patches are welcome.

=head1 AUTHOR

Ivan Wills - (ivan.wills@gmail.com)

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2014 Ivan Wills (14 Mullion Close, Hornsby Heights, NSW Australia 2077).
All rights reserved.

This module is free software; you can redistribute it and/or modify it under
the same terms as Perl itself. See L<perlartistic>.  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.

=cut
