#!/usr/bin/perl
#  ALTRAN_PROLOG_BEGIN_TAG                                                    
#  This is an automatically generated prolog.                                  
#                                                                              
#  Copyright (C) Altran ACT S.A.S. 2019,2020,2021.  All rights reserved.  
#                                                                              
#  ALTRAN_PROLOG_END_TAG                                                      
#                                                                              
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/cspoc/utilities/cl_find_commonpvids.sh 1.5 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,2000 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)  7d4c34b 43haes/usr/sbin/cluster/cspoc/utilities/cl_find_commonpvids.sh, 726, 2147A_aha726, Feb 05 2021 09:50 PM 
#Adds a directory to perls library path
use lib '/usr/es/lib/perl/';
use libcl_untaint;

## main is here 

# Set the ENV PATH variable
$0 = untaint($0);
$dirname = untaint(`/usr/bin/dirname/ $0`);
chop($dirname);
$getpath_cmd = "$dirname/../utilities/cl_get_path all";

# Set the ENV ODMDIR variable
$HA_DIR = `cl_get_path`;
$odmdir = "/etc/$HA_DIR/objrepos";
$ENV{'ODMDIR'} = "$odmdir";

$cl_datfile = "/tmp/cllspvids.out" ;
@temp_nodelist =  @ARGV ;

# if an output file is specified, use it - otherwise use the $cl_datfile.

$output_file_specified = 0;

foreach $yy (@temp_nodelist) {
        if ($yy =~ /-o/i) {
                $output_file_specified = 1;
                break ;
        }
        if($output_file_specified eq 0) {
            push(@new_nodelist, $yy);
        }
}
# if an output file was specified by the caller using -o <filename>,
# then use it

if($output_file_specified eq 1) {
        $cl_datfile = $ARGV[$#ARGV] ;
}
@temp_nodelist = @new_nodelist ;


 @common_pvids = find_common_pvids(@temp_nodelist) ;

 foreach $tt(@common_pvids) {
	print "$tt\n" ;
 }


sub find_common_pvids {

    local(@local_nodes)  = @_;
    my ($line, $pvid, @avail_pvids, $nodes);

    $common_pvid_list  = "" ;
    my $pvid ;
    my $lnode ;

    $nlist = join("|", @local_nodes);
    $nlist = "(" . $nlist . ")";
    $nlist = "$nlist.*" x ($#local_nodes+1);

    if (!(keys %NUPVID)) {
        open(DAT, "< $cl_datfile");
        @flatfile = <DAT>;
        close(DAT);
        foreach $line (@flatfile) {
            $pvid = (split(/:/, $line))[2];
            $node = (split(/:/, $line))[0];
            next if ($pvid =~ /none/i) ;
            next if ($line =~ /FREE/) ;
#            $NUPVID{$pvid} .= $node . ":" if ($line =~ /:None/);
            $NUPVID{$pvid} .= $node . ":"  ;
        }
    }

    foreach $pv (keys %NUPVID) {
        $common_pvid_list .= $pv . "," if ($NUPVID{$pv} =~ /$nlist/);
    }
    chop($common_pvid_list);

    chomp($common_pvid_list);
  return split(/,/, $common_pvid_list);
}
