######################################################################
#
#  EPrints Simple Search Form
#
######################################################################
#
#
# Copyright 2000-2008 University of Southampton. All Rights Reserved.
# 
#  This file is part of GNU EPrints 3.
#  
#  Copyright (c) 2000-2008 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 3 is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#  
#  EPrints 3 is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with EPrints 3; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################

use EPrints;

use strict;
use warnings;
my $repo = EPrints->new->current_repository;
exit( 0 ) unless( defined $repo );

my $path_info = $repo->get_request->path_info;
# lose a leading slash
$path_info =~ s#^/##;

my $args = $repo->get_request->args;
$args = "" if !defined $args;
$args = "?$args" if length( $args );

my( $datasetid, $searchid ) = split /\//, $path_info;

if( !defined $datasetid )
{
	$repo->redirect( $repo->config( "http_cgiroot" )."/search/simple$args" );
	exit;
}

# cover the old systems sins...
if( $datasetid eq "advsearch" )
{
	$repo->redirect( $repo->config( "http_cgiroot" )."/search/advanced$args" );
	exit;
}

if( defined($repo->param( "dataset" )) )
{
	if( !$searchid )
	{
		$repo->redirect( $repo->config( "http_cgiroot" )."/search/".$repo->param( "dataset" )."/$datasetid$args" );
		exit;
	}
	elsif( $searchid =~ /^export/ )
	{
		$repo->redirect( $repo->config( "http_cgiroot" )."/search/".$repo->param( "dataset" )."/$datasetid/$searchid$args" );
		exit;
	}
	# handle the user changing the search dataset after a search
	elsif( $repo->param( "dataset" ) ne $datasetid )
	{
		$repo->redirect( $repo->config( "http_cgiroot" )."/search/".$repo->param( "dataset" )."/$searchid$args" );
		exit;
	}
}

if( $datasetid =~ /^(advanced)|(simple)$/ )
{
	$searchid = $datasetid;
	$datasetid = "archive";
}
elsif( !defined $searchid )
{
	# /cgi/search/quicksearch i.e. repository-specific search spec. that
	# hopefully doesn't match an existing dataset id
	if( !defined $repo->dataset( $datasetid ) )
	{
		$searchid = $datasetid;
		$datasetid = "archive";
	}
	else
	{
		$repo->redirect( $repo->config( "http_cgiroot" )."/search/$datasetid/simple$args" );
		exit;
	}
}

my $dataset = $repo->dataset( $datasetid );
if( !defined $dataset )
{
	$repo->not_found;
	exit;
}

EPrints::ScreenProcessor->process( 
	session => $repo, 
	url => $repo->config( "http_cgiroot" )."/search/$datasetid/$searchid",
	screenid => "Search",
	searchid => $searchid,
	dataset => $dataset,
);

exit;
