#!/usr/bin/perl

=head1 NAME

pushbullet

=head1 DESCRIPTION

Program giving easy access to PushBullet features

=head1 SYNOPSIS

    pushbullet address [ -k <pushbullet_apikey> ] [ -d <device_id> ]
        --name 'address name' --address 'complete address'
        
    pushbullet devices [ -k <pushbullet_apikey> ]
    
    pushbullet file [ -k <pushbullet_apikey> ] [ -d <device_id> ] -f filename
    
    pushbullet link [ -k <pushbullet_apikey> ] [ -d <device_id> ]
        --title 'your title' --url 'http://address'
        
    pushbullet list [ -k <pushbullet_apikey> ] [ -d <device_id> ]
        --title 'your title' --item item1 --item item2 --item item3
        
    pushbullet note [ -k <pushbullet_apikey> ] [ -d <device_id> ]
        --title 'your title' --body 'your body message'
    
=head1 OPTIONS

=over 4

=item B<-a>, B<--address>

Sets address for 'pushbullet address'

=item B<-b>, B<--body>

Sets body for 'pushbullet note'

=item B<-c>, B<--config>

Sets configuration file where to find default parameters configuration

=item B<-D>, B<--debug>

Sets debug mode

=item B<-d>, B<--device_id>

Sets device to receive bullet

=item B<-f>, B<--file>

Sets file for 'pushbullet file'

=item B<-h>, B<--help>

Prints this Help

=item B<-k>, B<--apikey>

Sets PushBullet API key

=item B<-n>, B<--name>

Sets name for 'pushbullet address'

=item B<-t>, B<--title>

Sets title for 'pushbullet link', 'pushbullet list' or 'pushbullet note'

=item B<-u>, B<--url>

Sets url for 'pushbullet address'

=item B<-v>, B<--version>    

Prints WWW::PushBullet version

=back

=cut

use strict;
use warnings;

use File::Slurp;
use FindBin;
use Getopt::Long;
use JSON;
use Pod::Usage;

use lib "$FindBin::Bin/../lib/";

use WWW::PushBullet;

my $FILE_CONFIG = "$FindBin::Bin/../conf/pushbullet.json";

=head1 SUBROUTINES/METHODS

=head2 Read_File_Config($file_config)

Reads configuration file

=cut

sub Read_Config_File
{
    my $file_config = shift;

    $file_config ||= $FILE_CONFIG;
    if (-r $file_config)
    {
        my $json = read_file($file_config);
        return (JSON->new->decode($json));
    }

    return (undef);
}

#
# MAIN
#

my %opt       = ();
my @opt_items = ();

pod2usage(0) if (@ARGV < 2);
$opt{action} = $ARGV[0];
pod2usage(0) if ($opt{action} !~ /^(?:address|devices|file|link|list|note)$/);

my $status = GetOptions(
    'a|address=s'   => \$opt{address},
    'b|body=s'      => \$opt{body},
    'c|config=s'    => \$opt{file_config},
    'D|debug'       => \$opt{debug},
    'd|device_id=s' => \$opt{device_id},
    'h|help'        => \$opt{help},
    'f|file=s'      => \$opt{file},
    'i|item=s'      => \@opt_items,
    'k|apikey=s'    => \$opt{apikey},
    'n|name=s'      => \$opt{name},
    't|title=s'     => \$opt{title},
    'u|url=s'       => \$opt{url},
    'v|version'     => \$opt{version},
);
pod2usage(0) if ((!$status) || ($opt{help}));

my $conf = Read_Config_File($opt{file_config});

my $apikey    = $opt{apikey}    || $conf->{apikey};
my $address   = $opt{address}   || $conf->{default_address};
my $body      = $opt{body}      || $conf->{default_body};
my $device_id = $opt{device_id} || $conf->{default_device_id};
my $file      = $opt{file}      || $conf->{default_file};
my $items = (scalar @opt_items ? \@opt_items : $conf->{default_items});
my $name  = $opt{name}  || $conf->{default_name};
my $title = $opt{title} || $conf->{default_title};
my $url   = $opt{url}   || $conf->{default_url};

printf "API Key: [%s]\nDevice ID: [%s]\n", $apikey, $device_id
    if ($opt{debug});

my $pb = WWW::PushBullet->new({apikey => $apikey});

if ($opt{action} eq 'address')
{
    if ((defined $name) && (defined $address))
    {
        $pb->push_address(
            {
                device_id => $device_id,
                name      => $name,
                address   => $address
            }
        );
    }
    else
    {
        printf "pushbullet address --name name --address 'complete address'\n";
    }
}
elsif ($opt{action} eq 'devices')
{
    my $devices = $pb->devices();
    foreach my $d (@{$devices})
    {
        printf "Device '%s' => id %s\n", $d->{extras}->{model}, $d->{id};
    }
}
elsif ($opt{action} eq 'file')
{
    if (defined $file)
    {
        $pb->push_file({device_id => $device_id, file => $file});
    }
    else
    {
        printf "pushbullet file --file filename\n";
    }
}
elsif ($opt{action} eq 'link')
{
    if ((defined $title) && (defined $url) && ($url =~ /^https?:/))
    {
        $pb->push_link(
            {
                device_id => $device_id,
                title     => $title,
                url       => $url
            }
        );
    }
    else
    {
        printf "pushbullet link --title title --url http://address\n";
    }
}
elsif ($opt{action} eq 'list')
{
    if ((defined $title) && (scalar @{$items}))
    {
        $pb->push_list(
            {
                device_id => $device_id,
                title     => $title,
                items     => $items
            }
        );
    }
    else
    {
        printf
"pushbullet list --title title --item item1 --item item2 --item item3\n";
    }
}
elsif ($opt{action} eq 'note')
{
    if ((defined $title) && (defined $body))
    {
        $pb->push_note(
            {
                device_id => $device_id,
                title     => $title,
                body      => $body
            }
        );
    }
    else
    {
        printf "pushbullet note --title title --body 'body message'\n";
    }
}

=head1 AUTHOR

Sebastien Thebert <stt@ittool.org>

=cut
