#!/home/ben/software/install/bin/perl
use warnings;
use strict;
use utf8;
use FindBin '$Bin';
use lib '/home/ben/projects/html-valid/blib/lib';
use lib '/home/ben/projects/html-valid/blib/arch';
use HTML::Valid 'sanitize_errors';
use Path::Tiny;

# True if we can use the World Wide Web.
my $webok;
eval {
    require Data::Validate::URI;
    Data::Validate::URI->import (qw/is_web_uri/);
    require LWP::Simple;
    LWP::Simple->import ();
    $webok = 1;
};

if ($@) {
    $webok = 0;
}

my $htv = HTML::Valid->new (quiet => 1);
while (@ARGV) {
    my $file = shift @ARGV;
    if ($file =~ /^--(\S*)/) {
	my $option = $1;
	my $value = shift @ARGV;
	if (! $value) {
	    print "$0: $file needs a value";
	    next;
	}
	$htv->set_option ($option, $value);
	next;
    }
    my $content;
    if (-f $file) {
	$content = path ($file)->slurp_utf8 ();
	$htv->set_filename ($file);
    }
    elsif ($webok && is_web_uri ($file)) {
	print "$0: Getting '$file' from web.\n";
	$content = get ($file);
	if (! $content) {
	    warn "$0: Could not download '$file', skipping.\n";
	    next;
	}
    }
    else {
	warn "$0: Cannot find '$file'.\n";
	next;
    }
    my (undef, $errors) = $htv->run ($content);
    if ($errors) {
	print sanitize_errors ($errors);
    }
}

# Local variables:
# mode: perl
# End:
