#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004,2007 
# 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 

# "@(#)70   1.2   src/csm/hw/hs/hdwr_svr_helper, csm.hardware, csm_rfish, rfishs001b 4/22/05 19:38:23"

# http://vergil.chemistry.gatech.edu/resources/programming/perl-tutorial/process.html
# This script will try to bring up a slip interface given 
#  a vtty and optional IP address information
print "args = ".@ARGV."\n";
if (@ARGV < 4) {
   print "Usage: hdwr_svr_helper slipup ttypath network_interface netmask [mtu]\n";
   print "   Ex: hdwr_svr_helper slipup /dev/pts/1 192.168.1.1 255.255.255.0 576\n";
   print " Note: The MTU size should not exceed the hypervisor max MTU of 576.\n";
   die("exiting...");
}

if (@ARGV[0] != "slipup") {
  die ("bad command type");
}

$tty_path = @ARGV[1];
$gateway_address =  @ARGV[2];
$ifconfig_netmask = @ARGV[3];
# If hardware server doesn't support passing the mtu yet, then fall back to the old
#   296 mtu from GA1. Then this script can be used for GA1 through GA5.
$ifconfig_mtu_size = 296;
if (@ARGV > 4) {
  $ifconfig_mtu_size = @ARGV[4];
}

print "tty path = ".$tty_path."\n";
print "gateway_address = ".$gateway_address."\n";
print "netmask = ".$ifconfig_netmask."\n";
print "mtu_size = ".$ifconfig_mtu_size."\n";

$slattachout = `/sbin/slattach -ed -p slip $tty_path`;
print "slattach output:\n\n".$slattachout."\n";

$slip_interface = "uninitialized";

$ind = index($slattachout, " interface sl");
print "slindex = $ind \n";
if ($ind > 0) {
  $slip_interface = substr($slattachout, $ind + 11, 3);
} else {
  print "slattach output error:\n\n".$slattachout."\n";
  die("slattach output error");
}

print "slipifc = '$slip_interface' \n";
print "slipifc len = ".length($slip_interface)."\n";

# We now use ifup instead of ifconfig to close a window where some other code on the
# HMC calls ifdown on this slip interface (typically via /etc/init.d/network restart)  

# Create the slx ifconfig file for the ifup script on the HMC 
# We need 'onboot' to ensure that this interface is started vi the network restart script.
$filename = "/etc/sysconfig/network/ifcfg-".$slip_interface;
print "ifconfig filename = ".$filename."\n";

open(outfile, ">".$filename) || die "cannot open: $!";
print outfile "STARTMODE='onboot'\n";
print outfile "POOTPROTO='static'\n";
print outfile "IPADDR='".$gateway_address."'\n";
print outfile "NETMASK='".$ifconfig_netmask."'\n";
print outfile "MTU='".$ifconfig_mtu_size."'\n";
close(outfile);

$ifupout = `/sbin/ifup $slip_interface -o debug`;
print "ifup output:\n";
print $ifupout;

# Dump out the current route and link information to help with field debug.
print "\nip route output:\n";
print `/sbin/ip route`;

print "\nip link output:\n";
print `/sbin/ip link`;

# Dump a message so that the caller's trace knows that we are done.
print "\nhdwr_svr_helper is now done.\n"

