#!/usr/bin/perl -w

use strict ;
use warnings ;
use Carp ;

=head1 NAME 

 $> hdr - hexdump range

=head1 USAGE

 $> hdr -r range_definitions file_to_dump


=head1 OPTIONS

 range_description|r           perl file returning a description
 orientation|o                 'horizontal' or 'vertical'
 format|f                      'ANSI' or 'ASCII' or 'HTML' 
 color                         'bw' or 'cycle',
 data_width                    the number of bytes dumped per line
 
 offset_format                 'hex' or 'dec' 
 show_offset                   0 to not see the offset
 show_cumulative_offset        0 to not see the offset per range
 show_zero_size_range          0 to not see range names with size 0
 show_zero_size_range_warning  0 to not see warnings about ranges with size 0
 
 show_range_name               controls the display of the range name
 show_hex_dump                 controls the display of the hexadecimal dump
 show_dec_dump                 controls the display of the decimal dump
 show_ascii_dump               controls the display of the ASCII dump
 
 h|help                        display this scripts help page

=head1 EXIT STATUS


=head1 AUTHOR

  Nadim ibn hamouda el Khemir
  CPAN ID: NKH
  mailto: nkh@cpan.org

=cut

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

use Getopt::Long ;
use English qw( -no_match_vars ) ;

use Data::HexDump::Range qw() ;

our $VERSION = '0.02' ;

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

display_help() unless 
	GetOptions
		(
		'range_description|r=s' => \ my $range_description,
		'orientation|o=s' => \my $orientation,
		'format|f=s' => \my $format,
		'color=s' => \my $color,
		'data_width=i' =>  \my $data_width,
		
		'offset_format=s' => \my $offset_format,
		'show_offset=i' => \my $show_offset,
		'show_cumulative_offset=i' => \my $show_cumulative_offset,
		'show_zero_size_range=i' => \my $show_zero_size_range,
		'show_zero_size_range_warning=i' => \my $show_zero_size_range_warning,

		'show_range_name=i' => \my $show_range_name,
		'show_hex_dump=i' => \my $show_hex_dump,
		'show_dec_dump=i' => \my $show_dec_dump,
		'show_ascii_dump=i' => \my $show_ascii_dump,
		
		'h|help' => \&display_help, 
		) ;

display_help() unless @ARGV ;
$range_description ||= 'dumping all file' ;

my $file_to_dump = shift @ARGV ;

my $hdr = Data::HexDump::Range->new
			(
			ORIENTATION => $orientation || 'horizontal',
			FORMAT => $format || 'ANSI',
			COLOR => defined $color ? $color : 'cycle',
			
			OFFSET_FORMAT => $offset_format || 'hex',
			DATA_WIDTH => $data_width || 16,
			DISPLAY_RANGE_NAME => defined $show_range_name ? $show_range_name : 1 ,
			DISPLAY_OFFSET  => defined $show_offset ? $show_offset : 1 ,
			DISPLAY_CUMULATIVE_OFFSET  => defined $show_cumulative_offset ? $show_cumulative_offset : 1 ,
			DISPLAY_HEX_DUMP => defined $show_hex_dump ? $show_hex_dump : 1,
			DISPLAY_DEC_DUMP => defined $show_dec_dump ? $show_dec_dump : 0,
			DISPLAY_ASCII_DUMP => defined $show_ascii_dump ? $show_ascii_dump :  1 ,
			
			DISPLAY_ZERO_SIZE_RANGE => defined $show_zero_size_range ? $show_zero_size_range : 1,
			DISPLAY_ZERO_SIZE_RANGE_WARNING => defined  $show_zero_size_range_warning ? $show_zero_size_range_warning : 1,
			) ;

use File::Slurp ;
my $data = read_file $file_to_dump ;

my $range  = do $range_description || ['no range defined', length($data) ] ;

print $hdr->dump( $range, $data ) ;



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

sub display_help
{

#~ =head2 display_help()

#~ I<Arguments> - None

#~ I<Returns> - Nothing

#~ I<Exceptions> - exits with status code B<1>

#~ =cut

my ($this_script) = ($PROGRAM_NAME =~m/(.*)/sxm ) ;

print {*STDERR} `perldoc $this_script`  or croak 'Error: Can\'t display help!' ; ## no critic (InputOutput::ProhibitBacktickOperators)
exit(1) ;
}
