#!/usr/bin/sh

###############################################################################
#hacmp_config.sh
#Detect HACMP cluster and configure PBX component. 
#Cluster systems are auto detected.
###############################################################################

#
#initialize variables 
#
#PBX root path.
PBX_ROOT=/opt/VRTSpbx/
HACMP_CLUSTER_PATH=${PBX_ROOT}/bin/cluster
STAGE2_CMDFILE=${HACMP_CLUSTER_PATH}/hacmp_postconfig.sh
host_name=`hostname`
HACMP_CLUSTER=false
NODES=""
ECHO="echo"
SPACE=' '
TAB=`${ECHO} ' ' | tr ' ' '\011'`
ROLLBACK_CMDFILE=${HACMP_CLUSTER_PATH}/rollback.sh

#  Need to be root.
ISROOT=`id | egrep "^uid=0\("`
if [ "${ISROOT}" = "" ] ; then
	${ECHO} ""
	${ECHO} "$0 needs root permission. Please login as root.\n"
	${ECHO} ""
	exit 2 
fi

#check the type of OS  
OS_TYPE=`uname -s`
OS_VERSION=`uname -r`
HOST=`uname -n | sed 's/\..*$//'`

###############################################################################
# hacmp_config
# Show Usage of the script.
###############################################################################
display_usage(){
  	${ECHO} "Usage hacmp_config :"
	${ECHO} " -c/-u : Configure/Un-configure PBX"
	${ECHO} " Following options should be specified for -c option"
	${ECHO} "   -r    : Name of Server resource"
	${ECHO} "   -g    : Name of PBX Resource Group"
	${ECHO} "   -n    : Node name on which PBX should get configure"
	${ECHO} " Following options should be specified for -u option"
	${ECHO} "   -r    : Name of Server resource"
	${ECHO} "   -g    : Name of PBX Resource Group"
}


###############################################################################
#hacmp_config
#run the HACMP config utility.
###############################################################################
hacmp_config () {
        delete_tmp_file=0
	${HACMP_CLUSTER_PATH}/pbx_hacmp_config $*
	rc=$?
	case $rc in
	    0)
		${ECHO} "HACMP Configuration Succesful\n"
                ;;
	    1)
		${ECHO} "***** HACMP Configuration failed! *****\n"
		delete_tmp_file=1
                ;;
	    2)
		${ECHO} "***** Invalid Options *****\n"
		delete_tmp_file=1
                display_usage
		;;
	    *) 
		delete_tmp_file=1
                display_usage
	esac
         
        if [ $delete_tmp_file == 1 ]; then
           echo "Deleting Temp files"
           if [ -f "${ROLLBACK_CMDFILE}" ]; then
             rm -f ${ROLLBACK_CMDFILE}
           fi
           if [ -f "${STAGE2_CMDFILE}" ]; then
            rm -f ${STAGE2_CMDFILE}
           fi           
           exit 1 
       fi
}
##############################################################################
#Onlines the PBX in the cluster. 
##############################################################################
online() { 
	if [ "${HACMP_CLUSTER}" = "true" ] ; then 
		if [[ -x "$STAGE2_CMDFILE" ]] ; then
		   ${ECHO} "Configuring PBX Resource Group and bringing it online...\n"
		   $STAGE2_CMDFILE
		   if [ $? != 0 ] ; then  
                      echo "***** Problem configuring the PBX Resource group ***** \n"
       		      rm -f $STAGE2_CMDFILE
                      if [ -f "${ROLLBACK_CMDFILE}" ]; then
                        rm -f ${ROLLBACK_CMDFILE}
                      fi
		      exit 1
		   fi
   		fi   
		# TODO - decide whether/how to bring online if already configured
	fi 
	#TODO we should check the status using cluster active before we make
	#this determination. 
	${ECHO} "PBX is online... \n" 
} 

##############################################################################
##Start of Main Program 
##############################################################################
if [ ${OS_TYPE} != "AIX" ] ; then 
exit 1
fi

case $1 in
	-c)
		echo "Configuring PBX for HACMP\n"
		if [ -d /usr/es/sbin/cluster ]; then
		      HACMP_CLUSTER=true
		      hacmp_config $*
                      online
                      echo "Online complete..." 
                      if [ -f "${ROLLBACK_CMDFILE}" ]; then
                         rm -f ${ROLLBACK_CMDFILE}
                      fi
                      if [ -f "${STAGE2_CMDFILE}" ]; then
                         rm -f ${STAGE2_CMDFILE}
                      fi           
                      exit 0  
	        else # HACMP not found  
		      ${ECHO} "***** Cannot configure PBX - HACMP not detected.\n"
		      exit 1
		fi
		;;
	-u)
		echo "Un-Configuring PBX for HACMP\n"
		${HACMP_CLUSTER_PATH}/pbx_hacmp_config $*
                if [ -f "${ROLLBACK_CMDFILE}" ]; then
                    rm -f ${ROLLBACK_CMDFILE}
                fi
                if [ -f "${STAGE2_CMDFILE}" ]; then
                   rm -f ${STAGE2_CMDFILE}
                fi           
                rc=$?
	        case $rc in
                0)
                   ${ECHO} "HACMP Un-Configuration Succesful\n"
                   exit 0 
                   ;;
                1)
                   ${ECHO} "***** HACMP Un-Configuration failed! *****\n"
                   exit ${rc}
                   ;;
               2)
                   ${ECHO} "***** Invalid Options *****\n"
                   display_usage
                   exit 1
                   ;;
               *)
                  display_usage
                  exit 1
                  esac
		;;
	*)
		display_usage
		exit 1
esac 
