#!/usr/bin/env perl

=pod

=head1 NAME

wrt-display - print static HTML from wrt entries

=head1 USAGE

    wrt display 2016/4/1
    echo 2016/4/1 | wrt display --stdin
    wrt display --config ./wrt.json ...
    wrt display --help

=head1 DESCRIPTION

tk tk tk

=cut

use 5.10.0;

use strict;
use warnings;
no  warnings 'uninitialized';

# use Data::Dumper;
use Getopt::Long;
use Pod::Usage;
use App::WRT;

# Handle options, including help generated from the POD above.  See:
# - http://perldoc.perl.org/Getopt/Long.html#User-defined-subroutines-to-handle-options
# - https://metacpan.org/pod/Pod::Usage
# - http://michael.thegrebs.com/2014/06/08/Pod-Usage/
my $from_stdin  = 0;
my $config_file = 'wrt.json';
GetOptions(
  stdin  => \$from_stdin,
  config => \$config_file,
  help   => sub { pod2usage(0) },
) or pod2usage(2);

unless (-e $config_file) {
  die "No wrt config file found.  Tried: $config_file";
}

my $w = App::WRT::new_from_file($config_file);

# With --stdin, take names of entries to display from standard input, one line
# per name.  Otherwise, take names from arguments.
my @to_display = ();
if ($from_stdin) {
  while (my $entry = <>) {
    chomp($entry);
    push @to_display, $entry;
  }
} else {
  (@to_display) = @ARGV;
}

print $w->display(@to_display);
exit(0);
