#! /usr/bin/perl

# Copyright 2007 Cybozu Labs, Inc. All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation 
#    and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY CYBOZU LABS, INC. ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
# EVENT SHALL CYBOZU LABS, INC. OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

use strict;
use warnings;

use Getopt::Long;
use List::MoreUtils qw/apply/;
use Net::Stomp;

our $VERSION = 0.02;

my $host = 'localhost';
my $port = 61613;
my $persistent = 1;
my $user = $ENV{USER};
my $pass = '';
my $dest;
my $subs;
my $escape;
my $help;
my $version;

GetOptions(
    'host=s'        => \$host,
    'port=i'        => \$port,
    'persistent=i'  => \$persistent,
    'username=s'    => \$user,
    'password=s'    => \$pass,
    'destination=s' => \$dest,
    'subscribe=s'   => \$subs,
    'escape=s'      => \$escape,
    'help'          => \$help,
    'version'       => \$version,
) or exit 1;

if ($help) {
    print << 'EOT';
Usage: stompy [option]

Stompy is a command line frontend for the STOMP protocol.

Options:
  --host=host         default: localhost
  --port=port         default: 61613
  --persistent=[0|1]  default: 1
  --username=user     default: $ENV{USER}
  --password=pass     default: empty string
  --destination=path  destination
  --subscribe=path    subscription path
  --escape=exp        perl expr. for escaping the data received, default: none
  --help              print this help
  --version           print version

Either of --destination or --subscribe is mandatory.

EOT
    ;
    exit 0;
} elsif ($version) {
    print << "EOT";
stompy $VERSION
Written by Kazuho Oku

Copyright (c) 2007  Cybozu Labs, Inc.  All rights reserved.
EOT
    ;
    exit 0;
}

if ($escape) {
    local $@;
    $escape = eval << "EOT"
sub {
  \$_ = \$_[0];
  $escape;
  \$_;
}
EOT
    ;
    die "$@\n" if $@;
}


my $stomp = Net::Stomp->new({
    hostname   => $host,
    port       => $port,
    persistent => $persistent,
}) or die $!;

$stomp->connect({
    login    => $user,
    passcode => $pass,
}) or die $!;

if ($dest) {
    $stomp->send({
        destination => $dest,
        body        => do { local $/ = undef; <STDIN> },
    }) or die $!;
} else {
    $stomp->subscribe({
        destination             => $subs,
        'ack'                   => 'auto',
        'activemq.prefetchSize' => 1,
    }) or die $!;
    while (my $frame = $stomp->receive_frame) {
        my $body = $frame->body;
        $body = $escape->($body) if $escape;
        print $body;
        STDOUT->flush;
    }
}

$stomp->disconnect;
