#!/usr/bin/perl -CA

use strict;
use utf8;

use Getopt::Std;
use WWW::Tumblr;

our ($opt_h, $opt_b, $opt_f);
getopts('hb:f:');

my $skellington = <<'END';
our $blog = '???.tumblr.com';
our $tokens = {
  consumer_key => '',
  secret_key => '',
  token => '',
  token_secret => ''
};
END

my $config = $opt_f || "$ENV{HOME}/.tmblr";

my $usage = "$0 [-h] [-b blog] [-f config] ID";

our ($tokens, $blog);

$blog = $opt_b if $opt_b;

do $config or warn("unable to parse $config") if -f $config;
if(!$tokens || !$blog) {
  say STDERR "put your blog name and your silly tokens in $config";
  if(!-f $config) {
    open(my $file, ">", $config);
    say $file $skellington;
    close($file);
  }
  exit(1);
}

my $t = WWW::Tumblr->new($tokens);
my $tumblr = $t->blog($blog);

if($opt_h) {
  print("$usage\n");
  exit(0);
}

if($ARGV[0]) {
  my ($id) = $ARGV[0] =~ /([0-9]+)$/;
  my $ok = $tumblr->post_delete(id => $id);
  if($ok) {
    print("$id deleted\n");
  } else {
    print("that doesn't work\n");
  }
} else {
  print("$usage\n");
  exit(1);
}
__END__

=head1 NAME

tmblr-rm - delete tmblr posts

=head1 SYNOPSIS

B<tmblr> [-h] [-b blog] [-f config] ID

=head1 DESCRIPTION

Delete posts from tumblr.

=head1 OPTIONS

=over

=item B<-h>

Display usage information and exit.

=item B<-b> I<blog>

Post to I<blog>.

=item B<-f> I<config>

Read the blog name and oauth tokens from the file -I<config>.
Defaults to ~/.tmblr.

=back

=head1 EXAMPLES

  $ tmblr-rm 92831985892

=head1 AUTHOR

bie E<lt>bie@kyber.ioE<gt>

=head1 COPYRIGHT

Copyright (C) 2015 by bie

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.22.0 or,
at your option, any later version of Perl 5 you may have available.

=cut
