#!/usr/bin/perl -w
# (C) 2003-2007 Willem Jan Hengeveld <itsme@xs4all.nl>
# Web: http://www.xs4all.nl/~itsme/
#      http://wiki.xda-developers.com/
#
# $Id$
#

use strict;

# searches entire path for specified files, files may be specified with wildcards.

use POSIX;
#use Time::local;
use File::Glob qw(:globally :nocase);

use Getopt::Long;

my $verbose= 0;
GetOptions("verbose" => \$verbose);
my @found;

my $splitchar= $ENV{PATH}=~/;/ ? ";" : ":";
for my $dir (split $splitchar, $ENV{PATH})
{
    $dir =~ s/\\/\//g;
    $dir =~ s/\/$//g;
    $dir =~ s/([ ])/\\$1/g;

    for my $pattern (@ARGV)
    {
        print "searching $dir for $pattern\n" if ($verbose);
        for my $file (glob("$dir/$pattern"))
        {
            if (-e $file) { push(@found, $file); }
        }
    }
}
if (-p STDOUT && @found) {
    print $found[0]."\n";
}
else {
    print map { sprintf("%9d %s %s\n", -s $_, POSIX::strftime("%Y-%m-%d %H:%M:%S", localtime getmtime($_)), $_); } @found;
}

sub getmtime {
    my $filename= shift;
    my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
        $atime,$mtime,$ctime,$blksize,$blocks)
            = stat $filename;

    return $mtime;

}
