#!perl
use strict;
use warnings;
use Net::Eboks;
use Getopt::Long;

my %opt = (
	cpr        => '',
	password   => '',
	share      => '0',
	'list-shares' => 0,
	'list-messages' => 0,
	output     => 'eboks.mbox',
	n          => undef,
	help       => 0,
);

my $version = 0.01;

sub usage
{
	print <<USAGE;

$0

   --cpr           - CPR code, 0123456-7890
   --password      - Password
   --share         - Share name
   --list-shares   - Display list of available shares (0 is default)
   --list-messages - Display list of available messages
   --output        - output mail box ( $opt{output} )
   -n NUMBER       - dump only message NUMBER
   --help 

USAGE
	exit 1;
}

GetOptions(\%opt,
	"help|h",
	"cpr|c=s",
	"password|p=s",
	"share|s=s",
	"messages|g=s",
	"list-shares|l",
	"list-messages|m",
	"output|o=s",
	"n=i",
) or usage;

$opt{help} and usage();

sub fetch
{
	my $key = shift;
	return if length $opt{$key};
	$|=1;
	print "Enter $key code: ";
	system("stty -echo") if $^O !~ /win32/i;
	$opt{$key} = <STDIN>;
	chomp $opt{$key};
	system("stty echo") if $^O !~ /win32/i;
	print "\n";
}

fetch($_) for qw(cpr password);
$opt{cpr} =~ s/\-//g;

my $e = Net::Eboks->new(
	cpr        => $opt{cpr},
	password   => $opt{password},
);

my ($uname, $shares, $error);
print "Logging in...\n";
($uname, $error) = $e->fetch_request($e->login_nemid)->wait;
die "error: $error\n" if defined $error;
print "Welcome, $uname\n";

if ( $opt{'list-shares'} || $opt{share} ne '0') {
	print "Getting list of shares...\n";
	($shares, $error) = $e->fetch_request($e->shares)->wait;
	die "error: $error\n" if defined $error;
	my $found;
	for my $k ( sort keys %$shares ) {
		next if $k eq 'name';
		if ( $opt{share} ne '0' && $k eq $opt{share}) {
			$e->{share_id} = $shares->{$k}->{userId};
			$found = 1;
			print "Found share '$k'\n";
		}
		print "* $k: $shares->{$k}->{userId}\n" if $opt{'list-shares'};
	}
	if ( $opt{share} ne '0' && !$found) {
		die "Cannot find share '$opt{share}'\n";
	}
	exit if $opt{'list-shares'};
}


print "Getting list of folders...\n";
my ($folders, $list, $msg);
($folders, $error) = $e->fetch_request( $e->folders )->wait;
die "error: $error\n" if defined $error;

print "Getting list of messages in Inbox...\n";
($list, $error) = $e->list_all_messages(undef, $folders->{Inbox}->{id})->wait;
die "error: $error\n" if defined $error;
my @msgids = sort keys %$list;

print "Got ", scalar(@msgids), " messages ... \n";
unless ( $opt{'list-messages'} ) {
	open F, ">", $opt{output} or die "Cannot write $opt{output}:$!";
}

my $n = 0;
for my $msgid ( @msgids ) {
	$n++;
	next if defined $opt{n} && $opt{n} != $n;
	my $m = $list->{$msgid};
	print "Fetching #$n: ", $e->filename( $m ), " from $m->{receivedDateTime}...\n";
	next if $opt{'list-messages'};
	( $msg, $error ) = $e->fetch_message_and_attachments( $m )-> wait;
	die "error: $error\n" if defined $error;
	print F $e->assemble_mail(%$msg);
}
unless ( $opt{'list-messages'} ) {
	close F;
	print "Saved in $opt{output} as a mailbox. Open in your mail agent an enjoy!\n";
}
