#!/bin/ksh

# ident   "@(#)ds_remove_methods.sh 1.5   01/03/28 SMI"

TEXTDOMAIN=ds_remove_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"
_bindir=$(dirname $0)

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

if (( $# != 1 )); then
        gettext "Error: Illegal number of parameters passed!\n"
        exit 1
fi

DS="$1"

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
		if ${HAREG} | /bin/grep -w ${DS} >/dev/null 2>&1
		then
			printf "`gettext 'Data Service %s is currently registered.'`\n" "${DS}"
			printf "`gettext 'Cannot remove'` ${pkgname}\n"
			exit 1
		else
			row="${DS}:${lic_type}:${pkgname}:${rdir}"
			${SCCCD} ${CLUSTER} DS_SUN remove ds_name ${DS}
			if (( $? != 0 )); then
# If we fail on the remove it maybe because another host has already
# removed this row. Lets do another query for this ds. If its not there
# then we are ok and we can continue as normal. If it is there then
# something really strange has occured. Print the message and exit.
			   ${SCCCD} ${CLUSTER} DS_SUN query ds_name "${DS}" \
				>/dev/null 2>&1
			   if (( $? == 0 )); then
			     printf "`gettext 'Unable to delete Sun Data Service %s from the cluster configuration database.'`\n" "${DS}"
			     gettext "Check system console logs for more detailed error messages.\n"
			     exit 1 
			   fi
			fi

		fi
	fi
else
	if egrep "^DS_SUN:${DS}" ${_ccddb} >/dev/null 2>&1
	then
		${CCDADM} -p ${_ccddb}		#get "pure" ccd db
		if (( $? != 0 )); then
			printf "${_ccddb} `gettext 'is corrupted'`\n"
			exit 1
		fi
		sedcmd="/^DS_SUN:${DS}/d"
		sed ${sedcmd} ${_ccddb}.pure > ${_tempfile}
		${CCDADM} -x ${_tempfile}
		cp ${_ccddb} ${_ccddb}.orig
		mv ${_tempfile} ${_ccddb}
		rm -rf ${_ccddb}.pure
	fi
fi

exit 0

