#!/sbin/sh
#
#ident	"@(#)hipup.sh	1.11	95/07/25 CRS"
#
# Copyright (c) 1993, Cray Research Superservers, Inc.
#
#	Usage: hipup
#
# Bring the HIPPI interface up with the specifed ip address and load the ART
# table.
#
################################################################################
# hippi.art0 is a table described in hipart(7). The suffix of 0 is the device
# instance number. Each instance of the hippi board needs its own
# hippi.art table and the suffix is the way you may distinguish between them.
# Each instance needs to be ifconfig'd according to the script below.
################################################################################
HIPETC=/etc/opt/CYRShippi
hipart=$HIPETC/hipart
SCRIPT=`basename ${0}`
CHECKIP="check ip address"
UNABLE="Unable to configure the interface"
NOCONF="\nThe following HiPPI instances were found but none are configured.\n"
IPPAT="^[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*"
STAT=0

error()
{
	echo "\n${SCRIPT}: $* - aborting\n" >&2
	exit 1
}

warn()
{
	echo "\n${SCRIPT}: $*" >&2
	echo "${SCRIPT}: $ifname not configured.\n" >&2
	STAT=`expr $STAT + 1`
}

IDSTR=`id`
UID=`expr "$IDSTR" : 'uid=\([0-9][0-9]*\).*'`
test "${UID}" = "0" || error "You must be root to run this program."

if [ "`echo $HIPETC/hostname.hi[0-9]`" = "$HIPETC/hostname.hi[0-9]" -a "`echo $HIPETC/hostname.ap_nmhi[0-9]`" = "$HIPETC/hostname.ap_nmhi[0-9]" ]
then
	# No HiPPI interfaces are configured.  Check to see if we
	# have any that could be configured.
	prtconf | fgrep CHIS,hs | sort > /tmp/hippi.instance.$$
	if [ -s /tmp/hippi.instance.$$ ]
	then
		echo "$NOCONF"
		cat /tmp/hippi.instance.$$
		if [ -c /dev/hi -o -c /devices/pseudo/*:hi ]
		then
			echo "\nWe also have these device entries:\n"
			/bin/ls -l /dev/hi /devices/pseudo/*:hi
			echo ""
		else
			echo "\nI couldn't find any HiPPI device entries."
			echo "You may want to reconfigure your devices.\n"
		fi
	else
		: echo "No HiPPI boards were found."
	fi
	rm -f /tmp/hippi.instance.$$
	exit 0
fi

get_hostaddr()
{
	case $NS in
	NIS+)   line=`nismatch ${1} hosts.org_dir 2>/dev/null`
		status=$?
		HOST_ADDR=`echo ${line} | \
				(read cname name addr junk; echo $addr)`
		;;
	NIS)    line=`ypmatch ${1} hosts 2>/dev/null`
		status=$?
		HOST_ADDR=`echo ${line} | (read addr name junk; echo $addr)`
		;;
	*)      line=`grep "\<${1}\>" ${HOSTSFILE} 2>/dev/null`
		status=$?
		HOST_ADDR=`echo ${line} | (read addr name junk; echo $addr)`
		;;
	esac
	return $status
}

if [ -f /var/nis/NIS_COLD_START ]; then
	NS="NIS+"
elif ( ps -e | grep "[ /]nisd" > /dev/null ); then
	NS="NIS+"
elif ( ps -e | grep "[ /]ypbind" > /dev/null ); then
	NS="NIS"
else
	NS="local"
fi

if [ "`echo $HIPETC/hostname.hi[0-9]`" != "$HIPETC/hostname.hi[0-9]" ]
then
for ifhn in $HIPETC/hostname.hi[0-9]
do
	ifnum=`expr $ifhn : '.*\.hi\([0-9]\)$'`
	ifname=hi${ifnum}
	mapfile=$HIPETC/hippi.art${ifnum}
	read addr < $ifhn
	if [ -z "$addr" ]
	then
		warn "$ifhn contains nothing!"
		continue
	fi
	if [ ! -f $mapfile ]
	then
		warn "$mapfile does not exist!"
		continue
	fi
	if egrep "^${addr}[ 	]" $mapfile > /dev/null 2>&1
	then
		:
	else
		if get_hostaddr ${addr}
		then
			IPPAT=`echo ${HOST_ADDR} | sed 's;\.;\\\\.;g'`
			if egrep "^${IPPAT}[ 	]" $mapfile > /dev/null 2>&1
			then
				addr="$HOST_ADDR"
			else
				warn "No $addr or $HOST_ADDR in ${mapfile}!"
				continue
			fi
		else
			warn "No $addr in ${mapfile} and can't find IP address!"
			continue
		fi
	fi

	# Load ART table
	$hipart -lc $ifname $mapfile || error "Error loading the ART"

	# Plumb the driver into the protocol stack
	ifconfig $ifname plumb || error "Unable to plumb the $ifname interface"

	# Configure interface in system
	# IMPORTANT - DO NOT CHANGE THIS SEQUENCE OF IFCONFIG's
	ifconfig $ifname $addr netmask + || error "$UNABLE - $CHECKIP ${addr})"
	ifconfig $ifname -trailers up || error "$UNABLE up"

	echo "${SCRIPT}: HIPPI Network interface $ifname - UP"
done
fi

#
# Handle the ap'd networks
#
if [ "`echo $HIPETC/hostname.ap_nmhi[0-9]`" != "$HIPETC/hostname.ap_nmhi[0-9]" ]
then
for ifhn in $HIPETC/hostname.ap_nmhi[0-9]
do
	ifnum=`expr $ifhn : '.*\.ap_nmhi\([0-9]\)$'`
	ifname=ap_nmhi${ifnum}
	mapfile=$HIPETC/hippi.art${ifnum}
	read addr < $ifhn
	if [ -z "$addr" ]
	then
		warn "$ifhn contains nothing!"
		continue
	fi
	if [ ! -f $mapfile ]
	then
		warn "$mapfile does not exist!"
		continue
	fi
	if egrep "^${addr}[ 	]" $mapfile > /dev/null 2>&1
	then
		:
	else
		if get_hostaddr ${addr}
		then
			IPPAT=`echo ${HOST_ADDR} | sed 's;\.;\\\\.;g'`
			if egrep "^${IPPAT}[ 	]" $mapfile > /dev/null 2>&1
			then
				addr="$HOST_ADDR"
			else
				warn "No $addr or $HOST_ADDR in ${mapfile}!"
				continue
			fi
		else
			warn "No $addr in ${mapfile} and can't find IP address!"
			continue
		fi
	fi

	# Load ART table
	$hipart -lc $ifname $mapfile || error "Error loading the ART"

	# Plumb the driver into the protocol stack
	ifconfig $ifname plumb || error "Unable to plumb the $ifname interface"

	# Configure interface in system
	# IMPORTANT - DO NOT CHANGE THIS SEQUENCE OF IFCONFIG's
	ifconfig $ifname $addr netmask + || error "$UNABLE - $CHECKIP ${addr})"
	ifconfig $ifname -trailers up || error "$UNABLE up"

	echo "${SCRIPT}: HIPPI Network interface $ifname - UP"
done
fi

exit $STAT
