#! /usr/bin/env perl
#
#   SegEpics: Get segment data from Epics.
#
#   This script was written to give results identical to those of 
#   the SegData program.
#
#---------------------------------------  Initialize perl
$Debug = 0;
$|=1;
sub get_ichan;

#---------------------------------------  Set site dependencies
$Ifo = $ARGV[0];
if ($Ifo eq "L1") {
    $CAREAD    = "/opt/LLO/a/gds/bin/ezcaread";
    $ENV{EPICS_CA_ADDR_LIST} = "10\.100\.255\.255";
} elsif (($Ifo eq "H1") || ($Ifo eq "H2")) {
    $CAREAD    = "/opt/CDS/a/gds/bin/ezcaread";
    $ENV{EPICS_CA_ADDR_LIST} = "10\.1\.255\.255";
} else {
    print "No config information for Ifo: $Ifo\n";
    exit;
}
$ENV{EPICS_CA_AUTO_ADDR_LIST} = "NO";

#---------------------------------------  Set up channel names
$SEGCHAN   = "$Ifo:IFO-SV_SEGNUM";
$RUNTIME   = "$Ifo:IFO-SV_RUNTIME_SEC";
$STARTCHAN = "$Ifo:IFO-SV_START_GPS";
$STOPCHAN  = "$Ifo:IFO-SV_STOP_GPS";

#---------------------------------------  Print segment data
print("Segment Number: ", get_ichan($SEGCHAN),   "\n");
print("Run Time:       ", get_ichan($RUNTIME),   "\n");
print("Start Time:     ", get_ichan($STARTCHAN), "\n");
print("Stop Time:      ", get_ichan($STOPCHAN),  "\n");
exit 1; 

#---------------------------------------  Get a specified channel from Epics.
sub get_ichan {
    my $chan=$_[0];
    my $line=`$CAREAD -i $chan`;
    chomp $line;
    print "Channel: $chan  Response: $line\n" if ($Debug);
    return 0 if (! ($line =~ s/^.* = //));
    $line =~ s/^0x//;
    return 0 if (! ($line =~ s/ .*$// ));
    return $line;
}
