#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/cspoc/utilities/cl_find_commonvpathids.sh 1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002 
# 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 
# @(#)31        1.2 src/43haes/usr/sbin/cluster/cspoc/utilities/cl_find_commonvpathids.sh, hacmp.cspoc, 61haes_r714 11/19/02 15:41:49
# \$Id\$

#
#	Component:	hacmp.cspoc
#
#	Function:	Parse the file /tmp/cllsvpathids.out (or one
#			specified on the command line) and outputs a list
#			of Vpath PVIDs
#
#	Origins:	27
#
#	Arguments:	<list of nodes> [<file with pvids listed by nodes>]
#
#	Ouput:		List of Vpath PVIDs
#
#	Usage:		cl_find_commonvpathids <list of nodes>
#				[-o <file with pvids listed by nodes>]
#
#############################################################################


## main is here 

my ($dirname, $getpath_cmd, $path, $HA_DIR, $odmdir, $cl_datfile, $yy, @temp_nodelist);
my ($output_file_specified, @common_vpathids);

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

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

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

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

$output_file_specified = 0;

# determine if output file is specified.
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_vpathids = find_common_vpathids(@temp_nodelist) ;

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

exit 0;


sub find_common_vpathids {

    local(@local_nodes)  = @_;
    my ($line, $vpathid, @avail_vpathids, $nodes);

    $common_vpathid_list  = "" ;
    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) {
            $vpathid = (split(/:/, $line))[2];
            $node = (split(/:/, $line))[0];
            next if ($vpathid =~ /none/i) ;
            next if ($line =~ /FREE/) ;
            $NUPVID{$vpathid} .= $node . ":"  ;
        }
    }

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

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

