#!/bin/env perl

use Sophie::Cli;
require RPC::XML;
require RPC::XML::Client;
use Getopt::Long;
use Term::ReadKey;

GetOptions(
	's=s' => \my $server,
) or die "Wrong options";

ReadMode('noecho');
print "Enter password: ";
my $password = ReadLine(0);
ReadMode 0;
print "\n";
chomp($password);

my $xmlcli = RPC::XML::Client->new($server || 'http://sophie.zarb.org/rpc');

my $res = $xmlcli->send_request('login', 'admin', $password);
if (ref $res) {
    $xmlcli->request->header('cookie', $$res);
} else {
    die "Can't login";
}

my $globalenv = Sophie::Cli::globalenv($xmlcli);

if (@ARGV) {
    $globalenv->run(@ARGV);
} else {
    $globalenv->cli;
}
