######################################################################
#
# EPrints::Apache::WebDAV
#
######################################################################
#
#
# Copyright 2000-2009 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
#
######################################################################

package EPrints::Apache::WebDAV;

use EPrints;
use EPrints::Filesys;
use EPrints::Apache::AnApache; # constants
use Apache2::WebDAV;

use strict;
use warnings;

sub handler
{
	my( $r ) = @_;

	my $rc = OK;

binmode(STDERR, ":utf8");
print STDERR ('-'x79)."\n".$r->method." ".Encode::decode_utf8($r->uri)." ".$r->protocol."\n";
print STDERR "HEADERS_IN\n";
foreach my $key (keys %{$r->headers_in})
{
	print STDERR sprintf("%20s: %s\n", $key, $r->headers_in->{$key});
}

	my $session = EPrints::Session->new( 2 );

	my $auth_name = "DAV";

	# Require authentication
	if( !defined $session->current_user )
	{
		$r->auth_name( $auth_name );
		$r->ap_auth_type( "Basic" );
		$rc = EPrints::Apache::Auth::auth_basic( $r, $session );
		if( $rc == OK )
		{
			$session->{current_user} = $session->_current_user_auth_basic;
		}
	}

	if( !defined $session->current_user )
	{
		$r->err_headers_out->{'WWW-Authenticate'} = "Basic realm=\"$auth_name\"";
	}

	if( defined $session->current_user )
	{
print STDERR "current_user=".$session->current_user->get_value( "username" )."\n";

		my $dav = Apache2::WebDAV->new;

		my @handlers = ({
			path => "/DAV",
			module => "EPrints::Filesys",
			args => {
				root_path => "/DAV",
				session => $session,
				current_user => $session->current_user,
			},
		});

		$dav->register_handlers(@handlers);

		$rc = $dav->process( $r );
	}

print STDERR "HEADERS_OUT\n";
foreach my $key (keys %{$r->headers_out})
{
	print STDERR sprintf("%20s: %s\n", $key, $r->headers_out->{$key});
}
print STDERR "ERR_HEADERS_OUT\n";
foreach my $key (keys %{$r->err_headers_out})
{
	print STDERR sprintf("%20s: %s\n", $key, $r->err_headers_out->{$key});
}
print STDERR "RESULT ".($rc||$r->status_line||$r->status)."\n";
print STDERR "\n";

	$session->terminate;

	return $rc;
}

1;
