#!/usr/bin/env perl
use Mojolicious::Lite;
use Getopt::Long qw{ :config posix_default no_ignore_case gnu_compat pass_through };
use File::Spec;

my $opts = +{};
GetOptions( $opts, qw{ file=s webtailrc=s tail-opts=s v|verbose } ) or help();
help() if ( $ENV{MOJO_HELP} );

sub help { exec "perldoc $0" }

if (  !$opts->{webtailrc}
    && $ENV{HOME}
    && -e ( my $webtailrc = File::Spec->catfile( $ENV{HOGE}, '.webtailrc' ) )
    )
{
    $opts->{webtailrc} = $webtailrc;
}

app->log->level('info') unless ( $opts->{v} );

plugin 'Webtail', file => $opts->{file}, webtailrc => $opts->{webtailrc}, tail_opts => $opts->{'tail-opts'};

get '/' => sub { shift->redirect_to('/webtail') };

app->start('daemon', @ARGV);

__END__

=head1 NAME

mwtail - display tail to your browser

=head1 SYNOPSIS

  $ tail -f /path/to/logfile | mwtail

  or

  $ mwtail --file=/var/log/something.log

=head1 DESCRIPTION

mwtail displays tail to your browser

=head1 OPTIONS

L<mwtail> supports the following options.

and supports L<Mojolicious::Command::daemon> options too.

=head2 C<-h|--help>

show this.

=head2 C<--file>

displays the contents of C<file> or, by default, its C<STDIN>.

  $ mwtail --file=/path/to/logfile

=head2 C<--webtailrc>

define your custom callback in C<webtail> file.

the code in C<webtail> file is executed when a new line is inserted.

  $ tail -f /path/to/logfile | mwtail --webtailrc=/path/to/webtailrc

L<mwtail> will find webtailrc in the following order.

=over 4

=item 1. --webtailrc option

=item 2. $ENV{HOME}/.webtailrc

=back

=head2 C<--tail-opts>

define tail command options, by default, "-f -n 0"

  $ mwtail --tail-opts="-F -n 0"

=head2 C<-v|--verbose>

turn on chatty output.

  $ mwtail --verbose

=head1 AUTHOR

hayajo E<lt>hayajo@cpan.orgE<gt>

=head1 SEE ALSO

L<https://github.com/r7kamura/webtail>

=head1 LICENSE

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
