#!/usr/bin/perl

use EPrints;
use strict;

my $session = new EPrints::Session;
my $from_javascript = $session->param("callback");
my $type = $session->param("type");
Apache::exit( 0 ) unless( defined $session );

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

#TODO add error phrase
if(!defined $current_user){ 
	print "you're not a user";
	exit;
}


my $sneepid = $session->param("sneepid");

my $sneep = EPrints::DataObj::Sneep->new($session, $sneepid, $session->get_repository->get_dataset("sneep"));
#TODO add error phrase
if(!defined $sneep){
	print "no sneep defined!";
	exit;
}


#TODO add error phrase
if(!$sneep->can_edit){
	print "you dont have permission to delete this sneep";
	exit;
}

$sneep->remove();
render_response($from_javascript, $type, $sneep, $session);





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

        if ($from_javascript == "1")
        {
                $session->get_request->content_type("application/json");
                $session->get_request->headers_out->{'Cache-control'} = 'No-cache';
                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);
        }
}

