#!/usr/bin/perl
################################################################################
# This software program is available to you under a choice of one of two 
# licenses. You may choose to be licensed under either the GNU General Public 
# License 2.0, June 1991, available at http://www.fsf.org/copyleft/gpl.html, 
# or the Intel BSD + Patent License, the text of which follows:
# 
# Recipient has requested a license and Intel Corporation ("Intel") is willing
# to grant a license for the software entitled Linux Intel Advanced Network
# Services (iANS) (the "Licensed Program") being provided by Intel Corporation.
# The following definitions apply to this license:
# 
# "Licensed Patents" means patent claims licensable by Intel Corporation which 
# are necessarily infringed by the use of sale of the Software alone or when 
# combined with the operating system referred to below.
# 
# "Recipient" means the party to whom Intel delivers this Software.
# 
# "Licensee" means Recipient and those third parties that receive a license to 
# any operating system available under the GNU General Public License 2.0 or 
# later.
# 
# Copyright (c) 1999 - 2002 Intel Corporation.
# All rights reserved.
# 
# The license is provided to Recipient and Recipient's Licensees under the 
# following terms.
# 
# Redistribution and use in source and binary forms of the Software, with or 
# without modification, are permitted provided that the following conditions 
# are met:
# 
# Redistributions of source code of the Software may retain the above 
# copyright notice, this list of conditions and the following disclaimer.
# 
# Redistributions in binary form of the Software may reproduce the above 
# copyright notice, this list of conditions and the following disclaimer in 
# the documentation and/or materials provided with the distribution.
# 
# Neither the name of Intel Corporation nor the names of its contributors 
# shall be used to endorse or promote products derived from this Software 
# without specific prior written permission.
# 
# Intel hereby grants Recipient and Licensees a non-exclusive, worldwide, 
# royalty-free patent license under Licensed Patents to make, use, sell, offer 
# to sell, import and otherwise transfer the Software, if any, in source code 
# and object code form. This license shall include changes to the Software 
# that are error corrections or other minor changes to the Software that do 
# not add functionality or features when the Software is incorporated in any 
# version of an operating system that has been distributed under the GNU 
# General Public License 2.0 or later. This patent license shall apply to the 
# combination of the Software and any operating system licensed under the GNU 
# General Public License 2.0 or later if, at the time Intel provides the 
# Software to Recipient, such addition of the Software to the then publicly 
# available versions of such operating systems available under the GNU General 
# Public License 2.0 or later (whether in gold, beta or alpha form) causes 
# such combination to be covered by the Licensed Patents. The patent license 
# shall not apply to any other combinations which include the Software. NO 
# hardware per se is licensed hereunder.
# 
# 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 MECHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR IT CONTRIBUTORS BE LIABLE FOR ANY 
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
# (INCLUDING, BUT NOT LIMITED, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
# ANY LOSS OF USE; DATA, OR PROFITS; OR BUSINESS INTERUPTION) 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 (TurboL)  **
#   **                                                                        **
#   ***************************************************************************/
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";

$teamnum = 0;
@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
	 {
		  $vlan_mode = "";
		  print "\nEnter team name, <Enter> to end: ";
		  $team = <STDIN>;
		  chomp $team;
		  die "No team configured, installation aborting\n" if (!$team && $teamnum == 0);
		  exit 0 if (!$team);
		  print "Would you like to have VLANs in team $team? ";
		  $vlan_mode = "-V" if (options ("y", "n") eq "y");
	 }
	 while (system("$APP -a -t$team $vlan_mode"));

	 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 = "";
		  }
		  last if ($res eq "0");

		  $res = "err" if $res < 1 || $res > @adapters || system("$APP -a -t$team -m$adapters[$res-1]");
		  if ($res > 0)
		  {
				$pri = 0;
				while ($pri  < 1 || $pri > @PRIORITIES)
				{
					 print "\nSelect member priority for member $adapters[$res-1] in team $team:\n";
					 $i=1;
					 foreach $_ (@PRIORITIES)
					 {
						  chomp($_);
						  print "$i. $_\n";
						  $i++;
					 }
					 $pri = <STDIN>;
					 chomp $pri;
				}
				system("$APP -C -m$adapters[$res-1] -p$PRIORITIES[$pri-1]");
				push (@teamadapters, $adapters[$res - 1]);
				splice (@adapters, $res-1, 1);
		  }
	 }
	 delete @vlans {keys %vlans};
vadapter:

	 while (1)
	 {
		 $vadapter = "";

		 while (!$vadapter)
		 {
			 print "\nEnter virtual adapter name for team $team";
			 print ", enter for none" if $vlan_mode eq "-V";
			 print ": ";
			 $vadapter = <STDIN>;
			 chomp ($vadapter);
			 last vadapter if ($vadapter eq "" && $vlan_mode eq "-V" && %vlans > 0);
			 $vlan_id = "";
			 $vlan_num = 0;
			 while (!($vadapter eq "") && $vlan_mode eq "-V" && $vlan_num < 1)
			 {
				 print "Enter VLAN id for virtual adapter $vadapter: ";
				 $vlan_num = <STDIN>;
				 $vlan_id = "-i $vlan_num";
			 }
			 $vadapter = "" if (!($vadapter eq "") && system("$APP -a -t$team -v$vadapter $vlan_id"));
		 }
		 config_vadapter($vadapter);
		 open (OUT, ">ifcfg-$vadapter");
		 print OUT join("", @vadapter);
		 close (OUT);
		 `chmod +x ifcfg-$vadapter`;

		 if ($vlan_mode eq "-V")
		 {
			 $vlans{$vadapter} = $vlan_num;
		 }
		 else
		 {
			 $vlans{$vadapter} = 0;
			 last;
		 }
	 }

	 $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 -C -t$team -M$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 "\n";
	 if ($vlan_mode eq "-V")
	 {
		 while (($vadapter, $vlan_id) = each(%vlans))
		 {
			 print "Vadapter: $vadapter, VLAN id: $vlan_id";
		 }
	 }
	 else
	 {
		 print "Vadapter 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 -c$team"));

	 if ($res=~"^(n|N).*")
	 {
		  system("$APP -d -t$team");
		  push(@adapters, @teamadapters);
		  @adapters = sort @adapters;
	 }
	 else
	 {
		  foreach (@teamadapters)
		  {
				setonboot($_, "no");
		  }
		  foreach (keys %vlans)
		  {
			  `cp -f ifcfg-$_ $IFCFGDIR`;
		  }
		  $teamnum++;
	 }
	 print "\n\n";
}
die "No team configured, installation aborting\n" if ($teamnum == 0);

sub config_vadapter
{
	 my($devname) = @_;

	 splice(@vadapter, 0);
	 push @vadapter, "DEVICE=\"$devname\"\n";

	 print "Enter boot protocol - (N)one, (B)OOTP, (D)HCP, (O)ther for virtual adapter $devname: ";
	 $bootp = options ("n", "bdo");

	 if ($bootp eq "o")
	 {
		  print "Enter protocol name for virtual adapter $devname: ";
		  $BOOTPROTOS{"o"} = <STDIN>;
		  chomp $BOOTPROTOS{"o"};
	 }

	 push @vadapter, "BOOTPROTO=\"$BOOTPROTOS{$bootp}\"\n";
	 push @vadapter, "ONBOOT=\"yes\"\n";

	 if ($bootp eq "n")
	 {
		  $ip = "";
		  while (!is_ip_addr($ip))
		  {
				print "Enter ip address for $vadapter: ";
				$ip = <STDIN>;
		  }

		  $netmask = "";
		  while (!is_ip_addr($netmask))
		  {
				print "Enter netmask for $vadapter: ";
				$netmask = <STDIN>;
		  }
		  chomp $ip;
		  chomp $netmask;

		  push @vadapter, "IPADDR=\"$ip\"\n";
		  push @vadapter, "NETMASK=\"$netmask\"\n";
	 }
}

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;
}
