#!/usr/local/bin/perl -w
# at slac add local to perl path
#
use Env qw(IOC);
use strict;

our %namesHash;

# =========================================================================
sub usage
{
print "dupePVs_all.bash\n\n";
print "Checks \$IOC_DATA/iocInfo/<iocname>/IOC.pvlist and FACET IOC.pvlist files for a specified group of\n";
print "IOCs and reports any duplicate PVs it finds in the group.\n";  
print "Results are reported to the screen and to ./duplicatePVNames.lst.\n\n";

print "Examples:\n";
print "1. check all iocs found in $IOC\n";   
print "   dupePVs.bash\n\n";
print "2. check all eioc's\n";
print "   dupePVs.bash eioc\n\n";
print "3. check all iocs with mg in the name\n";
print "   dupePVs.bash mg\n\n";
print "4. check all soft magnet IOCs\n";
print "   dupePVs.bash sioc-.*-mg01\n\n";

print "Output example with duplicates found:\n";

print "  [softegr\@lcls-builder iocInfo]\$ dupePVs.bash sioc-sys0-al0\n\n";
print "  Logging duplicate PVs to duplicatePVNames.lst\n\n";
print "  CHECKING IOCs with names matching \"sioc-sys0-al0\"\n\n";

print "  checking sioc-sys0-al01\n";
print "  checking sioc-sys0-al02\n";
print "  checking sioc-sys0-al00\n";
print "  SIOC:SYS0:AL01:TOD: sioc-sys0-al01 sioc-sys0-al00\n";
print "  SIOC:SYS0:AL02:TOD: sioc-sys0-al02 sioc-sys0-al00\n";
print "  done!\n";

}


# =========================================================================
sub out($)
{
my $outStr = shift;

print $outStr;
print OUT $outStr;

}

# =========================================================================
sub findDupes($)
{

my $matchStr = shift;

my $outFile = "./duplicatePVNames.lst";
if (!open(OUT, '>'.$outFile)) {
     print "*** $outFile: failed to write \n";
     return;
}

out "\nLogging duplicate PVs to duplicatePVNames.lst\n\n";
out "CHECKING ALL IOCs\n\n";

my $iocBootDir_facet = $ENV{IOC};
my $iocBootDir = $iocBootDir_facet;
$iocBootDir =~ s/facet/lcls/;
my $iocData_facet = $ENV{IOC_DATA};
my $iocData = $iocData_facet;
$iocData =~ s/facet/lcls/;
print "$iocBootDir $iocBootDir_facet $iocData $iocData_facet\n";

# collect the list of IOC boot directories in $IOC
opendir BOOTDIR, $iocBootDir or die "cannot open $iocBootDir directory for some reason: $!\n";
my @dirlist = readdir BOOTDIR;
my @dirParts;
my $PVListFile;

close BOOTDIR;

my $currIOCName;

# for each directory that is an ioc name and matches the argument,
# for each PV name
#   check the namesHash. 
#   if the PV is there already, append the ioc name to its entry
#   if not, add to the namesHash. 
foreach my $dirname (@dirlist)
   {
   @dirParts = split /\//, $dirname;
   $currIOCName = $dirParts[scalar(@dirParts)-1];

   if ((!($currIOCName =~ /screen/)) && 
      ($currIOCName =~/ioc/))
      { 
      if ($currIOCName =~ m/.*$matchStr.*/)
        { 
        my @PVs = ();
        print "checking $currIOCName\n"; 
        $PVListFile = $iocData . "/" . $currIOCName . "/iocInfo/IOC.pvlist";
        if (!open(PVLIST, $PVListFile)) 
           {
           print OUT "$PVListFile: failed to open\n";
           }
        else
           {
           @PVs = <PVLIST>;
           my $PV;
           close PVLIST; 
           foreach $PV (@PVs)
              {
              my ($PVName, $rtype) = split /,/,$PV;
              #print "$PVName\n";
              if (defined $namesHash{$PVName}) 
                 { 
                 # only enter a duplicate IOC once
                 if (!($namesHash{$PVName} =~ /$currIOCName/))
                    {
                    $namesHash{$PVName} .= " $currIOCName";
                    out "$PVName: $namesHash{$PVName}\n"; 
                    }
                 }
              else
                 { $namesHash{$PVName} = $currIOCName; }

              }
           }
        } 
      }
}

# NOW ADD FACET!
# collect the list of IOC boot directories in $IOC
opendir BOOTDIR, $iocBootDir_facet or die "cannot open $iocBootDir_facet directory for some reason: $!\n";
@dirlist = readdir BOOTDIR;
@dirParts = ();

close BOOTDIR;

# for each directory that is an ioc name and matches the argument,
# for each PV name
#   check the namesHash. 
#   if the PV is there already, append the ioc name to its entry
#   if not, add to the namesHash. 
foreach my $dirname (@dirlist)
   {
   @dirParts = split /\//, $dirname;
   $currIOCName = $dirParts[scalar(@dirParts)-1];

   if ((!($currIOCName =~ /screen/)) && 
      ($currIOCName =~/ioc/))
      { 
      if ($currIOCName =~ m/.*$matchStr.*/)
        { 
        my @PVs = ();
        print "checking $currIOCName\n"; 
        $PVListFile = $iocData_facet . "/" . $currIOCName . "/iocInfo/IOC.pvlist";
        if (!open(PVLIST, $PVListFile)) 
           {
           print OUT "$PVListFile: failed to open\n";
           }
        else
           {
           @PVs = <PVLIST>;
           my $PV;
           close PVLIST; 
           foreach $PV (@PVs)
              {
              my ($PVName, $rtype) = split /,/,$PV;
              #print "$PVName\n";
              if (defined $namesHash{$PVName}) 
                 { 
                 if (!($namesHash{$PVName} =~ /$currIOCName/))
                    {
                    $namesHash{$PVName} .= " $currIOCName";
                    out "$PVName: $namesHash{$PVName}\n"; 
                    }
                 }
              else
                 { $namesHash{$PVName} = $currIOCName; }

              }
           }
        } 
      }
}
out "done!\n";
}

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

# code start

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

my $iocNameSpec;

if ($#ARGV < 0) 
   { $iocNameSpec = "ioc"; }
elsif (($ARGV[0] eq "-h") || ($ARGV[0] eq "-H")) 
   { usage;  exit; }
else 
   { $iocNameSpec = $ARGV[0]; }


findDupes($iocNameSpec);

