#!/usr/bin/perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#(C)IBM Corp
#

BEGIN
{
    $::XCATROOT =
        $ENV{'XCATROOT'} ? $ENV{'XCATROOT'}
      : -d '/opt/xcat'   ? '/opt/xcat'
      : '/usr';
}
use lib "$::XCATROOT/lib/perl";
use Getopt::Long;
use xCAT::MsgUtils;
use xCAT::Utils;

#-----------------------------------------------------------------------------

=head1  groupfiles4dsh 


 This tool will build a directory of files for each defined 
 nodegroup in xCAT.  The file will be named the nodegroup name and 
 contain a list of nodes that belong to the nodegroup. 
 The file can be used as input to the AIX dsh command run in the DSH 
 context.
 The purpose of this tool is to allow backward compatiblity with scripts 
 that were created using the AIX or CSM dsh command.


 groupfiles4dsh  -p <directory to place generated nodegroup files> 
 example: groupfiles4dsh -p /tmp/dshnodegrpfiles

  Use:   export DSH_CONTEXT=DSH  ( default unless CSM is installed)
		 export DSH_NODE_RSH=/bin/ssh   (default is rsh)
		 export DSH_NODEGROUP_PATH= /tmp/dshnodegrpfiles
         
		 dsh  -N all  date  ( all is a group in xCAT)
		 dsh -a date   ( see man dsh,  DSH context)

=cut

#-----------------------------------------------------------------------------
# Main
my $rc = 0;
&parse_args;
my $cmd = "lsdef -t group";
my @grpoutput = xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{    # error
    xCAT::MsgUtils->message("E", "Error running $cmd");
    exit 1;
}
foreach my $group (@grpoutput)
{
    my $cmd = "nodels $group > $::PATH/$group";
    xCAT::Utils->runcmd($cmd, 0);
    if ($::RUNCMD_RC != 0)
    {    # error
        xCAT::MsgUtils->message("E", "Error running $cmd\n");
        exit 1;
    }

}
exit $rc;

#-----------------------------------------------------------------------------

=head3 parse_args
  
  Parses for  input

=cut

#-----------------------------------------------------------------------------
sub parse_args
{
    my $msg;
    my $usagemsg;
    $usagemsg =
      "groupfiles4dsh -h \ngroupfiles4dsh -v \ngroupfiles4dsh [-p] [path to group files directory]";
    Getopt::Long::Configure("posix_default");
    Getopt::Long::Configure("no_gnu_compat");
    Getopt::Long::Configure("bundling");
    if (
        !GetOptions(
            'p|path=s'  => \$::PATH,
            'h|help'    => \$::HELP,
            'v|version' => \$::VERSION

        )
      )
    {
        xCAT::MsgUtils->message("E", $usagemsg);
        exit 1;
    }
    if ($::HELP)
    {
        xCAT::MsgUtils->message("I", $usagemsg);
        exit 0;
    }
    if ($::VERSION)
    {
        my $version = xCAT::Utils->Version();
        xCAT::MsgUtils->message("I", $version);
        exit 0;
    }
    if (!($::PATH))
    {
        my $msg =
          "Requires -p with path to existing directory to hold group files.";
        xCAT::MsgUtils->message("E", $msg);
        exit 1;
    }
    if (!(-d $::PATH))
    {
        my $msg = " Input directory must exist.";
        xCAT::MsgUtils->message("E", $msg);
        exit 1;
    }

}