#!/usr/bin/perl
#
# css_ccw_driver_association
#   Health check program for the Linux Health Checker
#
# Copyright IBM Corp. 2012
#
# Author(s): Nageswara R Sastry <nasastry@in.ibm.com>
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Eclipse Public License v1.0
# which accompanies this distribution, and is available at
# http://www.eclipse.org/legal/epl-v10.html
#
# Contributors: See file CONTRIBUTORS which is part of this package
#
#

use strict;
use warnings;

use lib $ENV{"LNXHC_LIBDIR"};
use LNXHC::Check::Base;


#
# Global variables
#

# Exception IDs
my $LNXHC_EXCEPTION_NO_DRIVER = "no_driver";

# Path to the file containing data for sysinfo item 'symlink_to_driver'.
my $sysinfo_symlink_to_driver = $ENV{"LNXHC_SYSINFO_symlink_to_driver"};


#
# Code entry
#

local *SI_FILE;
my %device_status;

open(SI_FILE, "<", $sysinfo_symlink_to_driver) or
	die("css_ccw_without_driver: Could not read file '$sysinfo_symlink_to_driver': ".
	    "$!\n");
foreach my $line ( <SI_FILE> ) {
	chomp($line);
	my ($device, $devtype, $cutype) = split /:/, $line;
	push @{ $device_status{$device} }, $devtype, $cutype;
}

close(SI_FILE);

if (%device_status) {
	my @summary_device_status = sort(keys(%device_status));
	lnxhc_exception($LNXHC_EXCEPTION_NO_DRIVER);

	if (scalar(@summary_device_status) > 4) {
		@summary_device_status = (@summary_device_status[0..3], "...");
	}

	lnxhc_exception_var("devices_list", join(", ", @summary_device_status));

	lnxhc_exception_var("all_devices",
	  sprintf("#%-10s   %7s   %7s", "BUS ID", "DevType", "CU Type"));

	foreach my $device (sort keys(%device_status)) {
		my ($devtype, $cutype) = @{ $device_status{$device} };
		lnxhc_exception_var("all_devices",
		  sprintf("#%-10s   %7s   %7s", $device, $devtype, $cutype));
	}
}

exit(0);
