#!/usr/bin/perl
# vim: set ts=4 sw=4 tw=78 et si ft=perl:
#
# leanpub
#
use 5.010;
use strict;
use warnings;

use Cwd;
use Getopt::Long;
use Pod::Usage;
use WebService::Leanpub;

my $defconffile = ".leanpub.conf";

my %opt = (
    really => 0,
);

Getopt::Long::Configure('require_order');
GetOptions( \%opt,
    'api_key=s', 'slug=s',
    'really',
    'help|?', 'manual')
    or pod2usage(2);

pod2usage(-exitval => 0, -verbose => 1, -input => \*DATA) if ($opt{help});
pod2usage(-exitval => 0, -verbose => 2, -input => \*DATA) if ($opt{manual});

read_config(\%opt);

my $cmd = shift;

pod2usage(0) unless ($cmd);

my $wl = WebService::Leanpub->new($opt{api_key},$opt{slug});

if ($cmd =~ /^individual/i) {
    my %lopt = ( page => 1, );
    GetOptions( \%lopt, 'page=i' );
    my $ip = $wl->get_individual_purchases( { page => $lopt{page} } );
    print $ip if ($ip);
}
elsif ($cmd =~ /^job_status/i) {
    my $js = $wl->get_job_status();
    print $js if ($js);
}
elsif ($cmd =~ /^preview/i) {
    my $pv = $wl->preview();
    print $pv if ($pv);
}
elsif ($cmd =~ /^publish/i) {
    if ($opt{really}) {
        my $pv = $wl->publish();
        print $pv if ($pv);
    }
    else {
        pod2usage(0);
    }
}
elsif ($cmd =~ /^sales/i) {
    my $sd = $wl->get_sales_data();
    print $sd if ($sd);
}

exit 0;

#--- only functions following ---
sub read_config {
    my ($opt) = @_;
    my $config;
    my $here = cwd();

    my $read = sub {
        my ($in) = @_;
        my $config = {};
        while (<$in>) {
            next if (/^\s*#/);
            if (/^\s*(\S+)\s*=\s*(\S.*)$/) {
                $config->{$1} = $2;
            }
        }
        return $config;
    };
    while ($here) {
        if ( -r $here . '/' . $defconffile
            && open my $in, '<', $here . '/' . $defconffile) {
            $config = $read->($in);
            close $in;
            foreach my $k (keys %$config) {
                unless ($opt->{$k}) {
                    $opt->{$k} = $config->{$k};
                }
            }
        }
        $here =~ s|/[^/]+$||;
    }

} # read_config()

__END__

=head1 NAME

leanpub - access the Leanpub web API

=head1 SYNOPSIS

 leanpub [options] command [command options]

=head2 OPTIONS

=over 8

=item B<< -api_key=key >>

Provide the Leanpub API key to be used for all actions.

=item B<< -help >>

Print a brief help message and exit.

=item B<< -manual >>

Print the manual page and exit.

=item B<< -really >>

State that you really intend to do the command (e.g. publish).

=item B<< -slug=your_book >>

Provide the book's slug.

=back

=head2 COMMANDS

=over 8

=item individual_purchases

Retrieve data about individual purchases.

This command takes the option C<< -page >> to set the page of the individual
purchases report to be retrieved.

=item job_status

Retrieve the status of the last job.

=item preview

Start a preview of your Book.

=item publish

Publish your book.

You have to use option B<< -really >> with this command.

=item sales_data

Retrieve a summary of sales data.

=back

=head1 DESCRIPTION

This program interacts with the Leanpub API.
See L<< https://leanpub.com/help/api >> for details about this API.

The slug is the part of the URL for your book coming
after C<< https://leanpub.com/ >>. For instance if your book is found at
C<< https://leanpub.com/your_book >>, then the slug for your book is
I<your_book>.

=head1 AUTHOR

Mathias Weidner

