#!/bin/sh
#
#ident  "@(#)hasap_dbms.sh 1.4 98/09/03 SMI"
#
#
# hasap_dbms - HA-SAP command to create dependency on a DBMS data service
#
# The purpose of this command is to add a dependency for the SAP data
# service on either an Oracle or Informix data service.  This is necessary
# since during pkgadd time, we don't know what DBMS the HA-SAP will be
# configured for, and being a Sun data service (hareg -s -r), there are
# no public interface for manipulating defaults.
#
# Note that establishing a dependency for the HA-SAP dataservice is only
# relevant when both the CI and DB dataservices are created on a single
# logical host.  By creating a dependency, the DBMS will always be started
# before the CI instance.
# 
# If DB and CI are configured on two separate logical host, creating
# this dependency is not needed, since the dataservices are on separate
# logical host.  However, in this configuration, the dependency are on
# the logical host (ie. the DB logicalhost must always be imported before
# the CI logicalhost).  To accomplish this, be sure to define using scconf
# the CI logicalhost before the DB logicalhost, because the logicalhost
# import is in the reverse order of definition.
#
# Using the "-r" flag will remove all existing DBMS dependency.
#
# The argument for this command is:
#
# <dbms data service> - can be one of more of oracle, informix or nfs
#
# -r  - Remove all dependency
#

# Get the BASEDIR and PRODUCTDIR settings from the installed pkgs
_basedir_etc=`pkgparam SUNWsccf BASEDIR 2>/dev/null`
_basedir=`pkgparam SUNWsc BASEDIR 2>/dev/null`
_productdir=`pkgparam SUNWsc PRODUCTDIR 2>/dev/null`

cdbpath="${_basedir_etc}/etc/opt/${_productdir}/conf"
CLUSTNAME=`cat ${cdbpath}/default_clustername`

usage() {
	echo "Usage: hasap_dbms [ oracle | informix ] [ nfs ]"
	echo "       hasap_dbms -r"
	exit 2
}

rem_dep="n"
while getopts r flag
do
	case $flag in
		r) rem_dep="y" ;;
		\?) usage ;;
	esac
done
shift `expr $OPTIND - 1`

# 
# First check to make sure the "sap" dataservice is registered
#
haget -f service_is_on -s sap >/dev/null 2>&1
if [ $? -ne 0 ]; then
	echo "Please register the \"sap\" dataservice before running this command"
	exit 1
fi


if [ ${rem_dep} = "y" ]; then
	if [ $# -eq 0 ]; then

		scccd ${CLUSTNAME} DS_DEPSVC_LIST remove ds_name sap
		scccd ${CLUSTNAME} DS_DEPSVC_LIST add "ds_name:service_list" "sap:"
	else

		usage

	fi

elif [ $# -ge 1 ]; then

	dep_svcs="$*"

	#
	# Check if the specified dataservice is registered
	#
	for svc in $dep_svcs; do
		haget -f service_is_on -s $svc >/dev/null 2>&1
		if [ $? -ne 0 ]; then
			echo "Please register the \"$svc\" dataservice before running this command"
			exit 1
		fi
	done

	#
	# Remove any existing dependency list for "sap" first
	#
	scccd ${CLUSTNAME} DS_DEPSVC_LIST remove ds_name sap

	#
	# Add the DBMS as a dependent dataservice for "sap" 
	#
	dep_svcs_list=`echo $dep_svcs | tr ' ' ','`
	scccd ${CLUSTNAME} DS_DEPSVC_LIST add "ds_name:service_list" "sap:$dep_svcs_list"

else
	usage
fi

exit 0

