#! /usr/bin/perl
#
# This version checks only HM.

$POS=$ARGV[0];
$DCS_ID   = `./modules/getdcsbypos.pl $POS`;     chop $DCS_ID;
$ROC_NAME = `./modules/getinfobypos.pl $POS rocname`; chop $ROC_NAME;

$flag_bad = 0;
$lp=2;

printf("$POS $ROC_NAME $DCS_ID: ");

# Check ping
open(FILE, "ssh $DCS_ID /usr/local/sbin/dcs2trap -link $lp 0 -p 2 2>&1 |");
$slaves_ring0 = 0;
$slaves_ring0 = 0;
while(<FILE>) {
    if( /^(\d+)\sslave.+ring\s0$/ ) {
	$slaves_ring0 = $1;
    }
    if( /^(\d+)\sslave.+ring\s1$/ ) {
	$slaves_ring1 = $1;
    }
}
printf("NSlv=%02d:%02d ", $slaves_ring0 , $slaves_ring1);
close FILE;

open(FILE, "ssh $DCS_ID /usr/local/sbin/dcs2trap -link $lp 0 -i /dcsnfs/SM_Test/rstate_quick.dat 2>&1 |");
$nslaves    = 0;
$nslaves_ec = 0;
while(<FILE>) {
    if( /^0x9\s0xa04\s\S+\s(\d+)\s+.+\s+(\S+)\s*$/ ) {  ## TRAP status
	$slave = $1;
	$state = $2;
	$states{$slave."-".$lp} = $state;
	# printf("$slave $state");
	$nslaves++;
    }
    if( /^0x9\s+0xc04\s+(\S+)\s+(\d+)\s+/ ) { ## Event counter 
	$slave = $2;
	$state = $1;
	$evcs{$slave."-".$lp} = $state;
	# printf("$slave $state");
	$nslaves_ec++;
    }
    if( /^0x9\s+0xb82\s+(\S+)\s+(\d+)\s+/ ) { ## Global counter 
	$slave = $2;
	$state = $1;
	$gcntr{$slave."-".$lp} = hex($state);
	# printf("$slave $state");
	$nslaves_gcntr++;
    }
}

#if( $nslaves != 2 || $nslaves_ec != 2 ) {
#    printf("Error: one of or both HMs did not return data.\n");
#}

# Show Results

printf("HCM-A: %9s, % 8s, % 9s ", $states{"35-2"}, $evcs{"35-2"}, $gcntr{"35-2"} );
printf("HCM-B: %9s, % 8s, % 9s ", $states{ "1-2"}, $evcs{ "1-2"}, $gcntr{ "1-2"} );

printf("\n");
# EOF
