#!/usr/bin/perl -w
#============================== namesort ==================================
# Filename:  	       namesort
# Description:         List files grouped by md5 and length.
# Original Author:     Dale M. Amon
# Revised by:          $Author: $ 
# Date:                $Date: $ 
# Version:             $Revision: $
# License:	       LGPL 2.1, Perl Artistic or BSD
#
#=============================================================================
use strict;
use Getopt::Long;
use Pod::Usage;
use Fault::DebugPrinter;
use Fault::Logger;
use Fault::Delegate::Stderr;
use FileHash::Name;
use FileHash::Report

#=============================================================================
#                   Logging, debugging and signal handling
#=============================================================================
my %opts;
$::PROCESS_NAME = "namesort";
$::DEBUG        = 0;

sub done {
  {local $SIG{'TERM'} = 'IGNORE'; kill 'TERM' => -$$;}
  Fault::Logger->log ("Shutdown.","NOTE",'notice');
  exit 1;
}

                 Fault::DebugPrinter->new(0);
my $delegate1  = Fault::Delegate::Stderr->new;
my @delegates = ($delegate1);
                 Fault::Logger->new (@delegates);

$SIG{'INT'}  = \&done;
$SIG{'TERM'} = \&done;

#=============================================================================
#                    Switch and argument handling
#=============================================================================
my ($HELP,$MAN,$REPORT,$DIAGNOSTIC_PRINT_LEVEL) = (0,0,"/dev/stdout",0);

my $opts  = Getopt::Long::Parser->new;
$opts->getoptions (
        'd|debug'      => sub { $::DEBUG = 1 },
        'v|verbose=i'  => \$DIAGNOSTIC_PRINT_LEVEL,
	'report=s' => \$REPORT,
        'h|help'   => \$HELP,
        'man'      => \$MAN
);
pod2usage(1)                            if $HELP;
pod2usage(-exitval => 0, -verbose => 2) if $MAN;
Fault::DebugPrinter->level ($DIAGNOSTIC_PRINT_LEVEL);

#-----------------------------------------------------------------------------

if ($#ARGV < 0 ) {
     pod2usage(-exitval => "NOEXIT", -verbose => 1);
     printf STDERR "$::PROCESS_NAME must have a target file or directory, eg.\n" .
     		   "      $::PROCESS_NAME /my/Archive\n" .
                   "Terminating...\n";
     exit 1;
   }
my $report = FileHash::Report->new;
my $fha    = FileHash::Name->alloc;
             $fha->init;

foreach my $target (@ARGV) {$fha->addFromTree ($target);}

$report->all ($fha, $REPORT, 1);

exit 0;

#=============================================================================
#                          POD DOCUMENTATION                                
#=============================================================================
# You may extract and format the documention section with the 'perldoc' cmd.

=head1 NAME

 namesort - List files from a tree or trees, grouped by md5 and length.

=head1 SYNOPSIS

 namesort {-h|--help|--man} \
	   {-d|--debug} \
	   {-v|--verbose=1} \
	   {--report="~report"} /Archive/Photos

=head1 Description

List all files, grouped by filename, in a set of one or more
trees to stdout or to the specified file if --report defines one.

=head1 Examples

 namesort --report=myreport /Archive/Photos
 namesort /Archive/Photos > report

=head1 Errors and Warnings

 Lots.

=head1 KNOWN BUGS

 None.

=head1 SEE ALSO

 None.

=head1 AUTHOR

Dale Amon <amon@vnl.com>

=cut

#=============================================================================
#                                CVS HISTORY
#=============================================================================
# $Log: $
# 20080217	Dale Amon <amon@vnl.com>
#		Created.
1;
