#!/usr/bin/perl -w
$0 = "apache $ENV{SCRIPT_FILENAME}";

use strict;

my($MT_DIR);
BEGIN {
    if ($0 =~ m!(.*[/\\])!) {
        $MT_DIR = $1;
    } else {
        $MT_DIR = './';
    }
    unshift @INC, $MT_DIR . 'lib';
    unshift @INC, $MT_DIR . 'extlib';
}

use CGI;
use CGI::Cache;
use MT;
use MT::ConfigMgr;
use MT::Template;
use MT::Template::Context;
use MT::XSearch;

eval {
    my $mt = MT->new( Config => $MT_DIR . 'mt.cfg', Directory => $MT_DIR )
        or die MT->errstr;

    my $charset = $mt->{cfg}->PublishCharset;
    my $q = new CGI;
    CGI::Cache::setup(
        {
            cache_options => {
                cache_root => './cache',
                default_expires_in => 3600
            }
        }
    );

    my $blog_id = $q->param('blog_id') or
        die "Missing parameter blog_id";
    my $key = $q->param('search_key') or
        die "Missing parameter key";
    CGI::Cache::set_key($q->Vars);
    CGI::Cache::start() or exit;
    my $search = MT::XSearch->execute($q);
    my $tmpl = MT::Template->load( { 
                        name=>'XSearch '.$key, 
                            type=>'custom',
                                blog_id=>$blog_id } );
    my $ctx = MT::Template::Context->new();
    $ctx->stash('MT::XSearch',$search);
    $ctx->stash('CGI',$q);
    my $out = $tmpl->build($ctx)
        or die "Building search template failed: ".$tmpl->errstr;
    #print $q->header(-charset=>$charset).$out;
    $charset = $mt->{cfg}->PublishCharset;
    my @m = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
    my @w = qw(Sun Mon Tue Wed Thu Fri Sat);
    my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime(time);
    my $now = sprintf("%3s, %02d %3s %04d %02d:%02d:%02d GMT",
        $w[$wday], $mday, $m[$mon], $year+1900, $hour, $min,$sec);
    print $q->header(-charset=>$charset,-Last_Modified=>$now).$out;
    CGI::Cache::stop();
};
if ($@) {
    print "Content-Type: text/html\n\n";
    print "Got an error: $@";
}

__END__

=begin

=head1 NAME

mt-xsearch,cgi - A generic search script for use with XSearch framework plugins.

=head1 SYNOPSIS

<form method="get" action="<$MTCGIPath$>mt-xsearch.cgi">
<input type="hidden" name="blog_id" value="<$MTBlogID$>" />
<input type="hidden" name="search_key" value="grep" />
<label for="search" accesskey="4">Search this site:</label><br />
<input id="search" name="search" size="20" /><br />
<input type="submit" value="Search" />
</form>

=head1 DESCRIPTION 

Since MT template are associated to a specific weblog, the blog_id param 
is required. This issue is under review to make this script useful to 
doing multi-weblog and non-weblog searches.

Also, this script looks for a template module in the form "XSearch foo" where
foo is the key name of the plugin it is associated to. 

=head1 LICENSE

The software is released under the Artistic License. The terms of the Artistic 
License are described at L<http://www.perl.com/language/misc/Artistic.html>.

=head1 AUTHOR & COPYRIGHT

Except where otherwise noted, mt-xsearch.cgi is Copyright 2004, Timothy Appnel, 
me@timaoutloud.org. All rights reserved.

=cut

=end
