#!/usr/local/bin/perl

# $Id: div_wrapper,v 1.3 2006/08/16 17:30:31 kclark Exp $

use strict;
use Carp qw( croak );
use CGI;
use Gramene::Config;
use Gramene::Page;
use Gramene::Utils qw( get_logger );
use Readonly;
use Template;

use vars qw[ $VERSION ];
$VERSION = sprintf "%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/;

delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
$ENV{'PATH'} = '/bin:/usr/bin:/usr/local/bin';

Readonly my %TEMPLATE => (
    header => 'header.tmpl',
    footer => 'footer.tmpl',
);

my $q = CGI->new;

my ( $t, $page, $html );
eval { 
    #
    # Get config info, template object, db handle, GramenePage, etc.
    #
    my $component = $q->param('component') || '';
    my $template  = $TEMPLATE{ $component } || croak("Bad arg '$component'");
    my $cfile     = Gramene::Config->new;
    my $config    = $cfile->get('diversity');
    $t            = Template->new( INCLUDE_PATH => $config->{'template_dir'} );
    my $referer   = $q->param('referer') || '';
    $page         = Gramene::Page->new( Apache->request, { 
        current_url  => $referer,
        current_args => '',
    } );

    #
    # Create the output or note the error.
    #
    $t->process(
        $template,
        {
            cgi          => $q,
            gramene_page => $page,
            title        => 'Gramene Diversity Search',
            no_header    => 1,
        },
        \$html
    ) or $html = $t->error;
};

#
# Error handler
#
if ( $@ ) {
    if ( $t ) {
        $t->process(
            'error.tmpl',
            {
                cgi          => $q,
                title        => 'Error',
                gramene_page => $page,
                err_msg      => $@,
            },
            \$html
        ) or $html = $t->error;
    }
    else {
        $html = $@;
    }
}
print $q->header, $html;
exit(0);

# ----------------------------------------------------

=pod

=head1 NAME

div_wrapper - creates headers and footers for Panzea searchces

=head1 DESCRIPTION

This script calls Gramene::Page's methods for wrapping Panzea's
diversity searches.

=head1 SEE ALSO

Gramene::Page, Template.

=head1 AUTHOR

Ken Youens-Clark E<lt>kclark@cshl.eduE<gt>.

=head1 COPYRIGHT

Copyright (c) 2006 Cold Spring Harbor Laboratory

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

=cut
