#!/sbin/sh
#
# Copyright (c) 2000-2001 by Sun Microsystems, Inc.
# All rights reserved.
#
#pragma ident  "@(#)sunips 1.13       02/04/04 SMI"
#
#
# ips only running under 64-bit and Solaris 8 above
#
mode=$1
inst=$1
plumb=""

if [ $2 ]; then
	plumb=$2
fi
	
#
# 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!
#
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

#
# usage()
#
usage() {
	echo "Usage: $0 { start | stop | ips# [plumb | unplumb] }"
	echo "Usage: $0 start (plumb all ips interfaces)"
	echo "Usage: $0 stop  (unplumb all ips interfaces)"
	echo "Usage: $0 ips# plumb (plumb ips# only)"
	echo "Usage: $0 ips# unplumb (unplumb ips# only)"
	echo " "
}

#
# save/move the /etc/hostname.ips# just in case
#
if [ -f /etc/hostname.ips[0-9] ]; then
	for IPSFILE in /etc/hostname.ips*; do
		mv $IPSFILE $IPSFILE.orig
	done
fi
#
# parse /etc/ips/ips.cf
#
if [ -f /usr/bin/grep ]; then
	IPS_CF=/etc/ips/ips.cf
	grep -v \^# $IPS_CF | grep "ips[0-9]" > /dev/null 2>&1
	if [ $? != 0 ]; then
		echo "no interface to plumb. exiting..."
		exit 1
	fi

	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}'`
	PPP_LIST=`grep -v \^#  $IPS_CF | awk '$3 ~ /PPP/ { print $1}' ; \
		grep -v \^#  $IPS_CF | awk '$3 ~ /ppp/ { print $1}' ; \
		grep -v \^#  $IPS_CF | awk '$4 ~ /-/ && $3 ~ /-/ { print $1}'`
fi

plumb_srp_if() {

	if [ ! -f $SRP_OPT ]; then
		# no options file, we do not plumb at all
		echo "Warning: no options file /etc/ips/srp-options.$i for SRP"
		exit 1
	else
		# need to check if the inst $i is the primary inst be configured
		ret=`/usr/sbin/ipsconfig | awk '$1 ~ /'$i'/ && ($3 ~ /SRP/ || $3 ~ /srp/) { print $4 }'`
		if [ "$ret" = "no" -o "$ret" = "" ]; then
			echo "sorry! you can not plumb the non-primary interface $i!, exiting..."
			exit 1
		fi

		IPADD=`grep -v \^#  $SRP_OPT | awk '$1 ~ /ip-addr/ { print $2}'` 
		NETM=`grep -v \^#  $SRP_OPT | awk '$1 ~ /netmask/ { print $2}'`
		BCST=`grep -v \^#  $SRP_OPT | awk '$1 ~ /broadcast/ { print $2}'`
		MTU=`grep -v \^#  $SRP_OPT | awk '$1 ~ /mtu/ { print $2}'`
		/usr/sbin/ifconfig $i plumb
		/usr/sbin/ifconfig $i inet $IPADD netmask $NETM broadcast $BCST mtu $MTU up 
	fi
}

plumb_ppp_if() {
	#
	# check for ips's PPP options file
	#
	if [ ! -f /etc/ppp/options.$i ]; then
		echo "no options file /etc/ppp/options.$i for PPP."
	else
		#
		# use 'file' option to load specific options
		#
		if [ ! -f /var/run/$i.pid ]; then
			/usr/bin/pppd /dev/ips file /etc/ppp/options.$i &
		else
			echo "pppd on the instance $i is already running"
		fi
	fi
}

case "$mode" in
'start')
	#
	# 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

	#
	# for SRP
	#
	for i in $SRP_LIST; do
		SRP_OPT=/etc/ips/srp-options.$i
		plumb_srp_if
	done
	# 
	# for PPP
	#
	# check if pppd installed on this machine
	#
	if [ -f /usr/bin/pppd ]; then
		if [ ! -x /usr/bin/pppd ]; then
			echo "Solaris PPP has not been correctly installed on"
			echo "this system.  Required files are missing."
			echo "please install <SUNWpppdu>"
			exit 1
		fi

		#
		# each PPP instance must have options.ipsN to differ from 
		# other PPP devices
		#
		for i in $PPP_LIST; do
			plumb_ppp_if
		done
	fi
	exit 0
	;;

'stop')
	#
	# for SRP
	# remove this for now since we have not tested SRP yet.
	#
	for i in $SRP_LIST; do
		/usr/sbin/ifconfig $i unplumb
	done
	# 
	# for PPP
	#
	#
	# each PPP instance must have options.ipsN to differ from other PPP
	# devices
	#
	for i in $PPP_LIST; do
		if [ -f /var/run/$i.pid ]; then
			ppp_pid=`/usr/bin/cat /var/run/$i.pid`
			echo "killing the pppd for $i..."
			/usr/bin/kill $ppp_pid
		fi
	done
	exit 0
	;;

ips[0-9])
	#
	# get second parameter
	#
	prot=`grep -v \^#  $IPS_CF |  awk '$3 ~ /PPP/ || $3 ~ /ppp/ || $3 ~ /SRP/ || $3 ~ /srp/ { print $1, $2, $3, $4 }' | awk '$1 ~ /'$inst'/ { print $3 }'`
	if [ "$plumb" = "plumb" ]; then
		if [ "$prot" = "PPP" -o "$prot" = "ppp" ]; then
			i=$inst
			plumb_ppp_if
		else
			if [ "$prot" = "SRP" -o "$prot" = "srp" ]; then
				SRP_OPT=/etc/ips/srp-options.$inst
				i=$inst
				plumb_srp_if
			else
				echo "no $inst to plumb in the /etc/ips/ips.cf!"
			fi
		fi
	else
		if [ "$plumb" = "unplumb" ]; then
			if [ "$prot" = "PPP" -o "$prot" = "ppp" ]; then
				if [ -f /var/run/$inst.pid ]; then
					ppp_pid=`/usr/bin/cat /var/run/$inst.pid`
					echo "killing the pppd for $inst..."
					/usr/bin/kill $ppp_pid
				else
					echo "$inst is not plumbed by pppd!"
				fi
			else
				if [ "$prot" = "SRP" -o "$prot" = "srp" ]; then
					/usr/sbin/ifconfig $inst unplumb
				else
					echo "no $inst to unplumb in the /etc/ips/ips.cf!"
				fi
			fi
		else
			usage
			exit 1
		fi
	fi
	exit 0
	;;
		
*)
	usage
	exit 1
	;;

esac
