#!/bin/ksh
# ident   "@(#)ds_install_methods.sh 1.6   01/03/28 SMI"

TEXTDOMAIN=ds_install_methods; export TEXTDOMAIN
TEXTDOMAINDIR=/opt/SUNWcluster/locale;  export TEXTDOMAINDIR

PATH=/usr/bin:/usr/sbin:/sbin

_basedir=`pkgparam SUNWsccf BASEDIR`
_productdir=`pkgparam SUNWsccf PRODUCTDIR`
_configdir="$_basedir/etc/opt/$_productdir/conf"
_tempfile=/var/opt/SUNWcluster/run/dev_install_a.$$
_ccddb="ccd.database"
_format="ds_name:lic_type:lic_ver:pkg:rdir:st:sp:ab:stn:spn:abn:fmi:fmst:fmsp:fmck"
_bindir=$(dirname $0)

CLUSTM=${_bindir}/clustm
CCDADM=${_bindir}/ccdadm
SCCCD=${_bindir}/scccd
CLUSTER=$(cat ${basedir}/etc/opt/${_productdir}/conf/default_clustername)

typeset  results

#
########################################################
# function validate_timeout <number>
#
# This function will take a single parameter and check 
# to see:
#
# 1. its a numeric value
# 2. in the range of 0-30000
#
# if either of these conditions are true, or if no value
# is passed in, the default value will be returned.
#
function validate_timeout
{
	typeset -i default_value=60
	typeset -i numeric_value
	typeset numeric_test
	typeset to_value

	to_value="$1"

	if [[ -z "${to_value}" ]]; then
		let numeric_value=${default_value}
	else
		numeric_test=$(print -R ${to_value} | tr -d '[0-9]')
		if [[ -n "${numeric_test}" ]]; then
# if this variable is non zero then the timeout value passed in
# was not numeric. Use the default value of 60 in this case.
			let numeric_value=${default_value}
		else
			(( numeric_value=${to_value} ))
			if (( numeric_value < 0 )) ||
			   (( numeric_value > 30000 )); then
# value is out of range, use default
				let numeric_value=${default_value}
			fi
		fi
	fi

	print $numeric_value
}

# Main script.

if (( $# > 0 )); then
	if (( $# != 5 )); then
		gettext "Error: Illegal number of parameters passed!\n"
		exit 1
	fi
	ds="$1"
	lic_type="$2"
	lic_ver="$3"
	pkgname="$4"
	rdir="$5"
	st=60
	sp=60
	ab=60
	stn=60
	spn=60
	abn=60
	fmst=60
	fmsp=60
	fmi=60
	fmck=60
else
	ds="${DS}"
	lic_type="${LIC_TYPE}"
	lic_ver="${LIC_VER}"
	pkgname="${PKGNAME}"
	rdir="${RDIR}"
	st=$(validate_timeout "${START_TO}")
	sp=$(validate_timeout "${STOP_TO}")
	ab=$(validate_timeout "${ABORT_TO}")
	stn=$(validate_timeout "${START_NET_TO}")
	spn=$(validate_timeout "${STOP_NET_TO}")
	abn=$(validate_timeout "${ABORT_NET_TO}")
	fmst=$(validate_timeout "${FM_START_TO}")
	fmsp=$(validate_timeout "${FM_STOP_TO}")
	fmi=$(validate_timeout "${FM_INIT_TO}")
	fmck=$(validate_timeout "${FM_CHECK_TO}")
fi

cd ${_configdir}

state=$(${CLUSTM} getstate ${CLUSTER} 2>/dev/null)
if (( $? == 0 )); then
	${SCCCD} ${CLUSTER} DS_SUN query ds_name "${ds}" >/dev/null 2>&1
	if (( $? == 0 )); then
		exit 0
	else
		row="${ds}:${lic_type}:${lic_ver}:${pkgname}:${rdir}:${st}:${sp}:${ab}:${stn}:${spn}:${abn}:${fmi}:${fmst}:${fmsp}:${fmck}"
		msg=$(${SCCCD} ${CLUSTER} DS_SUN add "${_format}" ${row})
		if (( $? != 0 )); then
#
# If the add fails its more than likely because this node attempted
# to add the row at the same time as one of the other nodes did. Since
# it appears that its almost impossible for us to catch and check for
# that error message (add.4001), Lets check to see if the row is present, 
# if so we're ok, if not print an error.
                        ${SCCCD} ${CLUSTER} DS_SUN query ds_name "${ds}" \
                                >/dev/null 2>&1
                        if (( $? != 0 )); then
				printf "`gettext 'Unable to add Sun Data Service %s to the cluster configuration database.'`\n" "${ds}"
				gettext "Check system console logs for more detailed error messages.\n"
				exit 1 
			fi

		fi
	fi
else
	if egrep "^DS_SUN:${ds}" ${_ccddb} >/dev/null 2>&1
	then
		exit 0
	else
		${CCDADM} -p ${_ccddb}		#get "pure" ccd db
		if (( $? != 0 )); then
			printf "${_ccddb} `gettext 'is corrupted'`\n"
			exit 1
		fi
		sedcmd="/^DS_SUN_sync/a\\
DS_SUN:${ds}:${lic_type}:${lic_ver}:${pkgname}:${rdir}:${st}:${sp}:${ab}:${stn}:${spn}:${abn}:${fmi}:${fmst}:${fmsp}:${fmck}"
		sed -e "$sedcmd" ${_ccddb}.pure >${_ccddb}.new
		${CCDADM} -x ${_ccddb}.new
		cp ${_ccddb} ${_ccddb}.orig
		mv ${_ccddb}.new ${_ccddb}
		rm -rf ${_ccddb}.pure
	fi
fi
exit 0

