#!/usr/bin/perl

# off: Power off wire pairs by writing to proc file
# jacobsen@npxdesigns.com

package MY_PACKAGE;
use strict;
sub getint;
my $procdir = "/proc/driver/domhub";

sub usage { return <<EOF;
Usage: $0 all
       $0 <card0> <pair0> <card1> <pair1> ...
EOF
;
}

sub printwarn { 
    print <<EOF;
WARNING: DOMs are not completely powered off; you must turn 
the power supply off before disconnecting or connecting cables
or touching exposed hardware.
EOF
;
}

my @cards;
my @pairs;
my $doall = 0;

while(1) {
    my $card = shift;
    if($card eq "all") { $doall++; last; }
    last unless defined $card;
    push @cards, $card;
    my $pair = shift;
    die usage unless defined $pair;
    push @pairs, $pair;
}
die usage unless (scalar @cards)==(scalar @pairs);
die usage unless $doall || (scalar @cards>0);

if(! $doall) {
    for(0..(@cards)-1) {
    }
}

if($doall) {
    my $pf = "$procdir/pwrall";
    open PF, ">$pf" || die "Can't open $pf: $!\n";
    print PF "off";
    close PF;
    printwarn;
    exit;
} 

for(0..(scalar @cards)-1) {
    my $icard = $cards[$_];
    my $ipair = $pairs[$_];
    print "Powering off card $icard, pair $ipair.\n";
    my $pf = "$procdir/card".$icard."/pair".$ipair."/pwr";
    open PF, ">$pf";
    print PF "off";
    close PF;
}

printwarn;
exit;

sub getint {
    my $s = shift; 
    while(1) {
	print $s;
	my $resp = <STDIN>;
	if($resp =~ /^(\d+)$/) {
	    return $1;
	}
    }
}


__END__

