#!/usr/bin/perl

use EPrints;
use strict;

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

my $from_javascript = EPrints::Utils::is_set( $session->param("callback") ) ? 1 : 0;
my $type = $session->param("type");

my $current_user = $session->current_user();

if(!defined $current_user)
{
	print STDOUT $session->phrase("sneep_not_logged_in");
	$session->terminate;
	return; 
}

my $sneep = EPrints::DataObj::Sneep->new($session, $session->param( "sneepid" ) );

if(!defined $sneep)
{
        print STDOUT $session->phrase( "sneep_does_not_exist" );
        $session->terminate;
        return;
}

if(!$sneep->can_edit)
{
        print STDOUT $session->phrase( "sneep_no_permission" );
        $session->terminate;
        return;
}

$sneep->remove();

&render_response($session, $sneep, $type, $from_javascript);

$session->terminate;

return;

sub render_response
{
	my( $session, $sneep, $type, $from_javascript ) = @_;

	if( $from_javascript )
        {
                $session->get_request->content_type("application/json");
                $session->get_request->headers_out->{'Cache-control'} = 'No-cache';
		# difference?
                if($sneep->can_edit)
		{
                        print '{ "status": 1, "sneepid": '.$sneep->get_id.' }';
                }
		else
		{
                        print '{ "status": 1, "sneepid": '.$sneep->get_id.' }';
                }
        }
        else
        {
                my $cgi_url = $session->get_repository()->get_conf("perl_url");
                $session->redirect($cgi_url."/sneep/sneep_page?eprintid=".$sneep->get_value("eprintid")."&type=".$type);
        }
}

