#!/usr/bin/env perl
#
# uricolor - Colorize URIs with ANSI colors.
#
# uricolor is a Perl script to colorize URIs with ANSI colors.
# It reads files sequentially, and writes them to STDOUT,
# with all URIs colored. If file is a dash "-" or if no file
# is given as argument, uricolor reads from STDIN.
#
# 2019.10.10 v0.15 jul : fixed META data
# 2019.08.05 v0.14 jul : added -c (color)
# 2019.07.09 v0.13 jul : added -s (schemeless), use 5.010, better doc
# 2019.06.18 v0.12 jul : dropped file extension, added pod
# 2019.06.15 v0.11 jul : changed shebang
# 2018.09.10 v0.10 jul : created

use 5.010;
use strict;
use warnings;
use utf8;
use Getopt::Std;
use File::Basename;
use URI::Find;
use URI::Find::Schemeless;
use Term::ANSIColor qw(colored colorvalid);

our $VERSION    = '0.15';
my $program     = basename($0);
my $usage       = <<EOF;

Usage: $program [-hVds] [-c color] [file ...]

    -h, --help      help
    -V, --version   version
    -d              debug
    -s              schemeless
    -c              color (default : underline blue)
EOF

# options

my %options = ();
getopts("hVdsc:", \%options) or die $usage;

my $help        = $options{h} // 0;
my $version     = $options{V} // 0;
my $debug       = $options{d} // 0;
my $schemeless  = $options{s} // 0;
my $color       = $options{c} // 'underline blue';

die $usage if $help;
die $VERSION . "\n" if $version;
die "color not valid. See `perldoc Term::ANSIColor`\n" if not colorvalid($color);

########
# MAIN #
########

my $class = $schemeless ? "URI::Find::Schemeless" : "URI::Find";	# copy-paste from urifind
my $finder = $class->new( sub {
	my ($uri, $orig_uri) = @_;
	return colored ($orig_uri, $color);
});


############
# RUN LOOP #
############

my $n = 0;

while (<>)
{
	my $res = $finder->find(\$_);	
	print "$_";
	$n += $res;
}

warn "$n URIs colorized\n" if $debug;

exit 1;

__END__

=head1 NAME

uricolor - Colorize URIs with ANSI colors.

=head1 SYNOPSIS

    $ uricolor [-hVds] [-c color] [file ...]

    -h, --help      help
    -V, --version   version
    -d              debug
    -s              schemeless
    -c              color (default : underline blue)

=head1 DESCRIPTION

uricolor is a Perl script to colorize URIs with ANSI colors.
It reads files sequentially, and writes them to STDOUT,
with all URIs colored. If file is a dash "-" or if no file
is given as argument, uricolor reads from STDIN.

=head1 BUGS

Please report any bugs or feature requests to C<kaldor@cpan.org>, or through
the web interface at L<https://github.com/kal247/App-uricolor/issues>.

=head1 AUTHOR

jul, C<kaldor@cpan.org>

=head1 LICENSE AND COPYRIGHT

This software is Copyright (c) 2019 by jul.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=head1 SEE ALSO

urifind, URI::Find, URI::Find::Schemeless