#!/usr/bin/perl
use warnings;
use strict;
use autodie;
use Data::Dump qw(dump);

my ($before, $delimiter, $after) = ( ''=>' | '=> "\n" ); # double space for display
   ($before, $delimiter, $after) = ( '"'=>'","'=> '"'."\n" ) if @ARGV;

my $in_pool;
open(my $zpool, '-|', 'zpool status');
my $name = 'zfs';
my $vdev = '';
while(<$zpool>) {
	chomp;
	$name = $1 if /^\s+pool:\s(\S+)/;
	$vdev = $1 if /^\s+(raid\S+|spare)/;
	$in_pool->{$1} = join(' ', $name, $vdev) if /^\s+(sd\S+)/;
}

warn "# in_pool ",dump($in_pool);

open(my $lsscsi, '-|', 'lsscsi --size -v');

print $before, join($delimiter, qw(id type name dev size zfs path) ), $after;

while(my $line = <$lsscsi>) {
	chomp($line);
#	warn "## $line\n";

	my @l = $line =~ m{(^\S+)\s+(\S+)\s+(.+)\s+(\S+)\s+(\S+)};

	my $dev = $l[3];
#	warn "# dev $dev\n";
	if ( $dev =~ s{/dev/}{} ) {
		push @l, exists $in_pool->{$dev} ? $in_pool->{$dev} : '-';
	} else {
		push @l, $dev;
	}

	my $v = <$lsscsi>;
	chomp($v);
	push @l, $1 if $v =~ m/\[(.+)\]/;
	print $before,join($delimiter,@l),$after;
};
