#!/sbin/sh
#
# Copyright (c) 2000-2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#pragma ident  "@(#)sunipsinit 1.4       02/04/11 SMI"
#
# If we can't find /usr/bin/id, we're most likely running during
# boot, and /usr isn't mounted yet.  In that case, we're sure to
# be root, so this check is not necessary, as well as not possible!
#
inst=""

#
# usage()
#
usage() {
	echo "Usage: $0 [ips#]"
	echo "Usage: $0 (configure all ips interfaces)"
	echo "Usage: $0 ips# (configure ips# only)"
	echo " "
}

if [ $1 ]; then
	inst=$1
	case "$inst" in
	ips[0-9])
		;;
	start)
		;;
	*)
		usage
		exit 1
	;;
	esac
fi
	
if [ -f /usr/bin/id ]; then
	set `/usr/bin/id`
	if [ $1 != "uid=0(root)" ]; then
		echo "$0: must be run as root"
		exit 1
	fi
fi

umask 022

#
# parse /etc/ips/ips.cf
#
IPS_CF=/etc/ips/ips.cf
if [ -f /usr/bin/grep ]; then
	grep -v \^# $IPS_CF | grep "ips[0-9]" > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "no interface to plumb for ips#. exiting..."
		exit 1
	fi
fi

#
# check the required files
#
if [ ! -f /platform/sun4u/kernel/drv/sparcv9/ips ]; then
	echo "Please install <SUNWipsx.u>."
	exit 1
fi

if [ ! -f /usr/sbin/ipsconfig ]; then
	if [ ! -f /etc/ips/ips.cf ]; then
		echo "Please install <SUNWipsut>"
		echo "and configure /etc/ips/ips.cf."
		exit 1
	fi
	echo "Please install <SUNWipsut>."
	exit 1
fi

#
# function to config SRP specific options 
#
HOSTS_FILE=/etc/hosts

TOPTIMER_MAX=600
TOPTIMER_MIN=1
TOPTIMER_DEF=60
IPSTIMER_MAX=60
IPSTIMER_MIN=1
IPSTIMER_DEF=1
WTRTIMER_MAX=600
WTRTIMER_MIN=10
WTRTIMER_DEF=60

config_srp_options() {

	if [ ! -f $SRP_OPT ]; then
		# no options file, we do not do anything here...
		exit 0
	else
		# parse the options and send down to ips...
		IP_ADDR=`grep -v \^#  $SRP_OPT | awk '$1 ~ /ip-addr/ { print $2}'`
		NODENAME=`grep -v \^#  $HOSTS_FILE | awk '$1 ~ /'$IP_ADDR'/ { print $2}'`
		IPSTIMER=`grep -v \^#  $SRP_OPT | awk '$1 ~ /ips-timer/ { print $2}'`
		IPSWTRTIMER=`grep -v \^#  $SRP_OPT | awk '$1 ~ /wtr-timer/ { print $2}'`
		TOPTIMER=`grep -v \^#  $SRP_OPT | awk '$1 ~ /topology-timer/ { print $2}'`
		if [ "$NODENAME" != "" ]; then
			/usr/sbin/ipsconfig -n $i $NODENAME $IP_ADDR
		else
			HOSTNAME=`grep -v \^#  $HOSTS_FILE | awk '$3 ~ /loghost/ {print $2}'`
			/usr/sbin/ipsconfig -n $i $HOSTNAME-$i $IP_ADDR
		fi

		if [ $IPSTIMER -ne $IPSTIMER_DEF ]; then
			if [ $IPSTIMER -le $IPSTIMER_MAX -a $IPSTIMER -ge IPSTIMER_MIN ]; then
				/usr/sbin/ipsconfig -t $i ips $IPSTIMER
			fi
		fi

		if [ $IPSWTRTIMER -ne $WTRTIMER_DEF ]; then
			if [ $IPSWTRTIMER -le $WTRTIMER_MAX -a $IPSWTRTIMER -ge WTRTIMER_MIN ]; then
				/usr/sbin/ipsconfig -t $i wtr $IPSWTRTIMER
			fi
		fi

		if [ $TOPTIMER -ne $TOPTIMER_DEF ]; then
			if [ $TOPTIMER -le $TOPTIMER_MAX -a $TOPTIMER -ge TOPTIMER_MIN ]; then
				/usr/sbin/ipsconfig -t $i top $TOPTIMER
			fi
		fi
	fi
}

#
# take individual instance to config
#
if [ "$inst" = "ips0" -o "$inst" = "ips4" -o "$inst" = "ips8" ]; then
	INST=`grep -v \^#  $IPS_CF |  awk '$3 ~ /PPP/ || $3 ~ /ppp/ || $3 ~ /SRP/ || $3 ~ /srp/ { print $1, $2, $3, $4 }' | awk '$1 ~ /'$inst'/ { print $1 }'`
	FRAME=`grep -v \^#  $IPS_CF |  awk '$3 ~ /PPP/ || $3 ~ /ppp/ || $3 ~ /SRP/ || $3 ~ /srp/ { print $1, $2, $3, $4 }' | awk '$1 ~ /'$inst'/ { print $2 }'`
	PROT=`grep -v \^#  $IPS_CF |  awk '$3 ~ /PPP/ || $3 ~ /ppp/ || $3 ~ /SRP/ || $3 ~ /srp/ { print $1, $2, $3, $4 }' | awk '$1 ~ /'$inst'/ { print $3 }'`
	MATE=`grep -v \^#  $IPS_CF |  awk '$3 ~ /PPP/ || $3 ~ /ppp/ || $3 ~ /SRP/ || $3 ~ /srp/ { print $1, $2, $3, $4 }' | awk '$1 ~ /'$inst'/ { print $4 }'`
	if [ "$INST" = "ips0" -o "$INST" = "ips4" -o "$INST" = "ips8" ]; then
		/usr/sbin/ipsconfig -s $INST $FRAME $PROT $MATE
		if [ $? != 0 ]; then
			echo "ipsconfig failed for $INST! please check hardware!"
			exit 1
		fi
		if [ "$PROT" = "SRP" -o "$PROT" = "srp" ]; then
			SRP_OPT=/etc/ips/srp-options.$INST
			i=$INST
			config_srp_options
		fi
		exit 0	
	fi
	echo "no $inst defined in $IPS_CF! check it again!"
	exit 1	
fi
		
#
# run ipsconfig now...
#
/usr/sbin/ipsconfig -s 
if [ $? != 0 ]; then
	echo "ipsconfig failed! please check hardware!"
	exit 1
fi

#
# to change SRP related parameters
#
SRP_LIST=`grep -v \^#  $IPS_CF | awk '$3 ~ /SRP/ { print $1}' ; \
	grep -v \^#  $IPS_CF | awk '$3 ~ /srp/ { print $1}' ; \
	grep -v \^#  $IPS_CF | awk '$4 !~ /-/ && $3 ~ /-/ { print $1}'`

#
# we also need to setup the SRP nodename
#
for i in $SRP_LIST; do
	SRP_OPT=/etc/ips/srp-options.$i
	config_srp_options
done

