#!/usr/bin/perl
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------
# Copyright (c) 1999 - 2000, Intel Corporation 
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, 
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation 
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of Intel Corporation nor the names of its contributors 
#    may be used to endorse or promote products derived from this software 
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
# EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
#
#--------------------------------------------------------------------------------
#--------------------------------------------------------------------------------

#   /***************************************************************************
#   **                                                                        **
#   ** INTEL CORPORATION                                                      **
#   **                                                                        **
#   ** This software is supplied under the terms of the license included      **
#   ** above.  All use of this software must be in accordance with the terms  **
#   ** of that license.                                                       **
#   **                                                                        **
#   **  Abstract:                                                             **
#   **    Configuration script for ANS -step 2/3- ANS configurtaion (SuSE)    **
#   **                                                                        **
#   ***************************************************************************/
use general;

print "\n\n-- ANS configuration\n\n";
print "The configuration process will now ask you to enter your\n";
print "selection for the ANS configuration. Please consult the README\n";
print "file for more information.\n\n";

@adapters = `ls $PROCDIR`;
$i=0;
while ($i < @adapters)
{
    $_ = $adapters[$i];
    chomp $_;
    splice(@adapters, $i, 1) unless (-d "$PROCDIR/$_");
    $i++ if -d "$PROCDIR/$_";
}

while (@adapters)
{
	 do
	 {
		  print "\nEnter team name, <Enter> to end: ";
		  $team = <STDIN>;
		  chomp $team;
		  exit 0 if (!$team);
	 }
	 while (system("$APP add_team team=$team"));
	 
	 splice(@teamadapters, 0);
	 
	 while (@adapters)
	 {
		  $i=1;
		  print "\nSelect members for team $team:\n";
		  print "0. done\n";
		  foreach $_ (@adapters)
		  {
				chomp($_);
				print "$i. $_\n";
				$i++;
		  }
		  
		  $res = <STDIN>;
		  chomp $res;
		  
		  if ($res eq "0" && @teamadapters==0)
		  {
				print "Must select at least one member in each team\n";
				$res = "";
		  }
		  goto vadapter if ($res eq "0");
		  
		  $res = "err" if $res < 1 || $res > @adapters || system("$APP add_member team=$team member=$adapters[$res-1]");
		  if ($res > 0)
		  {
				$pri = 0;
				while ($pri  < 1 || $pri > @PRIORITIES)
				{
					 print "\nSelect member priority for member $adapters[$res-1] ins team $team:\n";
					 $i=1;
					 foreach $_ (@PRIORITIES)
					 {
						  chomp($_);
						  print "$i. $_\n";
						  $i++;
					 }
					 $pri = <STDIN>;
					 chomp $pri;
				}
				system("$APP member_role member=$adapters[$res-1] priority=$PRIORITIES[$pri-1]");
				push (@teamadapters, $adapters[$res - 1]);
				splice (@adapters, $res-1, 1);
		  }
	 }
vadapter:
	 $vadapter = "";
	 
	 while (!$vadapter)
	 {
		  print "\nEnter virtual adapter name for team $team: ";
		  $vadapter = <STDIN>;
		  chomp ($vadapter);
		  $vadapter = "" if (!($vadapter eq "") && system("$APP add_vadapter team=$team vadapter=$vadapter"));
	 }
	 config_vadapter($vadapter);
	 
	 $mode = 0;
	 while ($mode < 1 || $mode > @MODES)
	 {
		  $i=1;
		  print "\nSelect teaming mode for team $team:\n";
		  foreach $_ (@MODES)
		  {
				chomp($_);
				print "$i. $_\n";
				$i++;
		  }
		  $mode = <STDIN>;
		  chomp $mode;
	 }
	 system ("$APP team_mode team=$team teaming_mode=$MODES[$mode-1]");
	 
	 print "\nTeam name: \t\t$team\n";
	 print "Teaming mode: \t\t@MODES[$mode - 1]\n";
	 print "Chosen members: \t";
	 print join(" ", @teamadapters);
	 print "\nVadapter name: \t\t$vadapter\n";
	 $res = "";
	 
	 while (!($res=~"^(y|Y|n|N).*"))
	 {
		  print "Are you sure (y/n)? ";
		  $res = <STDIN>;
	 }
	 
	 $res = "n" if ($res=~"^(y|Y).*" && system("$APP commit team=$team"));
	 
	 if ($res=~"^(n|N).*")
	 {
		  system("$APP rm_team team=$team");
		  push(@adapters, @teamadapters);
		  @adapters = sort @adapters;
	 }
	 else
	 {
		  open (OUT, ">/etc/rc.config");
		  print OUT @vadapter;
		  close (OUT);
		  foreach (@teamadapters)
		  {
		      setonboot($_, "no");
		  }
	 }
	 print "\n\n";
}

sub config_vadapter
{
	 my($devname) = @_;
	 
	 splice(@vadapter, 0);
	 $netconfig = `. /etc/rc.config; echo \$NETCONFIG`;
	 chomp $netconfig;
	 $index="";
	 for ($i=0; $i<=$MAXADAPTERS-1; $i++)
	 {
	     $index = "_$i" unless ($netconfig =~ /_$i/);
	 }
	 if ($index eq "")
	 {
	     die "FATAL: Maximum adapters reached - cannot config another vadapter\n";
	 }
	 $ip = "";
	 while (!is_ip_addr($ip))
	 {
	     print "Enter ip address for $devname: ";
	     $ip = <STDIN>;
	 }
	 
	 $netmask = "";
	 while (!is_ip_addr($netmask))
	 {
	     print "Enter netmask for $devname: ";
	     $netmask = <STDIN>;
	 }
	 chomp $ip;
	 chomp $netmask;
	 
	 foreach $line (`cat /etc/rc.config`)
	 {
	     if ($line =~ /^NETCONFIG=/)
	     {
				push @vadapter, "#$line";
				push @vadapter, "NETCONFIG=\"$netconfig $index\"\n";
	     }
	     elsif ($line =~ /^IFCONFIG$index/)
	     {
				push @vadapter, "IFCONFIG$index=\"$ip netmask $netmask\"\n";
	     }
	     elsif ($line =~ /^IPADDR$index/)
	     {
				push @vadapter, "IPADDR$index=\"$ip\"\n";
	     }
	     elsif ($line =~ /^NETDEV$index/)
	     {
				push @vadapter, "NETDEV$index=\"$devname\"\n";
	     }
	     else
	     {
				push @vadapter, $line;
	     }
	 }
}

sub is_ip_addr
{
	 my($ip)=@_;
	 if ($ip=~/^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$/)
	 {
		  if ($1>255 || $2>255 || $3>255 || $4>255)
		  {  
				return 0;
		  }
		  return 1;
	 }
	 return 0;
}
