#!/bin/sh
#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# ident	"@(#)scpostconfig	1.2	04/09/21 SMI"
#

SC_SCCONF=/usr/cluster/bin/scconf
SC_QUORUMCONFIG=/usr/cluster/lib/sc/scquorumconfig
POSTCONFIGFILE=/etc/cluster/ccr/postconfig

tmp_logfile="/tmp/postconfig.log.$$"

get_installmode()
{
        (
                LC_ALL=C; export LC_ALL
                mode=`${SC_SCCONF} -p | sed -n 's/^Cluster install mode:[   ]*\([^ ]*\).*/\1/p'`
                if [ -n "${mode}" ] && [ "${mode}" = "enabled" ]
		then
                        echo 1
                else
                        echo 0
                fi
        )

        return 0
}

# test whether we are a cluster and exit if not a cluster
/usr/sbin/clinfo > /dev/null 2>&1
if [ $? -ne 0 ]
then
	exit 0
fi

installmode=`get_installmode`
nodename=`/sbin/uname -n`

if [ ${installmode} -eq 1 ] && [ -f ${POSTCONFIGFILE} ] 
then
	tasks=`grep '^task\.' ${POSTCONFIGFILE} | awk -F. '{print $2}' | sort | uniq`

	for task in ${tasks}; do
		case "${task}" in
		'quorum')

			# Create quorum devices in 2 node clusters and do only
			# cluster initialization in >2 or 1 node clusters.
			nodecount=`grep '^task.' ${POSTCONFIGFILE} | awk -F. '{ if ($2 == "quorum") print $3 }' | wc -l`
			if [ ${nodecount} -eq 2 ]
			then
        			${SC_QUORUMCONFIG} > ${tmp_logfile} 2>&1
				exit_status=$?
				if [ ${exit_status} -eq 1 ]
				then
					/usr/bin/logger -p daemon.err -t SCPOSTCONFIG -f ${tmp_logfile}
					/usr/bin/logger -p daemon.err -t SCPOSTCONFIG "The quorum configuration task encountered a problem on node ${nodename}, manual configuration by using scsetup(1M) might be necessary"
					cat ${tmp_logfile}
					echo "The quorum configuration task encountered a problem on node ${nodename}, manual configuration by using scsetup(1M) might be necessary"

				elif [ ${exit_status} -eq 0 ]
				then
					/usr/bin/logger -p daemon.notice -t SCPOSTCONFIG -f ${tmp_logfile}
					/usr/bin/logger -p daemon.notice -t SCPOSTCONFIG "The quorum configuration task succeeded on node ${nodename}"
					cat ${tmp_logfile}
					echo "The quorum configuration task succeeded on node ${nodename}"

				fi
			else
        			${SC_QUORUMCONFIG} -i > ${tmp_logfile} 2>&1
				exit_status=$?
				if [ ${exit_status} -eq 1 ]
				then
					/usr/bin/logger -p daemon.err -t SCPOSTCONFIG -f ${tmp_logfile}
					/usr/bin/logger -p daemon.err -t SCPOSTCONFIG "Cluster initialization encountered a problem on node ${nodename}, manual initialization by using scsetup(1M) might be necessary"
					cat ${tmp_logfile}
					echo "Cluster initialization encountered a problem on node ${nodename}, manual initialization by using scsetup(1M) might be necessary"

				fi
			fi
			rm -f ${tmp_logfile}
			break
        		;;
		*)
			/usr/bin/logger -p daemon.err -t SCPOSTCONFIG "Unsupported task ${task} in ${POSTCONFIGFILE}"
			break
        		;;
		esac
	done
fi
