#!/usr/bin/env perl

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

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

use DBI;
use URI::file;
use SQL::Interp qw( sql_interp );
use URI;

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

my ($dbname, $dbhost, $dbpass, $dbuser, $debug);

# $dbname = $ENV{'NEB_DB'} unless $db;
# $dbhost = $ENV{'NEB_DBHOST'} || 'localhost';
# $dbuser = $ENV{'NEB_USER'} unless $dbuser;
# $dbpass = $ENV{'NEB_PASS'} unless $dbpass;

# XXX this program (and neb-df) should not have a hard-wired nebulous config location
# XXX can this be part of the build?

# my $conffile = "/data/ippc64.1/ippitc/ippconfig/nebulous.config";
# my $conffile = "/home/panstarrs/ipp/ippconfig/nebulous.config";
my $conffile = "/home/panstarrs/ippps1/ippconfig/nebulous.config";

if (-e $conffile) {
    open(IN,$conffile);
    while(<IN>) {
	chomp;
	if ($_ =~ /NEB_DB\s+/) {
	    $dbname = (split /\s+/,$_)[2];
	}
	elsif ($_ =~ /NEB_DBHOST/) {
	    $dbhost = (split /\s+/,$_)[2];
	}
	elsif ($_ =~ /NEB_USER/) {
	    $dbuser = (split /\s+/,$_)[2];
	}
	elsif ($_ =~ /NEB_PASS/) {
	    $dbpass = (split /\s+/,$_)[2];
	}
    }
    close(IN);
}

GetOptions(
    'dbname=s'       => \$dbname,
    'dbhost=s'       => \$dbhost,
    'dbuser=s'       => \$dbuser,
    'dbpass=s'       => \$dbpass,
    ) || pod2usage( 2 );

pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
pod2usage( -msg => "Cannot configure nebulous database", -exitval => 2) unless
    defined $dbname && defined $dbhost && defined $dbuser && defined $dbpass;

my $dbh = DBI->connect(
    "DBI:mysql:database=$dbname:host=$dbhost", $dbuser, $dbpass,
    {
        RaiseError => 1,
        PrintError => 0,
        AutoCommit => 0,
    },
    );

my $sql_cabinets = "SELECT cab_id, name, site_id, location from cabinet";
warn "$sql_cabinets\n" if $debug;

my $rowref = $dbh->selectall_arrayref( $sql_cabinets );
my @rowarr = @$rowref;

foreach my $valref (@rowarr) {
    my @values = @$valref;

    # there should be exactly 4 values
    my $Nvalues = @values;
    if ($Nvalues != 4) { print "ERROR: invalid response?\n"; next; }
    
#   printf ("%s %s %s %s", $values[0], $values[1], $values[2], $values[3]);
    printf ("%s %s %s %s", @values);
}

__END__

=pod

=head1 NAME

neb-cabinet - show / manage the state of Nebulous storage cabinets

=head1 SYNOPSIS

    neb-cabinet
    [--dbhost <database host>] [--db <database name>]
    [--dbuser <database user>] [--dbpass <database password>]

=head1 DESCRIPTION

This program displays a Nebulous server's list of cabinets.  

=head1 OPTIONS

=item * --dbname <database name>

Name of database (C<namespace>) to create tables in.

Optional if the appropriate environment variable is set.

=item * --dbhost <database host>

Host name where the database resides.

Defaults to C<localhost>.

=item * --dbuser <database username>

Username to authenticate with.

Optional if the appropriate environment variable is set.

=item * --dbpass <database password>

Password to authenticate with.

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_DB>

Equivalent to --db|-d

=item * C<NEB_HOST>

Equivalent to --host

=item * C<NEB_USER>

Equivalent to --user|-u

=item * C<NEB_PASS>

Equivalent to --pass|-p 

=back

=head1 COPYRIGHT

Copyright (C) 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-voladm>, L<neb-voladd>

=cut
