#!/usr/bin/perl

use warnings;
use strict;
use Plack::Runner;

our $VERSION = '0.001002';

sub version {
    print "$VERSION\n";
}

my $preload_app;
my $dezi_config_file;
my $server_class = 'Dezi::Server';
my $debug;

require Getopt::Long;
Getopt::Long::Configure( "no_ignore_case", "no_auto_abbrev", "pass_through" );
Getopt::Long::GetOptions(
    "preload-app"    => \$preload_app,
    "dezi-config=s"  => \$dezi_config_file,
    "server-class=s" => \$server_class,
    "debug"          => \$debug,
);

my @args = (

    #env        => 'deployment',
    version_cb => \&version
);
if ( !$preload_app ) {
    push @args, 'loader' => 'Delayed';
}

eval "use $server_class";
die $@ if $@;

my $config = {};
if ($dezi_config_file) {
    require Config::Any;
    $config = Config::Any->load_files(
        {   files   => [$dezi_config_file],
            use_ext => 1,
        }
    )->[0]->{$dezi_config_file};
}

$config->{debug} = $debug;

my $app = $server_class->app($config);

my @argv = @ARGV;

my $runner = Plack::Runner->new(@args);
$runner->parse_options(@argv);
$runner->set_options( argv => \@argv );
$runner->run($app);

__END__

=head1 NAME

Dezi - search platform based on Apache Lucy, Swish3 and Plack

=head1 SYNOPSIS

Start the Dezi server, listening on port 5000:

 % dezi -p 5000

Add a document B<foo> to the index:

 % curl http://localhost:5000/index/foo -XPOST \
   -d '<doc><title>bar</title>hello world</doc>' \
   -H 'Content-Type: application/xml'
   
Search the index:

 % curl 'http://localhost:5000/search?q=bar'

=head1 DESCRIPTION

Dezi is a search platform based on Apache Lucy, Swish3, Plack,
Search::OpenSearch and Search::Query. 

Dezi integrates several CPAN search libraries into one
easy-to-use interface.

=head1 OPTIONS

The dezi app supports all the options that Plack::Runner supports.
See the L<plackup> perldoc for a description. The following
options are specific to dezi.

=head2 debug

Turns on verbosity in stderr Plack logging.

=head2 dezi-config I<file>

I<file> can be in any format supported by Config::Any.
The structure expected contains a key called 'engine_config'
which is passed directly to Search::OpenSearch::Server::Plack,
which in turn passes through to Search::OpenSearch::Engine.

Other config options include:

=over 

=item search_path I<path>

The /-prefixed URI path where the Dezi server should accept
incoming searcher requests. Defaults to '/search'.

=item index_path I<path>

The /-prefixed URI path where the Dezi server should accept
incoming indexer requests. Defaults to '/index'.

=back

=head2 server-class I<class_name>

Use I<class_name> instead of the default 'Dezi::Server'. Useful
if you want to create your own server subclass of Dezi::Server.

=head1 AUTHOR

Peter Karman, C<< <karman at cpan.org> >>

=head1 BUGS

Please report any bugs or feature requests to C<bug-dezi at rt.cpan.org>, or through
the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Dezi>.  I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.


=head1 SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc Dezi


You can also look for information at:

=over 4

=item * Mailing list

L<https://groups.google.com/forum/#!forum/dezi-search>

=item * RT: CPAN's request tracker

L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Dezi>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Dezi>

=item * CPAN Ratings

L<http://cpanratings.perl.org/d/Dezi>

=item * Search CPAN

L<http://search.cpan.org/dist/Dezi/>

=back


=head1 ACKNOWLEDGEMENTS

Marvin Humphrey for a great library in Apache Lucy.

The Plack community.

=head1 COPYRIGHT & LICENSE

Copyright 2011 Peter Karman.

This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.


=cut

