#!/usr/bin/env perl 

use strict;
use warnings;

use FindBin qw($Bin);
use lib "$Bin/../lib";
use String::Normal;

use Safe;
use Pod::Usage;
use Getopt::Long;

GetOptions (
    'file=s'    => \my $file,
    'value=s'   => \my $value,
    'type=s'    => \my $type,
    'help'      => \my $help,
    'man'       => \my $man,
    'version'   => \my $version,
);
pod2usage( -verbose => 0 ) if defined $help;
pod2usage( -verbose => 2 ) if $man;

if ($version) {
    print "$_\n" for 
        "      String::Normal path: " . $INC{'Spreadsheet/HTML.pm'},
        "   String::Normal version: $Spreadsheet::HTML::VERSION",
    ;
    exit;
}

unless (defined( $value ) or defined( $file )) {
    pod2usage({ -message => '--value or --file is required', -exitval => 1, -verbose => 0 });
}

my $normalizer = String::Normal->new( type => $type );

if ($file) {
    open my $fh, $file or die "Cannot open $file: $!\n";
    print $normalizer->transform( $_ ), "\n" while <$fh>;
} else {
    print $normalizer->transform( $value ), "\n";
}

__END__
=head1 NAME

normalizer - transform strings into a normal form

=head1 SYNOPSIS

mktable method [options]

 Options:
   --value      value
   --type       business,address,city,state,zip,phone,title
   --help       list usage for methods and parameters
   --man        print man page
   --version    show module path and version

=head1 OPTIONS

=over 8

=item B<--value>

The value to transform.

  --value="The The"

=item B<--file>

Text file containing one value per line.

  --file=values.txt

=item B<--type>

The type of value: business, address, city, state, zip, phone or title. Defaults to business.

  --type=address

=item B<--help>

Shows synopsis and exits.

=item B<--man>

Prints the manual page and exits.

=item B<--version>

Prints the versions of the core modules used.

=back

=head1 EXAMPLES

  normalizer --value="Jones's & Sons Bakeries" --type=business

  # business is the default type
  normalizer --value="Jones's & Sons Bakeries"

  normalizer --value="123 Main Street" --type=address

=head1 SEE ALSO

=over 4

=item L<String::Normal>

The engine behind this script.

=back

=cut
