#!/usr/bin/perl

use strict;
use warnings;
use Getopt::Std;
use Solaris::Disk::SVM;
use Solaris::Disk::SVM::Graph;

my $has_paperspecs;
our $VERSION = '0.03';

BEGIN {
    $has_paperspecs=0;
    eval 'require Paper::Specs';
    unless ($@) {
        $has_paperspecs=1;
        import Paper::Specs units => 'in', brand => 'standard';
    }
}

use vars qw( $opt_D $opt_h $opt_f $opt_o $opt_P $opt_O $opt_W $opt_H $opt_v);

=head1 NAME

svmgraph - Graph Solaris Volume Manager objects using GraphViz

=head1 SYNOPSIS

    svmgraph [-o outfile.png] [-f format] [-D datadir] [-O [h|v]] [objects] 

    svmgraph -D t/data          # creates svm.png
    smvgraph -o d100.png d100   # creates d100.png

=head2 Options

  [-o <outfile>]  : outfile is the name of the image file to output, extension are honoured
  [-f <format>]   : format of the file, as supported by GraphViz
  [-D <datadir>]  : datadir is the directory where the data will be read from
  [-O <h|v>]      : image orientation (horizontal or vertical)
  [-P pageformat] : if Paper::Specs is available, you'll be able to say 'Letter' or 'A4'
  [-h]            : show this help text

=head2 Commands

=head1 DESCRIPTION

F<svmgraph> is a tool to graph your SDS/SVM configuration

The C<-D> option provides the same interface as L<svm(1)> (from
L<Solaris::Disk::SVM>), to read configuration from file instead from the
needed commands outputs.

The output file may be from one of the many output types in GraphViz.

=cut

sub say_usage {
    my $version = $VERSION;
    my $svmv = Solaris::Disk::SVM->version;
    print STDERR "svmgraph by Jrme Fenal\n";
    print STDERR "Version: $version, using Solaris::Disk::SVM $svmv\n";
    print STDERR "\n\t$0  [-D datadir] [ -o out.png ] [ objects ]\n\n";
    my $pspec = "  [-P paper]      : use paper (as A4 or Letter) size\n"
      if $has_paperspecs;
    print STDERR << "EOT";
Options:
  [-o outputfile] : image file name, with optional extension (.png by default)
  [-f format]     : image file format, as accepted by GraphViz (png by default)
                    ".format" is appended to image file name.
  [-D <datadir>]  : datadir is the directory where the data will be read from
  [-O <[h|l|v|p]>]: image orientation (horizontal/landscape, the default,
                    or vertical/portrait)
$pspec  [-h]            : show this help text
EOT

    exit 0;
}

# Main
my $opts='o:D:f:O:W:H:hv';
$opts .='P:' if $has_paperspecs;

getopts($opts); # Sets opt_* as a side effect.

if ($opt_v) {
    use Solaris::Disk::SVM;
    print "This is svmgraph $VERSION using Solaris::Disk::SVM $Solaris::Disk::SVM::VERSION\n";
    exit 0;
}

say_usage() if $opt_h;

my @orientation = ();
@orientation = ( orientation => 1 ) if defined $opt_O && $opt_O =~ m/[vp].*/;

my ( $width, $height );
( $width, $height ) = Paper::Specs->find( brand => 'standard', code => $opt_P )
  if defined($opt_P);
$width  = $opt_W if defined $opt_W;
$height = $opt_H if defined $opt_H;

my @dimensions = ();
@dimensions = ( width => $width ) if $width;
@dimensions = ( @dimensions, height => $height ) if $height;

my $svmg =
  Solaris::Disk::SVM::Graph->new(
    defined($opt_D) ? ( sourcedir => $opt_D ) : (),
  );

die "Cannot read configuration"
  if not defined $svmg;

my @objects=();
@objects = (objects => @ARGV)
  if @ARGV;

$svmg->output(
    @dimensions, @orientation,
    defined($opt_o) ? ( output => $opt_o ) : (),
    defined($opt_f) ? ( format => $opt_f ) : (),
    @objects
);

exit 0;

__END__

=head1 AUTHOR

Jrme Fenal <jfenal@free.fr>

=head1 WEBSITE

Head to L<http://jfenal.free.fr/Solaris/> to see some sample graphics.

=head1 VERSION

This is version 0.03 of the F<svmgraph> script.


=head1 COPYRIGHT

Copyright (C) 2004, 2005 Jrme Fenal. All Rights Reserved

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

=head1 SEE ALSO

=over

=item *
L<Solaris::Disk::SVM>

is used to retrieve information about the SVM configuration.

=item *
L<Solaris::Disk::VTOC>

is used to get raw disk partitions.

=item *
L<Solaris::Disk::Mnttab>

is used to get current mounted devices.

=item *
SDS / SVM manual pages

metastat(1M), metatool(1M), etc.

=back

