#!/usr/bin/env perl

=pod

=head1 NAME

wrt-fcgi - Sample FastCGI wrapper script for App::WRT

=head1 DESCRIPTION

Pulls in App::WRT and CGI::Fast, runs a configuration file, and loops over
FastCGI queries, passing their parameters to C<App::WRT::display()>.

Useful for local testing of output.  Please do not expose to the public web.

=cut

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

use App::WRT;
use CGI::Fast;
use Getopt::Long;
use Pod::Usage;

# 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 $config_file = 'wrt.json';
GetOptions(
  'config=s' => \$config_file,
  help       => sub { pod2usage(0) },
) or pod2usage(2);

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

# use Data::Dumper;

# Handle input from FastCGI:
while (my $query = CGI::Fast->new) {
  my @params = $query->multi_param('keywords');
  print "Content-Type: text/html\n\n"; 
  print $w->display(@params);
}

exit;
