#! /usr/bin/perl -w

use strict;

# Locate a disk in a ses cabinet.

my @DEV;

while (@ARGV && $ARGV[-1] =~ m(^/dev/sd[a-z]+$)) {
	push @DEV, pop @ARGV;
}

unless (@DEV) {
	print STDERR "* Type perldoc $0 for usage info\n";
	exit 1;
}

# Find SES cabinets, find SAS addresses of disks

open LSSCSI, "lsscsi -g -t | ";

my %SAS;
my %DEV;
my %ENC;

while (<LSSCSI>) {
	if (m(enclosu\w*\s+sas:0x([0-9a-f]+)\s*-\s*(/dev/sg\d+))) {
		$ENC {$2} = $1;
	}
	elsif (m(disk\s+sas:0x([0-9a-f]+)\s+(/dev/sd[a-z]+))) {
		$SAS {$2} = $1;
		$DEV {$1} = $2;
	}
	elsif (m(\[(\d+:\d+:\d+:\d+)\]\s+process\s+spi:\d+\s+-\s+(/dev/sg\d+))) {
		$ENC{$2} = $1;
	}
	elsif (m(\[(\d+:\d+:\d+:\d+)\]\s+disk\s+spi:\d+\s+(/dev/sd[a-z]+))) {
		$SAS {$2} = $1;
		$DEV {$1} = $2;
	}
}

close LSSCSI;

my %INDEX;

for my $enc (keys %ENC) {

	# What pages does the device have?

	open SG_SES, "sg_ses 2>/dev/null $enc |";

	my %page;

	while (<SG_SES>) {
		if (m(\[([a-z,]+)\]\s+\[0x[a-f0-9]+\])) {
			for (split /,/, $1) {
				$page{$_} = $_;
			}
		}
	}

	close SG_SES;

	next unless $page{cf};	# check it's an enclosure

	# Lets see if we can locate the disks that interest us.

	my $index;
	my $next_index = 0;
	my $desc;

	if ($page{aes}) {

		open SG_SES, "sg_ses -p aes 2>/dev/null $enc |";

		while (<SG_SES>) {
			if (/Element type: Array device slot/) {
				$index = $next_index ++;
				next;
			}
			elsif (/Element type:/) {
				undef $index;
			}

			next unless defined $index;

			if (/Element (\d+) descriptor/) {
				$desc = $1;
			}
			elsif (/Element index: (\d+)/) {
				$desc = $1;
			}

			if (/SAS address: 0x([0-9a-f]+)/) {
				next unless $DEV{$1};
				$INDEX{$DEV{$1}} = "$index,$desc $enc";
			}
		}

		close SG_SES;
	}
	elsif ($page{es} && $ENC {$enc} =~ /(\d+:\d+):\d+:\d+/) {

		my $bus = $1;

		open SG_SES, "sg_ses -p es 2>/dev/null $enc |";

		# Element type: Device slot, subenclosure id: 0 [ti=0]
		#      Element 0 descriptor:
		#        Slot address: 0

		while (<SG_SES>) {
			if (/Element type: Device slot/) {
				$index = $next_index ++;
				next;
			}
			elsif (/Element type:/) {
				undef $index;
			}

			next unless defined $index;

			if (/Element (\d+) descriptor/) {
				$desc = $1;
			}

			next unless defined $desc;

			if (/Slot address: (\d+)/) {
				my $addr = "$bus:$1:0";
				next unless $DEV{$addr};
				$INDEX{$DEV{$addr}} = "$index,$desc $enc";
			}
		}

		close SG_SES;
	}
}


my $failed = 0;

for (@DEV) {
	unless ($SAS {$_}) {
		print STDERR "* Can't find device $_\n";
		next;
	}

	unless ($INDEX {$_}) {
		print STDERR "* Can't find $_ in any enclosure\n";
		next;
	}

	system join (" ", "sg_ses", @ARGV, -I => $INDEX{$_});

	++$failed if $?; 
}

exit 1 if $failed;

__END__

=head1 NAME

ses_slot - perform actions on SES cabinet slot for disk device

=head1 SYNOPSYS

    ses_slot [ sg_ses options ] /dev/sd...

=head1 DESCRIPTION

ses_slot locates the /dev/sdI<x> devices given as arguments and performs
sg_ses options on the matching slot.  It can be used to, for example, set
and clear the "identify/locate" light on a disk slot.

See sg_ses(8) for details of all the commands available.

=head1 EXAMPLES

    ses_slot --set=locate /dev/sdp /dev/sdz

Sets the locate/identify light on the slots for /dev/sdp and /dev/sdz.

=cut
