#!/usr/bin/perl
# $Id: oardel,v 1.13 2005/06/01 16:20:42 capitn Exp $
# delete a job
use strict;
use warnings;
use Data::Dumper;
use DBI();
use oar_iolib;
use IO::Socket::INET;
use oar_conflib qw(init_conf dump_conf get_conf is_conf);


sub usage {
    print STDERR "usage: oardel [idJob]\n";
    exit 1;
}

init_conf("oar.conf");
my $remote_host = get_conf("SERVER_HOSTNAME");
my $remote_port = get_conf("SERVER_PORT");

usage if (@ARGV < 1);

my $idJob = $ARGV[0];
if ($idJob !~ m/^\s*\d+\s*$/m){
    die("\"$idJob\" is not a valid OAR job identifier. It must be a number.\n");
}

my $base = iolib::connect();
print("Deleting the job = $idJob...");
my $err = iolib::frag_job($base,$idJob);
iolib::disconnect($base);

if ($err != 0) {
    print("ERROR.\n");
    warn("Can't frag $idJob ; You are not the right user.\n");
    exit 1;
}else{
    print("REGISTERED.\n");
    #Signal Almigthy

    my $socket = IO::Socket::INET->new(PeerAddr => $remote_host,
                                       PeerPort => $remote_port,
                                       Proto => "tcp",
                                       Type  => SOCK_STREAM)
                 or die("Couldn't connect executor $remote_host:$remote_port\nContact your OAR administrator and relaunch the oardel command.\n");

    print $socket "Qdel\n";
    print("The job $idJob will be deleted in a near futur.\n");
}

exit 0;
