use XMLRPC::Lite;
use Data::Dumper;
use HTTP::Cookies;
use File::Basename;
use File::Spec;

#
# create cookie jar to emulate the browser
#
my $cookie_jar = new HTTP::Cookies('file' => File::Spec->catdir(dirname
('/tmp/bugz_cookies/'), 'cookies.txt'),
                      'autosave' => 1);

#
# establish XML::RPC proxy
#
my $Bugzilla_uri = shift @ARGV || 'http://bugtest.mvista.com';
$Bugzilla_uri .= '/xmlrpc.cgi';
my $proxy = XMLRPC::Lite->proxy($Bugzilla_uri,
                                'cookie_jar' => $cookie_jar);
my $soapresult;

#
# check bugzilla version
#
$soapresult = $proxy->call('Bugzilla.version');
_die_on_fault($soapresult);
print 'Connecting to a Bugzilla of version ' . $soapresult->result()->{version} . ".\n";


my $login      = 'cchan';
my $password   = shift @ARGV || 'test';
my $remember   = 1;

#
# login LDAP user, password
#
$soapresult = $proxy->call('User.login', {login=>$login,
    password=>$password,
                            remember=>$remember});
_die_on_fault($soapresult);

#
# create a bug with basic fields
#

#$soapresult = $proxy->call('Bug.find', 
#  {constraint => [
#     [
#       [], #row
#     ],  #chart
#   ],
#   _order =>'Reuse+same+sort+as+last+time',});
#_die_on_fault($soapresult, 'Missing Column Test invalid constraint');

$soapresult = $proxy->call('Bug.search', 
  {criteria => [
     [
       [
        #["delta_ts", "greaterthan", "+6h"], # col 1
        # ["delta_ts", "greaterthan", "-4w"], # col 1  # pass thru test passed
          ["bug_num", "equals", "1"], ["bug_num", "equals", "2"] # col 1 OR col2  test passed
        # ["LCHG", "greaterthan", "-4w"], # col 1  # last changed Short Form
       ], #row 1 
       [
        #["delta_ts", "greaterthan", "+6h"], # col 1
        # ["cust_issue", "equals", "1"], # col 1  # non static, non custom fields, MVISTA only
       ], #row 1 
     ],  #chart 1
   ],
   get_duplicates => 1, # get duplicate test passed
   order =>'bugs.bug_id DESC',});
_die_on_fault($soapresult);

print "****\n" x 3;
$result = $soapresult->result;
foreach my $bug (@{$result->{bugs}}) {
    print "id : $bug->{id}\n";
    print "summary : $bug->{summary}\n";
    print "last_change_time : $bug->{last_change_time}\n";
    print "resolution : '$bug->{resolution}'\n";  # resolution/status and duplicate test passed
    print "status: '$bug->{status}'\n";
    if ($bug->{duplicate_of} ne '') {
	print "bug $bug->{id} is a duplicate of " . $bug->{duplicate_of} . "\n" 
    } else {
	print "bug $bug->{id} has no duplicate\n";
    }
    print "\n";
}

#
# logout
#
$soapresult = $proxy->call('User.logout');
_die_on_fault($soapresult);

sub _die_on_fault {
    my $soapresult = shift;
    my ($stay_alive) = @_;

    if ($soapresult->fault) {
        my ($package, $filename, $line) = caller;
        my $err =  $soapresult->faultcode . ' ' . $soapresult->faultstring .
            " in SOAP call near $filename line $line.\n";
        die $err unless ($stay_alive);
        print "$stay_alive --> " . $err;
    }
}
