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

#

use Getopt::Long;

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

=head1   mkrtnodes

Make node definitions for RR nodes

 mkrtnodes -C <cu letter> -L < start Rack number for CU> -R <startrange,endrange>     (add) 
 mkrtnodes -d -C <cu letter> -R <startrange,endrange> (delete)

example command:
   mkrtnodes -C d -L 2 -R 1,180   ( 6 blades per rack)

 Builds some nodelist entries that look like this
   "rtd171a","rtd171,ls21,cud,opteron,opteron-cud,compute,tb,all,rack16,bc57",,,
   "rtd171b","rtd171,qs22,cud,cell,cell-b,cell-cud-b,cell-cud,compute,all,tb,rack16,bc57",,,
   "rtd171c","rtd171,qs22,cud,cell,cell-c,cell-cud-c,cell-cud,compute,all,tb,rack16,bc57",,,

=cut

#-----------------------------------------------------------------------------
# Main

my $rc = 0;

&parse_args;
my $cmd = "";
my @bladename;
@bladename = ("a", "b", "c");
foreach my $CU (@::CU)
{
    foreach my $range (@::RANGE)
    {
        foreach my $blade (@bladename)
        {
            my $nodeno = "";
            if ($range <=9) {  # want rt00X
               $nodeno .="0";
            } 
            if ($range <=99) {  # want rt0XX
               $nodeno .="0";
            } 
            $nodeno .= $range;

            $rack = "rack";
            $bc   = "bc";
            $cmd  = "rt";
            $cmd .= $CU;
            $cmd .= $nodeno;
            $cmd .= "$blade";
            $cmd .= " ";
            if ($blade eq "a")
            {

                $cmd .=
                  "groups=rt$CU$nodeno,ls21,cu$CU,opteron,opteron-cu$CU,compute,tb,all";
            }
            else
            {
                if ($blade eq "b")
                {
                    $cmd .=
                      "groups=rt$CU$nodeno,qs22,cu$CU,cell,cell-b,cell-cu$CU-b,cell-cu$CU,compute,all,tb";
                }
                else
                {    # c
                    $cmd .=
                      "groups=rt$CU$nodeno,qs22,cu$CU,cell,cell-c,cell-cu$CU-c,cell-cu$CU,compute,all,tb";

                }
            }

            # calculate the rack number ( 6 triblades/rack)
            my $count = ($range -1) / 6;
            my ($rackno, $rem) = split '\.', $count;
           
            $rackno = $rackno + $::LOCATION;
            if ($rackno <=9) {  # want rack0X
               $rack .="0";
            } 
            $rack .= $rackno;

            $cmd .= ",";
            $cmd .= $rack;

            # calculate the BC number ( 3 triblades/BladeCenter)
            #  BC number = (triblade# / 3) 
            my $count = (($range-1) / 3) + 1 ;
            my ($bcno, $rem) = split '\.', $count;
           
            if ($bcno <=9) {  # want bc0X
               $bc .="0";
            } 
            $bc .= $bcno;

            $cmd .= ",";
            $cmd .= $bc;


            if ($::DELETE)
            {
                if ($::TEST) {
                   print ("noderm $cmd \n");
                } else {
                   system("noderm $cmd");
                }
            }
            else
            {
                if ($::TEST) {
                   print ("nodeadd $cmd \n");
                } else {
                   system("nodeadd $cmd");
                }
            }

        }
    }
}
exit $rc;

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

=head3 parse_args
  
  Parses for  input

=cut

#-----------------------------------------------------------------------------
sub parse_args
{

    Getopt::Long::Configure("posix_default");
    Getopt::Long::Configure("no_gnu_compat");
    Getopt::Long::Configure("bundling");
    my $usagemsg =
          " mkrtnodes -h \n mkrtnodes [-d] -C [a|b|,...,r] -L [start rack number for CU] -R [startrange,endrange] [-t|--test]\n";
    if (
        !GetOptions(
            'C|CU=s'    => \$::CU,
            'L|LOC=s'   => \$::LOCATION,
            'h|help'    => \$::HELP,
            'd|delete'  => \$::DELETE,
            'R|RANGE=s' => \$::RANGE,
            't|test'    => \$::TEST

        )
      )
    {
        printf $usagemsg;
        exit 1;
    }
    if ($::HELP)
    {
        printf $usagemsg;
        exit 0;
    }
    @::CU = split(',', $::CU);

    my ($fNum, $eNum) = split(',', $::RANGE);
    my $prefix;
    foreach my $suffix ($fNum .. $eNum)
    {
        my $numOfZeros = (length($fNum) - length($suffix));
        my $prefix     = '0' x $numOfZeros;
        push @::RANGE, "$fRoot$prefix$suffix$fDomain";
    }

}