#!/usr/bin/perl -w

# buscarf.pl
# Utilidad para la búsqueda de archivos utilizando File::Find.
# Julio de 2005. FJA - neocipres@jazzfree.com

use strict;
use File::Find;
no warnings 'File::Find';
use Switch;
 
my $na = 0;
my $dir = "";
my $filtro = "";
my $tipo = "f";
my $ctipo = "Archivos"; 
my $ftotal = 0; 
my ($usuario, $grupo);
my ($modo,$uid,$gid,$tam);
my $tamT = 0;

sub raya {
  print "\n#";
  print "=" x 78 . "#\n\n";
}

sub imprimir {
	printf " %04o", $modo & 07777;
	$usuario = getpwuid($uid);
	$grupo =  getgrgid($gid);
	print "\t$usuario\t$grupo";
	printf("\t%10d by ", $tam);
	print "\t$File::Find::name \n";
	$tamT += $tam;
	$na++;
}

sub listaf{
 my $arch = $_;
 ($modo,$uid,$gid,$tam) = (stat($arch))[2,4,5,7];
 switch ($tipo) {
  case "f" {if ($ftotal == 0){if (-f $arch && $arch =~$filtro){imprimir;}}
	    else {if (-f $arch && $arch eq $filtro){imprimir;}}
   	}
  case "x" {if (-f $arch && -x $arch && $arch =~$filtro){imprimir;}}
  case "d" {if (-d $arch && $arch =~$filtro){imprimir;}}
  case "z" {if (-z $arch && $arch =~$filtro){imprimir;}}
  case "l" {if (-l $arch && $arch =~$filtro){imprimir;}}
  case "b" {if (-b $arch && $arch =~$filtro){imprimir;}}
  case "u" {if (-f $arch && -u $arch && $arch =~$filtro){imprimir;}}
  case "g" {if (-f $arch && -g $arch && $arch =~$filtro){imprimir;}}
  case "k" {if (-d $arch && -k $arch && $arch =~$filtro){imprimir;}}
  case "S" {if (-S $arch && $arch =~$filtro){imprimir;}}
  case "T" {if (-f $arch && -T $arch && $arch =~$filtro){imprimir;}}
  case "B" {if (-f $arch && -B $arch && $arch =~$filtro){imprimir;}}
  }
}

print "\n";
if ($#ARGV == -1) {
	system("clear");
	raya;
	print "\  Uso:\n\n";
	print "\t buscarf <directorio> <filtro> [tipo]\n\n";
	print "\t ---------------------------------------\n\n";
	print "\  Tipo: f x d z l b u g k S T B\n\n";
	print "\  Ejemplos de posibles filtros para la busqueda: \n\n";
	print "\t filtro : busca archivos que contengan filtro en su nombre\n";
	print "\t ^filtro: los que contengan filtro al comienzo del nombre\n";
	print "\t filtro\$: los que contengan filtro al final del nombre\n";
	print "\t fi?tro : sustituyendo alguna de las letras del filtro \n";
	print "\t =filtro: coincidencia total del nombre con el filtro \n";
	print "\t ^[.]   : busca archivos ocultos \n";
	print "\t * o \\* : prueba con ellos \n\a";
	raya;
	exit;
}
if ($#ARGV == 0) { $dir = $ARGV[0]; }
if ($#ARGV >  0) {
	$dir = $ARGV[0];
	$filtro = $ARGV[1];
	if ($filtro =~ /\*/){$filtro = "";}
	if ($filtro =~ /=/ ){
		$filtro = substr($filtro,1,length($filtro)-1);
		$ftotal = 1;
		}
	if ($#ARGV == 2 && length($ARGV[2]) == 1) {
		$tipo = $ARGV[2];
		switch ($tipo) {
		 case "f" {$ctipo = "Archivos";}
		 case "x" {$ctipo = "Archivos ejecutables";}
		 case "d" {$ctipo = "Directorios";}
		 case "z" {$ctipo = "Archivos vacios";}
		 case "l" {$ctipo = "Enlaces simbolicos";}
		 case "b" {$ctipo = "Dispositivos de bloques";}
		 case "u" {$ctipo = "Archivos con el setuid activo";}
		 case "g" {$ctipo = "Archivos con el setgid activo";}
		 case "k" {$ctipo = "Directorios con el sticky activo";}
		 case "S" {$ctipo = "Archivos socket";}
		 case "T" {$ctipo = "Archivos en ASCII";}
		 case "B" {$ctipo = "Archivos binarios";}	
		 }
	 } 
	}
find(\&listaf, $dir);
raya;
$tamT = int(($tamT/1024)*10)/10;
print "\  $ctipo en $dir con el filtro $filtro: $na, con $tamT KB \n\n\a";