#!/usr/bin/perl

use strict;
use warnings;

use App::Siesh;
use Getopt::Long;
use IO::Prompt;
use Config::Find;

our $VERSION=0.09;

sub equal_sep {
    my $file = shift;
    open my $in, $file or die $!;
    my %config;
    while (<$in>) {
        next if /^\s*#/;
        /^\s*(.*?)\s*=\s*(.*)\s*$/ or next;
        my ( $k, $v ) = ( $1, $2 );
        my @v;
        if ( $v =~ /,/ ) {
            $config{$k} = [ split /\s*,\s*/, $v ];
        }
        elsif ( $v =~ / / ) {    # XXX: Foo = "Bar baz"
            $config{$k} = [ split /\s+/, $v ];
        }
        else {
            $config{$k} = $v;
        }
    }
    return \%config;
}


my %config;

if ( my $filename = Config::Find->find ) {
    %config = %{ equal_sep($filename) };
}

my $result = GetOptions( \%config,
    'debug',
    'user=s',
    'host=s',
    'port=s',
    'tls=s',
    'password=s',
) or pod2usage(2);

if(!$config{password}) {
	$config{password} = prompt( "Password: ", -e => '*' );
}

App::Siesh->run(%config);

0;

__END__

=head1 NAME

siesh - interactive sieve shell

=head1 SYNOPSIS

$ siesh
    --debug 
    --user USER 
    --host SERVER 
    --tls {require|auto} 
    --port PORT
    --password PASSWORD

=head1 DESCRIPTION

Siesh provides a shell-like interface for manipulating sieve scripts
using the ManageSieve protocol.

See the L<App::Siesh> documentation for more details.

=head1 OPTIONS

=over 4

=item B<--debug>|B<-d>

Enable debugging.

=item B<--user>|B<-u> USERNAME

Specifies the username to use when logging into the sieve server. This
option defaults to the value of the environment variable C<USER>.

=item B<--host>|B<-h> HOST

Specifies the machine to connect to. Defaults to C<imap>.

=item B<--port>|B<-p> PORT

Specifies the remote port to connect to. Defaults to C<2000>.

=item B<--tls>|B<-t> {require|auto}

Specifies whether TLS is required ("require") or optional ("auto"). Defaults to unset, disabling TLS.

=item B<--password> PASSWORD

Specifies the password to login.

=back

=head1 CONFIGURATION FILE

When siesh is invoked, it first reads configurations variables from
F</etc/siesh.conf> or F<~/.siesh.conf> if one of these files exist. The
file is structured as a set of lines with name and values seperated by
an equal sign.

	user     = mario
	host     = sieve.example.com
	port     = 2000
	tls      = require
	debug    = 1
	password = secret

Currently only these six options are recognized. Values are overriden
by options specified on the command line.

=head1 SEE ALSO

L<App::Siesh>, L<Net::ManageSieve::Siesh>, L<Net::ManageSieve>

=head1 COPYRIGHT & LICENSE

Copyright 2008 Mario Domgoergen, all rights reserved.

This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
