#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/migrate.sh 1.16 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,2004 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)86	1.16 src/43haes/usr/sbin/cluster/events/migrate.sh, hacmp.events, 61haes_r714 12/13/04 08:27:36
#
#########################################################################
#                                                                       #
#       Name:           migrate                                         #
#                                                                       #
#       Description:    This event script is called when a migration    #
#                       from HACMP Classic has been detected.           #
#                       or when a node crashes during the migration     #
#                       process and the /etc/firstboot file was created #
#                       certain portions of this event script are       #
#                       skipped if the firstboot flag is passed in      #
#                                                                       #
#       Called by:      Recovery Driver                                 #
#                                                                       #
#       Calls to:       None                                            #
#                                                                       #
#       Arguments:      Nodename                                        #
#                       Firstboot                                       #
#                                                                       #
#       Returns:        0       success                                 #
#                       1       failure                                 #
#                       2       bad argument                            #
#                                                                       #
#########################################################################

PROGNAME=$(basename ${0})

PATH="$($(dirname ${0})/../utilities/cl_get_path all)"
export PATH="/usr/sbin/rsct/bin:${PATH}"

set -a
eval $(cllsparam -n $LOCALNODENAME)
set +a

[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.16'
HA_DIR="$(cl_get_path)"

cl_migcheck "HACMP"
[ $? -ne 1 ] && exit 0

NODENAME=$1
FIRSTBOOT=$2

[[ -n "$1" ]] && NODENAME=$1

let success=0
let failure=1
let STATUS=success
STARTDIR="${PWD}"
OLDODMDIR="${ODMDIR}"

###############################################################################
# "odmcopy"  HACMP HACMPdaemons  to  HACMP/ES HACMPdaemons.
# -----------------------------------------------------------

if [[ ! -f /etc/objrepos/HACMPdaemons || ! -f /etc/es/objrepos/HACMPdaemons ]] 
then
	cl_log 5103 "\n${PROGNAME}: WARNING: Can not copy resource locator database entries from HACMP to HACMP/ES.\n" ${PROGNAME}
fi 
tmp1="/tmp/HACMPdaemons_tmp$$"
export ODMDIR='/etc/objrepos'
odmget HACMPdaemons > $tmp1
if (( $? != $success )) ; then
	cl_log 5103 "\n${PROGNAME}: WARNING: Can not copy resource locator database entries from HACMP to HACMP/ES.\n" ${PROGNAME}
	[[ -f $tmp1 ]] && rm -f $tmp1
fi
if [[ -s $tmp1 ]] ; then
	cl_echo 5104 "\n${PROGNAME}: Copying resource locator database entries from HACMP to HACMP/ES...\n" ${PROGNAME}
	export ODMDIR='/etc/es/objrepos'
	odmdelete -o HACMPdaemons >/dev/null
	odmadd $tmp1
	if (( $? != $success )) ; then
		cl_log 5103 "\n${PROGNAME}: WARNING: Can not copy resource locator database entries from HACMP to HACMP/ES.\n" ${PROGNAME}
		[[ -f $tmp1 ]] && rm -f $tmp1
	fi
fi
[[ -f $tmp1 ]] && rm -f $tmp1
export ODMDIR="${OLDODMDIR}"

###############################################################################
# Stop the HACMP (classic) cluster services. (only if not called by the first
# boot utility

if [ -z "${FIRSTBOOT}" ]
then

	cl_echo 5105 "\n${PROGNAME}: Stopping HACMP cluster services...\n" ${PROGNAME}
	/usr/sbin/cluster/utilities/clstop -y -N -s -f
	if (( $? != $success )) 
	then
		cl_log 5106 "\n${PROGNAME}: WARNING: Unable to stop HACMP cluster services.\n" ${PROGNAME}
	fi

fi
###############################################################################

###############################################################################
# Wait for the HAS clsmuxpd and clinfo daemons to be stopped before proceeding.

if [ -z "${FIRSTBOOT}" ]
then

	while (! clcheck_server clsmuxpd)
	do
		cl_log 5121 "\n${PROGNAME}: Waiting for Cluster SMUX peer daemon (clsmuxpd) to stop.:\n" ${PROGNAME}
		sleep 1
	done

	while (! clcheck_server clinfo)
	do
		cl_log 5122 "\n${PROGNAME}: Waiting for Cluster information daemon (clinfo) to stop.:\n" ${PROGNAME}
		sleep 1
	done

fi
###############################################################################

###############################################################################
# Start the HAES clinfo daemon.

if [ -z "${FIRSTBOOT}" ]
then

	ipcrm -M 0x00006000
	startsrc -s clinfoES
	if (( $? != $success )) 
	then
	    let STATUS=failure
  	    cmd="startsrc -s clinfoES"
	    cl_log 5110 "\n${PROGNAME}: ERROR: Unable to start the clinfoES process!\n" ${PROGNAME} "clinfoES"
	    cl_log 5123 "\n${PROGNAME}: To start the clinfoES process, issue the following command:\n" ${PROGNAME} "clinfoES"
	    cl_log 5124 "\n${cmd}\n" ${cmd}
	fi

fi
###############################################################################

###############################################################################
# Deactivate the HACMP (classic) godm subserver.

sub='/usr/sbin/cluster/godmd'
cmd="chsubserver -d -v godm -p tcp -t stream -w nowait -u root -g ${sub}"

# Execute the command
${cmd}
if (( $? != $success )) 
then
    let STATUS=failure
    cl_log 5125 "\n${PROGNAME}: ERROR: Unable to unconfigure the HACMP global ODM subserver!\n" ${PROGNAME}
    cl_log 5126 "\n${PROGNAME}: To unconfigure the HACMP global ODM subserver, issue the following command:\n" ${PROGNAME}
    cl_log 5124 "\n${cmd}\n" ${cmd}
fi

# Force the file to be synced
syscall open /etc/inetd.conf 2 0 \; fsync \$0 \; close \$0
###############################################################################

###############################################################################
# Activate the HAES godm subserver.

sub='/usr/es/sbin/cluster/godmd'
cmd="chsubserver -a -v godm -p tcp -t stream -w nowait -u root -g ${sub}"

# Execute the command
${cmd}
if (( $? != $success )) 
then
    let STATUS=failure
    cl_log 5127 "\n${PROGNAME}: ERROR: Unable to configure the HACMP/ES global ODM subserver!\n" ${PROGNAME}
    cl_log 5128 "\n${PROGNAME}: To configure the HACMP/ES global ODM subserver, issue the following command:\n" ${PROGNAME}
    cl_log 5124 "\n${cmd}\n" ${cmd}
fi

# Force the file to be synced
syscall open /etc/inetd.conf 2 0 \; fsync \$0 \; close \$0

# Refresh inetd
refresh -s inetd
###############################################################################

export ODMDIR="${OLDODMDIR}"
cd $STARTDIR

exit $STATUS

