#!/bin/ksh
#
#ident  "@(#)publicnet.ksh 1.1     01/07/18 SMI"
#
# Copyright (c) 2001 by Sun Microsystems, Inc.
# All rights reserved.
#
# Script for use by DR RCM to decide if it is okay to remove a given
# network adapter, and to remove it from NAFO control.

# Note that we consider it okay to remove an adapter if it is not part
# of any NAFO group. We ignore any adapter that is not in a NAFO group.

# Usage
#	scpublicnet list
#	scpublicnet check <adapter>
#	scpublicnet remove <adapter>
#	scpublicnet restore <nafo_group:adapter>

# Return zero on success; non-zero otherwise.

# Note that all outputs are written only to stdout.

# Note also that for adapters that NAFO is not concerned with, the check
# and remove operations take no action, and a 0 exit code is returned.



PROG=scpublicnet
PATH=/usr/cluster/bin:/usr/sbin:/sbin:/bin
PNMCONFIG=/etc/cluster/pnmconfig

cmd=$1
target=$2

function usage_exit {
	echo "Usage: $PROG list"
	echo "       $PROG check|remove <adapter>"
	echo "       $PROG restore <nafo_group:adapter>"
	exit 2
}

#
# Prints list of all adapters in NAFO groups, active and standby
#
function get_adapter_list {
	pnmstat -l | grep nafo | awk '{ print $2 }' | sed 's/:/ /g'
}

#
# Checks if an adapter is part of a NAFO group.
# Exit 0 if adapter is not.
#
function check_interest_list {
	adapter=$1

	list=$(get_adapter_list)
	if [ "$list" != "" ]; then
		for i in $list; do
			if [ $i = $adapter ]; then
				return
			fi
		done
	fi
	exit 0
}

#
# Check if an adapter is an active adapter in a NAFO group.
#
function check_active {
	adapter=$1

	pnmstat -l | awk '{ print $1 " " $5 }' |
	while read group active; do
		if [ "$active" = "$adapter" ]; then
			echo "$adapter is active in $group"
			return 1
		fi
	done
	return 0
}

#
# Remove (standby) adapter from NAFO config.
#
function remove_adapter {
	adapter=$1

	# Ask pnmd to remove it
	group=`pnmrtop $adapter 2>/dev/null`
	if [ "$group" = "" ]; then
		return 0
	fi

	pnmset -c $group -o remove $adapter 2>&1
	ret=$?
	echo "$group:$adapter"
	return $ret
}

#
# Rollback function when DR operation fails to remove an adapter.
# Add adapter back to its nafo group.
# Input string is of the form "nafo_group:adapter", e.g. "nafo0:qfe1"
#
function restore_adapter {
	group=`echo $1 | awk -F: '{ print $1 }'`
	adapter=`echo $1 | awk -F: '{ print $2 }'`

	if [ "$group" = "" -o "$adapter" = "" ]; then
		echo "insufficient info to restore"
		return 2
	fi

	pnmset -c $group -o add $adapter 2>&1
	return $?
}


#
# main
#

# Exit error if pnmd is not running
pnmstat >/dev/null 2>&1
if [ $? != 0 ]; then
	echo "pnmd is not running or set up properly"
	exit 2
fi

case "$cmd" in
list)
	echo $(get_adapter_list)
	exit 0
	;;

check)
	if [ "$target" = "" ]; then
		usage_exit
	fi
	check_interest_list $target
	check_active $target
	exit $?
	;;

remove)
	if [ "$target" = "" ]; then
		usage_exit
	fi
	check_interest_list $target
	check_active $target
	ret=$?
	if [ $ret != 0 ]; then
		exit $ret
	fi
	remove_adapter $target
	exit $?
	;;

restore)
	if [ "$target" = "" ]; then
		usage_exit
	fi
	restore_adapter $target
	exit $?
	;;

*)
	usage_exit
esac

