#!/bin/ksh
#
# ident	"@(#)tunhb_check_ccr.sh	1.2	03/12/17 SMI"
#
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

CCR=/etc/cluster/ccr/infrastructure
CLPL_DIR=/etc/cluster/clpl

# Utilities to use
GREP=/usr/bin/grep
WC=/usr/bin/wc
NAWK=/usr/bin/nawk

# I18N
export TEXTDOMAINDIR=/usr/cluster/lib/locale
export TEXTDOMAIN=SUNW_SC_CMD
export LC_COLLATE=C

# Get how many nodes are there in the cluster
NODES_NUM=`${GREP} '^cluster.nodes.[0-9]*.name	' ${CCR} \
	    2>/dev/null | ${WC} -l`

integer i=1 j=1
good_setting="yes"

# Go through all nodes
while [[ $i -le ${NODES_NUM} ]]; do
	# Get the nubmber of the adapters of this node
	node_pat="'^cluster.nodes.${i}.adapters.[0-9]*.name	'"
	ADAPTERS_NUM=`eval "${GREP} ${node_pat} ${CCR} 2>/dev/null | \
	    ${WC} -l"`

	# Go through each adapter of this node
	j=1
	# Beginning of the dapter pattern
	pat_head="^cluster.nodes.${i}.adapters."
	while [[ $j -le ${ADAPTERS_NUM} ]]; do

		# Get the transport type of this adapter
		tr_pat="${pat_head}${j}.properties.transport_type	"
		trtype=`${NAWK} '/'"${tr_pat}"'/ {printf $2}' \
		    ${CCR} 2>/dev/null`

		# Set the adapter pattern tail
		pat_tail=".properties.${trtype}_heartbeat_"

		# Get the device name of this adapter
		dev_pat="${pat_head}${j}.properties.device_name	"
		dev_name=`${NAWK} '/'"${dev_pat}"'/ {printf $2}' \
		    ${CCR} 2>/dev/null`

		# Check the clpl files
		clpl_file="${CLPL_DIR}/SUNW.adapter.${dev_name}.clpl"
		clpl_pat="^adapters.${dev_name}.${trtype}_heartbeat_"

		# Get this adapter's timeout range
		t_max_clpl_pat="${clpl_pat}timeout_v1.max	"
		t_max_clpl=`${NAWK} '/'"${t_max_clpl_pat}"'/ {printf $2}' \
		    ${clpl_file} 2>/dev/null`
		t_min_clpl_pat="${clpl_pat}timeout_v1.min	"
		t_min_clpl=`${NAWK} '/'"${t_min_clpl_pat}"'/ {printf $2}' \
		    ${clpl_file} 2>/dev/null`

		# Get the timeout value for this adapter
		t_pat="${pat_head}${j}${pat_tail}timeout	"
		timeout=`${NAWK} '/'"${t_pat}"'/ {printf $2}' \
		    ${CCR} 2>/dev/null`

		#
		# Check if timeout is within the range
		#
		if [[ ("${timeout}" -gt "${t_max_clpl}") || \
		     ("${timeout}" -lt "${t_min_clpl}") ]]; then
			printf "$(gettext 'Error - Downgrading:\
	Adapter %s on node %s heartbeat timeout: %s out of range\
	supported by earlier release %s - %s.')\n\n" \
	${j} ${i} ${timeout} ${t_min_clpl} ${t_max_clpl}
			good_setting="no"
		fi


		# Get this adapter's quantum range
		q_max_clpl_pat="${clpl_pat}quantum_v1.max	"
		q_max_clpl=`${NAWK} '/'"${q_max_clpl_pat}"'/ {printf $2}' \
		    ${clpl_file} 2>/dev/null`
		q_min_clpl_pat="${clpl_pat}quantum_v1.min	"
		q_min_clpl=`${NAWK} '/'"${q_min_clpl_pat}"'/ {printf $2}' \
		    ${clpl_file} 2>/dev/null`

		# Get the quantum value for this adapter
		q_pat="${pat_head}${j}${pat_tail}quantum	"
		quantum=`${NAWK} '/'"${q_pat}"'/ {printf $2}' \
		    ${CCR} 2>/dev/null`

		#
		# Check if quantum is within the range
		#
		if [[ ("${quantum}" -gt "${q_max_clpl}") || \
		     ("${quantum}" -lt "${q_min_clpl}") ]]; then
			printf "$(gettext 'Error - Downgrading:\
	Adapter %s on node %s heartbeat quantum: %s out of range\
	supported by earlier release %s - %s.')\n\n" \
	${j} ${i} ${quantum} ${q_min_clpl} ${q_max_clpl}
			good_setting="no"
		fi

		
		# Go to next adapter
                j=`expr $j + 1`
	done

	# Go to next node
	i=`expr $i + 1`
done

# If we are here, everything is ok
if [[ "${good_setting}" != "yes" ]]; then
	printf "$(gettext '\
You are doing downgrading, and need to boot into cluster mode and\
use scconf to fix the out of ranged heartbeat settings to the\
default values. Or refer to related documents for details.')\n\n"
	exit 1
else
	exit 0
fi
