######################################################################
#
#  EPrints 404 Error Hander
#
######################################################################
#
#  This file is part of GNU EPrints 2.
#  
#  Copyright (c) 2000-2004 University of Southampton, UK. SO17 1BJ.
#  
#  EPrints 2 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 2 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 2; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
######################################################################


#cjg Not fully finnished. Should detect missing items.

use EPrints;

use strict;

my $session = new EPrints::Session();
exit( 0 ) unless( defined $session );

my $url = $ENV{REDIRECT_URL};


my $newurl;
my $fn = $session->get_repository->get_conf( "catch404" );
if( defined $fn )
{
	$newurl = &{$fn}( $session, $url );
}

if( defined $newurl )
{
	$session->redirect( $newurl );
}
elsif( $url =~ m#^/view/# )
{
	&noview_404( $session );
}
else
{
	&plain_404( $session );
}

$session->terminate();

## Try and work out if the user is trying to access a deleted document
#my $url = $ENV{REDIRECT_URL};
#
#my $was_deleted = 0;
#
## Look for digits in the path
#if( $url =~ m#/(\d+)/(\d\d)/(\d\d)/(\d\d)/# )
#{
#	# There's a match, looks like we have an ID
#	my $guessed_id = $1.$2.$3.$4;
#
#	# Was the eprint deleted?
#	#cjg!
#	#my $deletion_record = new EPrints::Deletion( $session, $guessed_id );
#	
#	if( defined $deletion_record )
#	{
#		# The user seems to be trying to get to a deleted eprint. 
#		print $session->{render}->render_deleted_eprint( $deletion_record );
#	}
#	else
#	{
#		# See if the eprint exists.
#		my $eprint = new EPrints::DataObj::EPrint(
#			$session,
#			$guessed_id,;
#			EPrints::Database::table_name( "archive" ) );
#		
#		if( defined $eprint )
#		{
#			# Seems to be a slightly malformed access to an existing eprint.
#			existing_record( $session, $eprint );
#		}
#		else
#		{
#			# Not a deleted or existing eprint: a plain old Document Not Found
#			plain_404( $session );
#		}
#	}
#}
#else
#{
#	# No match, can't find any ID. Just a plain old 404.
#	plain_404( $session );
#}
#
#$session->terminate();
#
#
#
#######################################################################
##
## existing_record( $session, $eprint )
##
##  Render an appropriate erro saying that the user seems to be trying
##  to get to a particular eprint, but may have just entered the URL
##  incorrectly.
##
#######################################################################
#
#sub existing_record
#{
#	my( $session, $eprint ) = @_;
#	
#	print $session->{render}->start_html( 
#		$session->{lang}->phrase( "cgi/handle_404:doc_not_found" ) );
#
#	print "<P>";
#	print $session->{lang}->phrase( "cgi/handle_404:file_not_found" );
#	print "<CODE>$url</CODE></P>\n";
#	print "<P>";
#	print $session->{lang}->phrase( "cgi/handle_404:looking_for" );
#	print "</P>\n";
#	print "<P ALIGN=CENTER>";
#	print $session->{render}->render_eprint_citation( $eprint, 1, 1 );
#	print "</P>\n";
#	
#	print $session->{render}->end_html();
#}


######################################################################
#
# plain_404( $session )
#
#  Render a standard Document Not Found error message.
#
######################################################################

sub plain_404
{
	my( $session ) = @_;

	$session->build_page( 
		$session->html_phrase( "cgi/handle_http_error:404_title" ),
		$session->html_phrase( "cgi/handle_http_error:404_blurb", 
			url => $session->make_text( $ENV{REDIRECT_URL} ) ),
		"plain_404" );

        $session->send_page();
}


sub noview_404
{
	my( $session ) = @_;

	my $error = "The browse views section is missing. The site admin needs to run generate_views. This should be automated with cron. See the documentation for more details.";

	$session->build_page( 
		$session->html_phrase( "cgi/handle_http_error:404_title" ),
		$session->make_text( $error ),
		"noview_404" );
	$session->get_repository->log( $error );

        $session->send_page();
}


