#!/usr/bin/perl -w
#================================= fhd_ls ====================================
# Filename:  	       fhd_ls
# Description:         Generate a filehash dump file from ls -l input.
# 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::Delegate::Stderr;
use Fault::DebugPrinter;
use Fault::Logger;
use FileHash::Name;

#=============================================================================
#                   Logging, debugging and signal handling
#=============================================================================
my %opts;
$::PROCESS_NAME = "fhd_ls";
$::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,$DUMP,$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,
	'dump=s'       => \$DUMP,
        'h|help'       => \$HELP,
        'man'          => \$MAN
);
pod2usage(1)                            if $HELP;
pod2usage(-exitval => 0, -verbose => 2) if $MAN;

Fault::DebugPrinter->level ($DIAGNOSTIC_PRINT_LEVEL);

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

my $file       = ($#ARGV < 0 ) ? "/dev/stdin" :  $ARGV[0];
my $formatline =  
   "modeChars hardlinks uidName gidName sizeBytes mtimeDate mtimeTime file";
my $fnhash     = FileHash::Name->alloc;

$fnhash->initFromFile ($formatline,$file);
$fnhash->dump         ($DUMP);

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

=head1 NAME

 fhd_ls - create a filehash dump file from ls -l input.

=head1 SYNOPSIS

 fhd_ls {-h|--help|--man} \
	{-d|--debug} \
	{-v|--verbose=1} \
	{--dump="~dump"} {/Archive/myls.dir}

=head1 Description

Generate a filehash dump file from ls -l input. That input may come either
from a file or from stdin.

If the --dump is not present, dump output defaults to stdout.

=head1 Examples

  fhd_ls --dump=mydump /home/amon/ListOfFilePathnames
  ls -l | fhd_ls --dump=mydump
  ls -l | fhd_ls > dumpfile

=head1 Errors and Warnings

 Lots.

=head1 KNOWN BUGS

The program handles header lines, blank lines and other intermediate items by
skipping if they do not match the length required by the format line.
This could cause screw ups if there is a non-data line with the same
length as a data one. I really will need to make this program a whole lot 
smarter.

Also, because it is not parsing header lines, it does not build a hash
with the full path of the files, only with the terminal names. This will have
to be fixed sooner rather than later.

Beware that older ls -l output may have an mtimeDate like 'Jan 2'. This will
fail with the program as it stands. I need fancier parsing schemes to handle 
that.

When using stdout, the log messages will be mixed with the output.

=head1 SEE ALSO

 fhd_namelist, fhd_tree_hash.

=head1 AUTHOR

Dale Amon <amon@vnl.com>

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