#!/usr/bin/perl

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

my $usepaperspecs;

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

# use Paper::Specs units => "cm";
# my $form = Paper::Specs->find( code => "1234");
# $form->sheet_size

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

=head1 NAME

svmgraph - Graph Solaris Volume Manager objects using GraphViz

=head1 SYNOPSIS

    svmgraph [-o outfile.png] [-f format] [-D datadir]  [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 = Solaris::Disk::SVM::Graph->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 $usepaperspecs;
    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:h';
$opts .='P:' if $usepaperspecs;

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

my @objects = @_;

say_usage() if $opt_h;

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

($xsize, $ysize) = Paper::Specs->find(brand => 'standard', code => $opt_P)
  if defined($opt_P);
$xsize = $opt_W if defined $opt_W;
$ysize = $opt_H if defined $opt_H;
my @dimensions=();
@dimensions = ( xsize => $xsize ) if $xsize;
@dimensions = (@dimensions, ysize => $ysize) if $ysize;

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

if (@ARGV) {
    if ( defined $opt_o ) {
        $svmg->output(
            @dimensions,
            @orientation,
            output => $opt_o,
            defined( $opt_f ) ? (format => $opt_f) : (),
        );
    } else {
        foreach (@ARGV) {
            $svmg->output(
                @dimensions,
                @orientation,
                defined( $opt_f ) ? (format => $opt_f) : (),
                objects => @objects,
            );
        }
    }
} else {
    $svmg->output(
        @dimensions,
        @orientation,
        defined( $opt_o ) ? (output => $opt_o) : (),
        defined( $opt_f ) ? (format => $opt_f) : (),
    );
}

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.02 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

