#!/usr/bin/env perl
# PODNAME: bee
# ABSTRACT: Small application to handle bee files

use strict;
use warnings;
use Carp qw( croak );
use BeePack;
use File::Temp qw/ tmpnam /;
use Data::Dumper;

my $beepack = shift @ARGV;

my $key = shift @ARGV;
my $type = shift @ARGV;
my @values = @ARGV;
my $tmpfile = tmpnam();
my @args = ( $beepack, defined $type ? ( $tmpfile ) : () );
my $bee = BeePack->open(@args);

if (defined $key) {
  if (defined $type) {
    if ($type =~ m/^n/) {
      croak("nil type can't have a value") if scalar @values > 0;
      $bee->set_nil($key);
    } elsif ($type =~ m/^i/) {
      croak("integer can only handle one value") if scalar @values > 1;
      $bee->set_integer($key,$values[0]);
    } elsif ($type =~ m/^s/) {
      croak("string can only handle one value") if scalar @values > 1;
      $bee->set_string($key,$values[0]);
    } elsif ($type =~ m/^b/) {
      croak("bool can only handle one value") if scalar @values > 1;
      $bee->set_bool($key,$values[0]);
    } elsif ($type =~ m/^a/) {
      $bee->set($key,\@values);
    } elsif ($type =~ m/^h/) {
      croak("hash needs even number") if scalar @values % 2;
      $bee->set($key,{ @values });
    } else {
      croak("Unknown type ".$type);
    }
    $bee->save;
  } else {
    my $val = $bee->get($key);
    if (defined $val) {
      my $ref = ref $val;
      if ($ref eq 'ARRAY' || $ref eq 'HASH') {
        print Dumper $val;
      } else {
        print $val;
      }
    }
  }
} else {
  for ($bee->keys) {
    print $_."\n";
  }
}

exit 0;

__END__

=pod

=head1 NAME

bee - Small application to handle bee files

=head1 VERSION

version 0.102

=head1 SYNOPSIS

  # show keys
  $ bee test.bee

  # get key
  # integer displays as ascii numbers
  # bool displays as true or false
  # nil value is not displayed
  # arrays and hashs are displayed as Data::Dumper dump
  $ bee test.bee key

  # sample complex key, no need for escaping
  $ bee test.bee web#img/background.jpg#content

  # get is transparent on string
  $ bee test.bee gzipped_value | gunzip -c

  # set key to nil
  $ bee test.bee key n

  # set bool key
  $ bee test.bee key b 0

  # set integer key
  $ bee test.bee key i 123

  # set string key
  $ bee test.bee key s "This is a test"

  # set array key
  $ bee test.bee key a "Peter Parker" "Bruce Banner" "Clark Jerome Kent"

  # set hash key
  $ bee test.bee key h \
    spiderman "Peter Parker" \
    hulk "Bruce Banner" \
    superman "Clark Jerome Kent"

=head1 SUPPORT

IRC

  Join #vonBienenstock on irc.freenode.net. Highlight Getty for fast reaction :).

Repository

  http://github.com/Getty/p5-beepack
  Pull request and additional contributors are welcome

Issue Tracker

  http://github.com/Getty/p5-beepack/issues

=head1 AUTHOR

Torsten Raudssus <torsten@raudss.us>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by Torsten Raudssus.

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

=cut
