#!/sbin/sh

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

PDBCONF=/etc/opt/SUNWcluster/conf
CCDTMP=/var/opt/SUNWcluster/run/ccd.database
CCDADM=/opt/SUNWcluster/bin/ccdadm
ERRFILE=/var/opt/SUNWcluster/run/ccdinstall.errors
AWKMATCH=/var/opt/SUNWcluster/run/awkmatch
AWKDIFF=/var/opt/SUNWcluster/run/awkdiff

# error codes
PURE_ERROR=1
DUP_ERROR=2
NOTFOUND_ERROR=3
USAGE_ERROR=4
OTHER_ERROR=5

Usage() {
	echo "Usage: ccdinstall [-i] add component_file"
	echo "       ccdinstall [-i] rem key component_file"
	exit ${USAGE_ERROR}
}

Addccd() {
	# verify that datafile exists and is pure
	${CCDADM} -p ${datafile} > ${ERRFILE} 2>&1
	if [ $? != 0 ]; then
		echo "Error: ${datafile} is not a valid ccd file."
		/bin/cat ${ERRFILE}
		exit_error=${PURE_ERROR}
		return
	fi
	/bin/rm -f ${datafile}.pure

	# if no ccdfile, create with new entries
	if [ ! -f ${CCDFILE} ]; then
		/bin/cp ${datafile} ${CCDFILE}
		${CCDADM} -x ${CCDFILE}
		return
	fi

	# verify that ccdfile is pure
	${CCDADM} -p ${CCDFILE} > ${ERRFILE} 2>&1
	if [ $? != 0 ]; then
		echo "Error: ${CCDFILE} is not a valid ccd file."
		/bin/cat ${ERRFILE}
		exit_error=${PURE_ERROR}
		return
	fi
	/bin/rm -f ${CCDFILE}.pure

	# append datafile to ccdfile in /var/opt/SUNWcluster/run directory
	/bin/awk -F':' '{if ($1 != "CCD_CHK") print}' ${CCDFILE} > ${CCDTMP} || \
		(exit_error=${OTHER_ERROR} ; return)
	/bin/cat ${datafile} >> ${CCDTMP} || \
		(exit_error=${OTHER_ERROR} ; return)

	# verify that the new ccdfile is pure
	${CCDADM} -p ${CCDTMP} > ${ERRFILE} 2>&1
	if [ $? != 0 ]; then
		echo "Error: ${datafile} can not be appended to the CCD file \
because the resulting file would contain duplicate format rows."
		/bin/cat ${ERRFILE}
		exit_error=${DUP_ERROR}
		return
	fi
	/bin/rm -f ${CCDTMP}.pure

	/bin/mv ${CCDTMP} ${CCDFILE} || \
		(exit_error=${OTHER_ERROR} ; return)
	${CCDADM} -x ${CCDFILE} || \
		(exit_error=${OTHER_ERROR} ; return)
}

Remccd() {
	# if no ccdfile, do nothing
	if [ ! -f ${CCDFILE} ]; then
		exit_error=0
		return
	fi

	# verify that ccdfile is pure
	${CCDADM} -p ${CCDFILE} > ${ERRFILE} 2>&1
	if [ $? != 0 ]; then
		echo "Error: ${CCDFILE} is not a valid ccd file."
		/bin/cat ${ERRFILE}
		exit_error=${PURE_ERROR}
		return
	fi
	/bin/rm -f ${CCDFILE}.pure

	# verify that format row for key is in ccdfile
	fmtfound=`/bin/awk -F':' -f ${AWKMATCH} ${CCDFILE} | /bin/wc -l`
	if [ $fmtfound = "0" ]; then
		exit_error=0
		return
	fi

	# move rows with key from ccdfile to datafile
	/bin/awk -F':' '{if ($1 != "CCD_CHK") print}' ${CCDFILE} > ${CCDTMP} || \
		(exit_error=${OTHER_ERROR} ; return)
	/bin/awk -F':' -f ${AWKMATCH} ${CCDTMP} >> ${datafile} || \
		(exit_error=${OTHER_ERROR} ; return)
	/bin/awk -F':' -f ${AWKDIFF} ${CCDTMP} > ${CCDFILE} || \
		(exit_error=${OTHER_ERROR} ; return)
	/bin/rm -f ${CCDTMP}
	${CCDADM} -x ${CCDFILE} || \
		(exit_error=${OTHER_ERROR} ; return)
}

# Just do it!

if [ $# -lt 2 ]; then
	Usage
fi

if [ $1 = "-i" ]; then
	CCDFILE=${PDBCONF}/ccd.database.init
	shift
else
	CCDFILE=${PDBCONF}/ccd.database
fi

cmd=$1
shift

case ${cmd} in
	add) if [ $# != 1 ]; then
		Usage
	     fi
	     datafile=$1 ;;
	rem) if [ $# != 2 ]; then
		Usage
	     fi
	     key=$1
	     datafile=$2 ;;
	*)   Usage ;;
esac

# create awk scripts for add/remove commands
echo '{if ($1 == "'${key}_fmt'" || $1 == "'${key}_idx'" || $1 == "'${key}_sync'" || $1 == "'${key}'") print}' > ${AWKMATCH}
echo '{if ($1 != "'${key}_fmt'" && $1 != "'${key}_idx'" && $1 != "'${key}_sync'" && $1 != "'${key}'") print}' > ${AWKDIFF}

exit_error=0

case ${cmd} in
	add)		Addccd ;;
	rem)		Remccd ;;
	*)		Usage ;;
esac

rm -f ${AWKMATCH} ${AWKDIFF}
if [ -f ${ERRFILE} ]; then
	/bin/rm -f ${ERRFILE}
fi

exit ${exit_error}
