#!/usr/bin/perl -w

# take a list of PVs, and return a list of the PVs plus the corresponding IOCs
# the list format assumes:
#  assumes that (after any leading blanks) the first string on a line is a PV name
# the script:
#  ignores blank lines
#  ignores lines beginning with "#"
#  strips off fields (i.e. what follows "." in a name)
# outputs to STDOUT

use strict;
my @filelist;

my $fix = 0;
my $chan_or_group;

sub usage()
{
        print "====================================================================================\n";
	print "Usage:\n";
        print "  PVIOCList.pl filespec\n";
        print "\nfilespec can be one file or a bunch of files:\n";
        print "  PVIOCList.pl pvlist.txt\n";
        print "  PVIOCList.pl *.cwConfig\n";
        print "\n";
        print "Takes a file (or files) with a list of PV names and uses cainfo to\n";
        print "get the IOC for each PV.  Files are typically Channel Watcher files...or similar.\n";
        print "\nOutput is to STDOUT\n";
        print "\nInput file format: something like Channel Watcher config files where:\n";
        print "   the first string on a line is a PV name\n";
        print "   there may be other stuff on the line, which is ignored\n";
        print "\nThe script:\n";
        print "  strips off field names (stuff following \"\.\" in the name)\n";
        print "  strips off leading blanks from each line\n";
        print "  ignores blank lines\n";
        print "  ignores lines beginning with #\n";
        print "\nPlease source epicsReset.bash, or otherwise set EPICS_CA* parameters before\n";
        print "running PVIOCList.pl\n";
        print "====================================================================================\n";
	exit;
}


sub getIOCs($)
{
	my $fil = shift;
	
	if (!open(INP, $fil)) {
		print "*** $fil: failed to read \n";
		return;
	}
        print "FILE: $fil\n";
	my @lines = <INP>;
	close INP;
        my $count = 0;
	foreach my $line (@lines) {
           # first, remove leading whitespace
           $line =~ s/^\s+//;
           if (length($line) == 0)
              {  next;   }
          
           # ignore lines starting with #
           if ($line =~ /^\#.*/)
              {  next;   }

           # split off PV name, the first element of the line
           my @fields = split/\s+/, $line;
           my $PV = $fields[0]; 
 
           my @PVNameFields = split/\./, $PV;
           my $PVName = $PVNameFields[0]; 
           #print "pv name is $PVName\n";
           
           # now get the IOC name using cainfo
           open (CAINFO, "cainfo $PVName |");
           while (<CAINFO>)
              {
              my $cainfoLine = $_;
              $cainfoLine =~ s/^\s+//;
              if ($cainfoLine =~ /Host:/)
                 {
                 my ($label, $host) = split/:/, $cainfoLine;
                 $host =~ s/^\s+//;

                 my $paddedPV = pack("A40", $PVName);
                 print "$paddedPV $host\n";

                 }
              }
           close CAINFO;

	}
        print "===========================================================================\n\n";
	return;
}


##################################################

# code start

##################################################

my $pwd = `pwd`;
chomp $pwd;

if ($pwd !~ /\/$/) {
	$pwd .= '/';
}

my $sdir = $pwd;

if ($#ARGV < 0) { usage(); }

if ($#ARGV < 0) {
	die "*** need file name(s) or wild-card pattern ***\n";
}
@filelist = @ARGV;

print "\n";

my $lookcount = 0;
foreach my $infil (@filelist) {
#print "file $infil\n";
	getIOCs($infil);
}

