# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/sbin/mktcpip.ib/mktcpip.ib 1.7.2.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2005,2014 
# 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 
#
#   COMPONENT_NAME: ibif
#
#   FUNCTIONS: problem
#		usage
#
#   ORIGINS: 27
#
#                    -- (                            when
#   combined with the aggregated modules for this product)
#   OBJECT CODE ONLY SOURCE MATERIALS
#
#   (C) COPYRIGHT International Business Machines Corp. 1995
#   All Rights Reserved
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#


# 
# FILE NAME: mktcpip.ib
#
# FILE DESCRIPTION: High-level shell command for performing minimal
#   configuration required to get a machine up and running TCP/IP over ATM.
#
#   Basic functions performed are:
#   1)  the hostname is set both in the config database and in running machine
#   2)  the IP address of the interface is set in the config database.
#   3)  /etc/hosts entries made for hostname and IP address
#   4)  the IP address of the nameserver and domain name are set
#   5)  the subnet mask is set
#   6)  destination and gateway routes are set
#   7)  TCP/IP deamons started
#       or
#   8)  Retrieve the above information for SMIT display 
#
#   See Usage message for explanation of parms
#
#
# RETURN VALUE DESCRIPTION: 
#                             0         Successful
#                             non-zero  Unsuccessful
# 
#
# EXTERNAL PROCEDURES CALLED: chdev, hostname, hostent, lsdev
#                             mkdev, netstat, namerslv, /etc/rc.tcpip
#
#
# GLOBAL VARIABLES: none
#

################################# usage #######################################
#
# NAME: usage()
#
# DESCRIPTION: Issue "usage" message and exit.
#
# INPUT: 
#        None
#
# OUTPUT:
#        Error messages (Standard Error)
#
# RETURN VALUE DESCRIPTION:
#                           2
#
# NOTE: This function will not return (i.e., it will exit the entire
#       script with exit status of 2).
#
usage(){
   cat <<- HERE >&2
   Usage:  $NAME {-S interface | -h hostname -a address -i interface
               [-n nameserver_address -d domain] [-m subnet_mask] 
               [-g gateway_address] 
	       [-I idle_time] [-U ubr_rate]}

   -h hostname            Hostname of your machine
   -a address             IP address of the interface specified by -i
                          (must be in dotted decimal notation)
   -i interface           Interface to associate with the -a IP address
   -n nameserver_address  IP address of nameserver machine
                          (must be in dotted decimal notation)
   -d domain              Domain name, only use with -n
   -m subnet_mask         Subnetwork mask (dotted decimal or 0x notation)
   -g gateway_address     Gateway destination address
                          (dotted decimal or symbolic name)
   -S interface           Retrieve information for SMIT display
   -s                     Start the TCP/IP daemon

   Example: $NAME -h fred.austin.ibm.com -a 192.9.200.9 -i ib0 

HERE
   exit 2			# don't return
}

############################# mandatory #######################################
#
# NAME: mandatory()
#
# DESCRIPTION: Issue "mandatory" message and invoke "usage" function.
#
# INPUT: 
#        None
#
# OUTPUT:
#        Error messages (Standard Error)
#
# RETURN VALUE DESCRIPTION:
#                           None
#
mandatory(){
   /usr/bin/echo "$NAME: Mandatory flag(s) missing" >&2
   usage			# issue msg and don't return
}

############################## problem ########################################
#
# NAME: problem()
#
# DESCRIPTION: Issue "problem" message and exit
#
# INPUT: 
#        $RTNCODE
#        $1		Name of command that encountered an error
#
# OUTPUT:
#        Error messages (Standard Error)
#
# RETURN VALUE DESCRIPTION:
#                           $RTNCODE
#
# NOTE: This function will not return (i.e., it will exit the entire
#       script with exit status of $RTNCODE).
#
problem(){
   /usr/bin/echo "$NAME: Problem with $1 command, return code = $RTNCODE" >&2
   exit $RTNCODE			# don't return
}

############################### dotted ########################################
#
# NAME: dotted()
#
# DESCRIPTION: Perform rudimentary check of IP address to see if composed
#              of dots and numbers.  Will issue error message and invoke
#              usage function if error detected.
#
# INPUT: 
#        $1, IP Address
#
# OUTPUT:
#        Error messages (Standard Error).  If error detected.
#
# RETURN VALUE DESCRIPTION:
#                           0     (Successful)
#                           None  (Will invoke usage function if error)
#
dotted(){
# compare number chars to number of "decimal and 0-9" characters
if [ "`expr $1 : '.*'`" -ne "`expr $1 : '[.0-9]*'`" ] ; then
   /usr/bin/echo "$NAME: IP address, $1, not in dotted decimal notation" >&2
   usage			# issue message and don't return
fi
}

############################## main ############################################
PATH=/bin:/usr/bin:/usr/sbin:/etc:/usr/ucb export PATH
NAME=$0
# Parse command flags and arguments
set -- `getopt h:a:i:S:n:d:m:g:t:sp:A: $*`
if [ $? != 0 ] ; then		# test for syntax error
   usage			# issue msg and don't return
fi

#if [ $# -lt 3 ] ; then		# test for too few parms
#   mandatory			# issue msg and don't return
#fi

HOSTNAME= IPADDRESS= INTERFACE= NAMESERVER= DOMAIN= SUBNETMASK= GATEWAY= STARTTCP= SHOW= DESTINATION= SUBCHANNEL= DEVNAME= SERVADDR= IDLE= UBR=

while [ "$1" != "--" ]
do

   NULLARG=1
   # If the next arg starts with - set NULLARG to a null value
   if [[ $2 = -* ]]; then NULLARG=""; fi

   case $1 in
      -h)
         if [ -n "$NULLARG" ]; then 
	   HOSTNAME=$2
           shift 2
         fi
      ;;
      -a)
         if [ -n "$NULLARG" ]; then
           IPADDRESS=$2
           shift 2
         fi
      ;;
      -i)
         if [ -n "$NULLARG" ]; then
           INTERFACE=$2
           shift 2
         fi
      ;;
      -C)
         if [ -n "$NULLARG" ]; then
           CONNECTION=$2
           shift 2
         fi
      ;;
      -I)
         if [ -n "$NULLARG" ]; then
           IDLE=$2
           shift 2
         fi
      ;;
      -n)
         if [ -n "$NULLARG" ]; then
           NAMESERVER=$2
           shift 2
         fi
      ;;
      -d)
         if [ -n "$NULLARG" ]; then
           DOMAIN=$2
           shift 2
         fi
      ;;
      -m)
         if [ -n "$NULLARG" ]; then
           SUBNETMASK=" -a netmask=$2"
           shift 2
         fi
      ;;
      -g)
         if [ -n "$NULLARG" ]; then
           GATEWAY=$2
           shift 2
         fi
       ;;
      -s)
         NULLARG=1
	 STARTTCP=1 
         shift 1

      ;;
      -S)
         if [ -n "$NULLARG" ]; then
           SHOW=$2
           shift 2
         fi
      ;;
      -U)
         if [ -n "$NULLARG" ]; then
           UBR=$2
           shift 2
         fi
      ;;
      -A)
         if [ -n "$NULLARG" ]; then
           HCA_ADAPTER=$2
           shift 2
         fi
      ;;
      -p)
         if [ -n "$NULLARG" ]; then
           IB_PORT=$2
           shift 2
         fi
      ;;
      *)
         /usr/bin/echo this is the flag: parameter $1 value $2
         exit
	 usage				# shouldn't ever hit this
      ;;
   esac

  # We got an flag without an argument.  call usage

  if [ -z "$NULLARG" ]; then
     usage
  fi

done

if [ $# -gt 1 ] ; then			# see if extra parms specified
   /usr/bin/echo "$NAME: Too many flags" >&2
   usage				# issue msg and don't return
fi


# get the domain and nameserver in /etc/resolv.conf, if any
/usr/bin/grep domain /etc/resolv.conf > /dev/null 2>&1
RTNCODE=$?
if [ $RTNCODE -ne 0 ] ; then
   SNAME=
   DOMN=
else
   TMP=`/usr/sbin/namerslv -sI | /usr/bin/awk '{ print $2}'`
   SNAME=`/usr/bin/echo $TMP | /usr/bin/awk '{ print $1}'`
   DOMN=`/usr/sbin/namerslv -sn | /usr/bin/awk '{ print $2}'`
fi

if [ "$SHOW" != "" ] ; then

   # Find whether the interface is for Ethernet, Token ring or else ..
   SHW=`/usr/bin/echo $SHOW | /usr/bin/cut -c1-2`
   # get the hostname
   HOST=`/usr/bin/hostname`
   # get the IPaddr and the subnetmask
   ADDR=`/usr/sbin/lsattr -E -l $SHOW -F "value" -a netaddr`

   MASK=`/usr/sbin/lsattr -E -l $SHOW -F "value" -a netmask`
   #IDLE=`/usr/sbin/lsattr -E -l $SHOW -F "value" -a idle` ??? TODO
   #UBR=`/usr/sbin/lsattr -E -l $SHOW -F "value" -a ubr_rate` do we need this? TODO
   	
   # get the default gateway address
   FLAG=`/usr/bin/netstat -rn | /usr/bin/grep default | /usr/bin/head -1 | /usr/bin/awk '{ print $3 }'`
   FLAGS=`/usr/bin/echo $FLAG | /usr/bin/cut -c1-2` #to get first two letters of the FLAG
   HCA_ADAPTER=`/usr/sbin/lsattr -El $SHOW -F "value" -a ib_adapter`
   IB_PORT=`/usr/sbin/lsattr -El $SHOW -F "value" -a ib_port`
   if [ "$FLAGS" = "UG" ]; then
        GATE=`/usr/bin/netstat -rn | /usr/bin/grep default | /usr/bin/head -1 | /usr/bin/awk '{ print $2 }'`
   fi

   # we don't want rc.tcpip started again accidentally
   START="no"
   # print all of the requested information in command to discover format
   /usr/bin/echo "#host:addr:mask:_rawname:nameserv:domain:gateway:ib_adapter:ib_port:start" >&2
   /usr/bin/echo "$HOST:$ADDR:$MASK:$SHOW:$SNAME:$DOMN:$GATE:$HCA_ADAPTER:$IB_PORT:$START" >&2
   exit 0
fi

SHW=`/usr/bin/echo $INTERFACE | /usr/bin/cut -c1-2`	#to get first two letters, ex. en, et..
INT=`/usr/bin/echo $INTERFACE | /usr/bin/cut -c3-4`	#to get last digits in interface.

# See if mandatory parms were specified

if [ "$HOSTNAME" = "" -o "$IPADDRESS" = "" -o "$INTERFACE" = "" -o "$HCA_ADAPTER" = "" -o "$IB_PORT" = "" ] ; then
#if [ "$HOSTNAME" = "" -o "$IPADDRESS" = "" -o "$INTERFACE" = "" ] ; then
 /usr/bin/echo $HOSTNAME $IPADDRESS $INTERFACE $HCA_ADAPTER $IB_PORT
   mandatory
fi

GWAY=`/usr/bin/echo "$GATEWAY" | /usr/bin/awk '{split($0,a,","); print a[1]}'`
if [ "$GWAY" != "" ] ; then
	GATEWAY="$GWAY"
fi
# See if default gateway addresses present 
if [ "$GATEWAY" != "" ] ; then
   DESTINATION="0"
else
   DESTINATION=
fi

dotted $IPADDRESS		# rudimentary check on address

# See if DOMAIN specified without NAMESERVER, and vice versa
if [ "$DOMAIN" != "" -a "$NAMESERVER" = "" ] ; then
   /usr/bin/echo "$NAME: -d flag cannot be specified without -n flag" >&2
   usage			# issue message and don't return
fi
if [ "$NAMESERVER" != "" -a "$DOMAIN" = "" ] ; then
   /usr/bin/echo "$NAME: -n flag cannot be specified without -d flag" >&2
   usage			# issue message and don't return
fi

# Save off current umask and set it to 022 so that any creation
# of the files /etc/resolv.conf or /etc/hosts by namerslv or hostent
# will have the correct 644 permissions.
UMASKSAVE=`umask`
umask 022

[ "$NAMESERVER" != "" ] && dotted $NAMESERVER	# rudimentary address check
# Make entry in local /etc/hosts
# Make sure that /etc/hosts exists
if [ ! -f /etc/hosts ]; then
	/usr/bin/touch /etc/hosts
fi
# Make sure that the address isn't commented out
/usr/bin/grep -F -w "$IPADDRESS" /etc/hosts | /usr/bin/grep -v ".*#.*$IPADDRESS" > /dev/null 2>&1
RTNCODE=$?
# If new IP address is in /etc/hosts file then add the new hostname as an alias
# Else enter the IP address and hostname as new entry in /etc/hosts file.

if [ $RTNCODE -eq 0 ]; then
         ONAMES=`/usr/bin/grep -F -w "$IPADDRESS" /etc/hosts | \
               /usr/bin/grep -v ".*#.*$IPADDRESS" | \
               /usr/bin/awk '{for(i=2;i<=NF;i++)
                         if(substr($i,1,1)=="#")
                           break;
                         else
                           printf("%s ",$i) }'`
        # if new hostname is anywhere on old entry, don't add it
        DONTADD=0
        for i in $ONAMES
          do
            if [ $HOSTNAME = $i ]; then
              DONTADD=1
            fi
          done
        if [ $DONTADD -eq 0 ]; then
                /usr/bin/hostent -c "$IPADDRESS" -h "$ONAMES $HOSTNAME"
		RTNCODE=$?
		[ $RTNCODE -ne 0 ] && problem hostent
	fi
else
	/usr/bin/hostent -a "$IPADDRESS" -h "$HOSTNAME"
	RTNCODE=$?
	[ $RTNCODE -ne 0 ] && problem hostent
fi
# Set hostname in the running system
/usr/bin/hostname "$HOSTNAME"
RTNCODE=$?
[ $RTNCODE -ne 0 ] && problem hostname

# Set hostid  of  the running system
/usr/sbin/hostid `/usr/bin/hostname`
RTNCODE=$?
[ $RTNCODE -ne 0 ] && problem hostid

# Set uname of the running system
/usr/bin/uname -S "${HOSTNAME%%.*}"
RTNCODE=$?
[ $RTNCODE -ne 0 ] && problem uname

# Set hostname and IP addr in config DB

# Avoid a 2nd mkdev if inet0 already exists.  If the user accidentally
# "committed" before entering all SMIT commands, they'll need to re-enter
# the data and try again.  The 1st time would have created inet0
if /usr/sbin/lsdev -C -c tcpip | /usr/bin/egrep inet0 2>&1 > /dev/null ; then
   : 			# inet0 already exists
else
   /usr/sbin/mkdev -t inet
   RTNCODE=$?
   [ $RTNCODE -ne 0 ] && problem mkdev
fi
/usr/sbin/chdev -l inet0 -a hostname="$HOSTNAME"
RTNCODE=$?
[ $RTNCODE -ne 0 ] && problem chdev

if /usr/sbin/lsdev -Cc if -s IB -t ib  | /usr/bin/egrep "^$INTERFACE " 2>&1 > /dev/null ; then
   : 			# the interface already exists
else
      /usr/lib/methods/defibaif -c if -s IB -t ib -l $INTERFACE 
      RTNCODE=$?
      [ $RTNCODE -ne 0 ] && problem mkdev
fi


if [ "$IDLE" != "" ] ; then
   /usr/sbin/chdev -l $INTERFACE -a idle="$IDLE"
   RTNCODE=$?
   [ $RTNCODE -ne 0 ] && problem chdev
fi

if [ "$UBR" != "" ] ; then
   /usr/sbin/chdev -l $INTERFACE -a ubr_rate="$UBR"
   RTNCODE=$?
   [ $RTNCODE -ne 0 ] && problem chdev
fi

# Change nameserver and domain entries, if the new entry is same as old then
# Don't change.
if [ "$NAMESERVER" != "" -a "$NAMESERVER" != "$SNAME" ] ; then
   /usr/sbin/namerslv -a -i "$NAMESERVER"
   RTNCODE=$?
   [ $RTNCODE -ne 0 ] && problem /usr/sbin/namerslv
fi

if [ "$DOMAIN" != "" -a "$DOMAIN" != "$DOMN" ] ; then
   /usr/sbin/namerslv -c "$DOMAIN"
   RTNCODE=$?
   [ $RTNCODE -ne 0 ] && problem /usr/sbin/namerslv
fi

# Change inet0 with route if route present 
if [ "$DESTINATION" != "" ] ; then
   GATE2=`/usr/sbin/lsattr -E -l inet0 -F "value" -a route`
   if [ "$GATE2" != "" ] ; then
   	/usr/sbin/chdev -l inet0 -a delroute="$GATE2" > /dev/null 2>&1
   fi
   FLAGS=`/usr/bin/netstat -rn | /usr/bin/grep default | /usr/bin/head -1 | /usr/bin/awk '{ print $3 }'`
   if [ "$FLAGS" = "UG" ]; then
      GATE1=`/usr/bin/netstat -rn | /usr/bin/grep default | /usr/bin/awk '{ print $2 }'`
      if [ "$GATE1" != "" ] ; then
                /usr/sbin/chdev -l inet0 -a delroute="$DESTINATION","$GATE1" > /dev/null 2>&1
      fi
   fi
   /usr/sbin/chdev -l inet0 -a route="$DESTINATION","$GATEWAY"
   RTNCODE=$?
   [ $RTNCODE -ne 0 ] && problem chdev
fi


# Change the interface ip address if it was changed.

OLDIPADDR=`/usr/sbin/lsattr -El $INTERFACE | /usr/bin/grep netaddr | /usr/bin/awk '{ print $2 }' `
OLDMASK=`/usr/sbin/lsattr -El $INTERFACE | /usr/bin/grep netmask | /usr/bin/awk '{ print $2 }' `

if [ "$OLDIPADDR" != "$IPADDRESS" -o "$OLDMASK" != "$SUBNETMASK" ]; then
/usr/sbin/chdev -l $INTERFACE -a netaddr=$IPADDRESS -a netmask=$SUBNETMASK -a ib_adapter=$HCA_ADAPTER -a ib_port=$IB_PORT
fi



# Set the umask back to the original value.
/usr/bin/umask $UMASKSAVE
# Start TCP/IP if need be
[ "$STARTTCP" = "1" ] && exec /etc/rc.tcpip
exit 0