#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_add_routes.sh 1.5.1.2 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1993,2011 
# 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 
# @(#)36	1.5.1.2  src/bos/usr/lib/nim/methods/c_add_routes.sh, cmdnim, bos720  7/14/11  17:58:06
#
#
#   COMPONENT_NAME: CMDNIM
#
#   FUNCTIONS: ./usr/lib/nim/methods/c_add_routes.sh
#		
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1993
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.

# ---------------------------- c_add_routes           
#
# NAME: c_add_routes
#
# FUNCTION:	 Imports the "new" niminfo file and adds any routes 
#
# RETURNS: (int)
#	1	= The niminfo file is zero length or does not exist
#	0	= always assumes no errors
#
# --------------------------------------------------------------------------- #
#
NIM_INFO="/etc/niminfo" 
ROUTE="/usr/sbin/route"
CHDEV="/usr/sbin/chdev"
NETSTAT="/usr/bin/netstat"
AWK="/usr/bin/awk"
GREP="/usr/bin/grep"
LSATTR="/usr/sbin/lsattr"

[[ -s ${NIM_INFO} ]] && . ${NIM_INFO} || exit 1
[[ -z "${ROUTES}" ]] && exit 0

#
# set up route tables
#
# If a route aleady exists, the chdev command will return an error.  To
# avoid that error, unconditionally attempt to remove the route from the
# routing table before calling chdev to add it.
#
for route_args in `echo "${ROUTES}"`
do
	OIFS="$IFS"; IFS=':'
	set -- $route_args
	IFS="$OIFS"

	# Need to check if the current route is already in our routing table
	# If it is then we don't need to add it again...
	$NETSTAT -nr | $AWK '{print $2}' | $GREP "^${3}$" >/dev/null 2>&1
	if [[ $? -eq 0 ]]; then
		continue
	fi

	# If the route is a default route, then a different syntax
	# must be used to add it... BUT, only if a default route is non-existent
	if [[ ${1} = default ]]
	then
		# does client have a route value set for inet0 ?
		if [[ -n `$LSATTR -OEl inet0 -a route 2>/dev/null | $AWK 'NR==2{print $1}'` ]]; then
			# then do NOTHING for this entry
			continue
		fi

		$ROUTE delete -net 0 ${3} >/dev/null 2>&1
		$CHDEV -l inet0 -a route="0,${3}" >/dev/null

	else
		$ROUTE delete -net ${1} ${3} >/dev/null 2>&1
		$CHDEV -l inet0 -a route="net,${1},-netmask,${2},${3}" >/dev/null

	fi

done

#
# assume all is ok
# 
exit 0
