#!/usr/bin/perl -I/opt/perl_modules/SOAP-Lite-0.60/blib/lib

use strict;

use SOAP::Lite;
#    on_fault => sub { my($soap, $res) = @_; 
#      eval { die ref $res ? $res->faultdetail : $soap->transport->status };
#      return ref $res ? $res : new SOAP::SOM;
#    };
#SOAP::Lite->import(+trace => debug);

use SOAP::MIME;
use Data::Dumper;

my $uri   = 'urn:Dbfetch';
my $proxy = 'http://www.ebi.ac.uk/ws/services/Dbfetch';

my $soap = new SOAP::Lite(uri   => $uri,
                          proxy => $proxy);

my $method;
my $query;
my $format;
my $style;

my @allowedMethods = ("getSupportedDBs", "getSupportedFormats", "getSupportedStyles", "fetchData", "fetchDataFile");
my @allowedMethods1 = ("getSupportedDBs", "getSupportedFormats", "getSupportedStyles");
my @allowedMethods2 = ("fetchData", "fetchDataFile");
my $validMethod = 0;

if (! @ARGV) {
    &printUsage;
}
else {
    my $numArgs = $#ARGV + 1;
    $method = shift @ARGV;
    
    foreach my $i (@allowedMethods) {
        if ($method eq $i) {
            $validMethod = 1;
            last;
        }
    }
    if ($validMethod == 0) {
        print "$method is not a valid method\n";
        &printUsage;
    }
    else {
        foreach my $i (@allowedMethods1) {
            if ($method eq $i) {
                &makeSOAPCall;
            }
        }
        if ( $numArgs >= 2) {
            $query	= $ARGV[0];
            $format = defined($ARGV[1]) ? $ARGV[1] : "default";
            $style	= defined($ARGV[2]) ? $ARGV[2] : "raw";
            if ($method eq "fetchData") {
                &makeSOAPCall;
            }
            else {
                my $som = $soap->call($method => $query, $format, $style);
                if ($som->fault) {
                    print $som->faultdetail;
                    print $som->faultcode;
                    print $som->faultstring;
                    print $som->faultactor;
                }
                else {
                    print $som->result;
                }
                exit(0);
            }
        }
        else {
            print "Invalid number of parameters\n";
            &printUsage;
        }
    }
}

sub makeSOAPCall {
    
    my $result;


    if ($method eq "fetchData") {
        $result = $soap->call($method => $query, $format, $style);
    }
    else {
        $result = $soap->call($method);
    }
    if ($result->fault) {
        print STDERR $result->faultcode . " " . $result->faultstring . "\n";
    }
    else {
        $result  = $result->result;
        foreach my $i (@$result) {
            print "$i\n";
        }
    }
    exit(0);
}

sub printUsage {
    print "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n";
    print "USAGE: \n";
    print "------ \n";
    print "getSupportedDBs \n\n";
    print "getSupportedFormats \n\n";
    print "getSupportedStyles \n\n";
    print "fetchData dbName:id <output format> <output style> \n";
    print "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++\n\n";
    exit(0);
}
