#!/usr/bin/env perl

use warnings;
use strict;

use LWP::UserAgent;
use Getopt::Long qw( GetOptions :config auto_help auto_version gnu_getopt );
use XML::LibXML;
use File::Temp qw(tempfile);


my $batchName = undef;
my $file = undef;

GetOptions(
        'batch|b=s' => \$batchName,
        'file|f=s' => \$file,
        );

my $dxWebServiceUrl = "http://lsb01.psps.ifa.hawaii.edu/cgi-bin/dxlayer/dxwebservice.cgi";
#my $dxWebServiceUrl = "http://166.122.172.121/cgi-bin/dxlayer/dxwebservice.cgi";
my $ua = LWP::UserAgent->new;
$ua->timeout(15);
$ua->env_proxy;

    my $doc = XML::LibXML::Document->new(); # Global document variable.
    my $rootNode = $doc->createElement('AdminQueryParams');
$doc->setDocumentElement($rootNode);

my $paramNode = $doc->createElement('Param');
$paramNode->setAttribute('Name', "BatchID"); # default FirstRow is 1
my $text=XML::LibXML::Text->new($batchName);
$paramNode->appendChild($text); #append
$rootNode->appendChild($paramNode); # append a node to the xml tree

my $xmlParams = $doc->toString; 

print  $xmlParams;

my $response = $ua->post($dxWebServiceUrl,
        [op => "delete_dxbatch_files",
        xmlParams => $xmlParams ]
        );

# '200' is the 'OK' response
if ($response->code != 200) {

    print "Problem connecting to DXLayerweb service, HTTP response status: ".$response->status_line."\n";
    exit(1);
}

open (DAT, ">".$file);
print DAT $response->content;


print "$file\n";

exit(0);



