#!/usr/bin/env perl

# Copyright (C) 2007-2008  Joshua Hoblitt
#
# $Id: neb-ls,v 1.7 2008-06-12 20:03:37 jhoblitt Exp $

use strict;
use warnings FATAL => qw( all );

use vars qw( $VERSION );
$VERSION = '0.02';

use Nebulous::Client;

use Getopt::Long qw( GetOptions :config auto_help auto_version );
use Pod::Usage qw( pod2usage );

my ($server, $long, $recursive);

$server = $ENV{'NEB_SERVER'} unless $server;

GetOptions(
    'server|s=s'    => \$server,
    'recursive|r'   => \$recursive,
    'l|1'           => \$long,
) || pod2usage( 2 );

my $pattern = shift;

pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
pod2usage( -msg => "Required options: --server", -exitval => 2 )
    unless $server;

my $neb = Nebulous::Client->new(
    proxy => "$server",
);

die "can't connected to Nebulous Server: $server"
    unless defined $neb;

# default to listing everything (bad idea?)
$pattern ||= ".*";

if ($recursive) {
    $pattern = "^" . $pattern . ".*";
} else {
    $pattern = "^" . $pattern . "\$";
}

my $keys = $neb->find_objects($pattern);

if ($keys) {
    if ($long) {
        print join("\n", @{ $keys }), "\n";
    } else {
        print join(" ", @{ $keys }), "\n";
    }
}

__END__

=pod

=head1 NAME

neb-ls - list Nebulous keys

=head1 SYNOPSIS

    neb-ls [--server <URL>] [-l|-1] [--recursive] <pattern>

=head1 DESCRIPTION

This program list Nebulous keys matched by C<<pattern>>.  Call it with no
arguments is equivalanet to searching with the pattern C<.*>.  Where
C<<pattern>> is a POSIX 1003.2 compatable regular repression.

=head1 OPTIONS

=over 4

=item * -l|-1

Use a long listing format.

Optional

=item * --recursive|-r

By default C<neb-ls> will only try to match the exact string provided to it.
With this option set all keys which match C<<pattern>> as a REGEX or substring
will be returned.

Optional

=item * --server|-s <URL>

URL of the Nebulous server to connect to.

Optional if the appropriate environment variable is set.

=back

=head1 ENVIRONMENT

These environment variables may be used in place of the specified command line
options.  All command line option will override the corresponding environment
value.

=over 4

=item * C<NEB_SERVER>

Equivalent to --server|-s

=back

=head1 CREDITS

Just me, myself, and I.

=head1 SUPPORT

Please contact the author directly via e-mail.

=head1 AUTHOR

Joshua Hoblitt <jhoblitt@cpan.org>

=head1 COPYRIGHT

Copyright (C) 2007-2008  Joshua Hoblitt.  All rights reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA  02111-1307, USA.

The full text of the license can be found in the L<perlgpl> Pod as supplied
with Perl 5.8.1 and later.

=head1 SEE ALSO

L<Nebulous::Server>, L<neb-initdb>, L<neb-addvol>, L<nebdiskd>, L<neb-df>,
L<neb-touch>, C<regex(7)>

=cut
