#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/events/migrate_complete.sh 1.29.1.20 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1999,2005 
# 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 
# @(#)87	1.29.1.20 src/43haes/usr/sbin/cluster/events/migrate_complete.sh, hacmp.events, 61haes_r714 6/29/05 18:55:31
#
#########################################################################
#                                                                       #
#       Name:           migrate_complete                                #
#                                                                       #
#       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   (always)                      #
#                       1       failure                                 #
#                                                                       #
#########################################################################

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.29.1.20'
HA_DIR="$(cl_get_path)"

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

NODENAME=$1
FIRSTBOOT=$2

[[ -n "$1" ]] && NODENAME=$1
STARTDIR="${PWD}"
OLDODMDIR="${ODMDIR}"
let success=0
let failure=1

###############################################################################
# Any filesets that are in this list and have been installed will be 
# deinstalled.

set -A classic_filesets \
cluster.vsm.server \
cluster.taskguides.shrvolgrp \
cluster.msg.en_US.client \
cluster.msg.en_US.server \
cluster.msg.En_US.client \
cluster.msg.En_US.server \
cluster.msg.ja_JP.client \
cluster.msg.ja_JP.server \
cluster.msg.Ja_JP.client \
cluster.msg.Ja_JP.server \
cluster.cspoc.dsh \
cluster.cspoc.rte \
cluster.cspoc.cmds \
cluster.base.client.rte \
cluster.base.client.lib \
cluster.base.client.utils \
cluster.base.server.rte \
cluster.base.server.diag \
cluster.base.server.events \
cluster.base.server.utils \
cluster.adt.client.demos \
cluster.adt.client.include \
cluster.adt.client.samples.clinfo \
cluster.adt.client.samples.libcl \
cluster.adt.client.samples.clstat \
cluster.adt.client.samples.demos \
cluster.adt.server.demos \
cluster.adt.server.samples.demos \
cluster.adt.server.samples.images \
cluster.hc.rte \
cluster.clvm.rte

# Copy the default pager configuration file before deinstall
# ----------------------------------------------------------
PAGERFILE=/usr/sbin/cluster/samples/pager/sample.txt
ESPAGERFILE=/usr/es/sbin/cluster/samples/pager/sample.txt
cp -f ${PAGERFILE} ${ESPAGERFILE} > /dev/null 2>&1

# Copy the NFS exports file (if exists)
# ----------------------------------------------------------
EXPORTFS_FILE=/usr/sbin/cluster/etc/exports
EXPORTFS_ES_FILE=/usr/es/sbin/cluster/etc/exports
cp -f ${EXPORTFS_FILE} ${EXPORTFS_ES_FILE} > /dev/null 2>&1

# Make a list of the installed HACMP (classic) lpps.
# ----------------------------------------------------
unset lslpplist
lslpplist=$(lslpp -LIcq "cluster*"  | awk -F\: '{print $2}' 2>/dev/null)

# Compose the list of installed lpps to deinstall.
# --------------------------------------------------
unset deinstlist
for lpp in ${lslpplist}
do
    echo ${classic_filesets[*]} | grep -w "${lpp}" >/dev/null 2>&1
    if (( $? == 0 )) 
	then
		# If found it, add it to deinstall list.
		deinstlist="${deinstlist} ${lpp}"
    fi
done

if [[ -n ${deinstlist} ]]
then
	x=$(echo ${deinstlist} | sed "#^ ##")
	deinstlist="${x}"
fi

if [[ -n ${deinstlist} ]]
then
	# Uninstall HACMP classic lpps.
	cl_echo 5112 "\n${PROGNAME}: Uninstalling HACMP filesets...\n" ${PROGNAME}
	installp -gu ${deinstlist}

	if (( $? != $success )) 
	then
		cl_log 5113 "\n${PROGNAME}: WARNING: Unable to uninstall HACMP filesets.\n" ${PROGNAME}
	fi
fi

# ####################################################
# Re-Add IPAT stuff if needed
# ####################################################

# Get the local node name from ODM if possible
ODMDIR="/etc/${HA_DIR}/objrepos"
LOCALNODENAME=$(odmget HACMPcluster 2>/dev/null | \
           grep -w nodename 2>/dev/null | \
           cut -d\" -f2 2>/dev/null)

# If you got it, add the IPAT stuff back if needed
if [ ! -z "$LOCALNODENAME" ]
then
	# Remove old instances
	clchipat false

	# See if any boot addresses are configured
	boot_adapters=$(cllsif -ci $LOCALNODENAME | grep :boot: | cut -d: -f1)

	# Only add IPAT to nodes w/boot adapters
	if [ ! -z "${boot_adapters}" ]
	then
		# add the new ONLY if we aren't run from firstboot
		# this will prevent an unreachable node during syncronization
		[ -z "${FIRSTBOOT}" ] && clchipat true
    fi

	# Force the files to be synced
	syscall open /etc/rc.net 2 0 \; fsync \$0 \; close \$0
	syscall open /etc/inittab 2 0 \; fsync \$0 \; close \$0

	# Re-examine the /etc/inittab file using init commands

	# NOTE:  This is here to help workaround an AIX defect #270490
	# in which sporadically, the init daemon was ignoring the last
	# line of /etc/inittab which had the effect of hanging a node
	# trying to come up (w/IPAT configured) after a NxN migration
	# takes place.  this command causes the init daemon to re-examine 
	# the file, according to the AIX docs.  the bug is sporadic and
	# hard to reproduce, so this line was added as extra insulation
	# from the problem.
	telinit q
fi

# ****************************************************
#   Start of special handling for HAES smit pages.
# ****************************************************

# Install cluster.es smit pages.
# --------------------------------
lpp="cluster.es"
LPPDIR="/usr/lpp/${lpp}"
export ODMDIR="/usr/lib/objrepos"

set -A smitpage \
server.rte.base_smit \
server.rte.cspoc_smit

for page in ${smitpage[*]}
do
	# First run the odmdel file if one exists.
	# -----------------------------------------
	DELFILE="${LPPDIR}/${lpp}.${page}.odmdel.NxN_MIG"
	if [[ -f ${DELFILE} ]]; then
	    if [[ ! -x ${DELFILE} ]]; then
		chmod 755 ${DELFILE}
		if (( $? != $success )) ; then
		    cl_log 5117 "${PROGNAME}: WARNING: Can not change permissions for ${DELFILE}." ${PROGNAME} ${DELFILE}
		fi
	    fi
	    ${DELFILE}
	    if (( $? != $success )) ; then
		cl_log 5116 "${PROGNAME}: WARNING: Can not run ${DELFILE}." ${PROGNAME} ${DELFILE}
	    else
		rm -f ${DELFILE}
		if (( $? != $success )); then
		    cl_log 5118 "${PROGNAME}: WARNING: Can not remove ${DELFILE}." ${PROGNAME} ${DELFILE}
		fi
	    fi
	fi

	# Run the odmadd file.
	# --------------------------
	ADDFILE="${LPPDIR}/${lpp}.${page}.odmadd.NxN_MIG"
	if [[ -f ${ADDFILE} ]]
	then
		if [[ ! -r ${ADDFILE} ]]
		then
			chmod 644 ${ADDFILE}
			if (( $? != $success )) 
			then
				cl_log 5117 "\n${PROGNAME}: WARNING: Unable to change permissions for ${ADDFILE}.\n" ${PROGNAME} ${ADDFILE}
			fi
		fi
		odmadd ${ADDFILE}
		if (( $? != $success )) 
		then
			cl_log 5116 "\n${PROGNAME}: WARNING: Unable to run ${ADDFILE}.\n" ${PROGNAME} ${ADDFILE}
		else
			rm -f ${ADDFILE}
		fi
	else
		cl_log 5116 "\n${PROGNAME}: WARNING: Unable to run ${ADDFILE}.\n" ${PROGNAME} ${ADDFILE}
	fi
done

# ****************************************************
#   End of special handling for HAES smit pages.
# ****************************************************

# ****************************************************
#   Start of special handling for history files.
#   We want to keep the old ones, so
#   if they exist, just move and rename them
# ****************************************************
OLDHDIR=/usr/sbin/cluster/history
NEWHDIR=/usr/es/sbin/cluster/history
if [[ -d ${OLDHDIR} ]]
then
        cd ${OLDHDIR}
        HFILES=$(ls -a)
        HFILES=$(echo ${HFILES} | sed "s/\.\. //g")
        HFILES=$(echo ${HFILES} | sed "s/\. //g")
        for file in ${HFILES}
        do
                case ${file} in
                        \.*)
                                mv ${file} "${NEWHDIR}/.has${file}" ;;
                        *)
                                mv ${file} "${NEWHDIR}/has.${file}" ;;
                esac
        done
fi
# ****************************************************
#   End of special handling for history files.
# ****************************************************

# Create symbolic links in the classic locations to the HACMP/ES stuff.
# If the classic version of any of the directories listed exists, 
#    this block cds to them, gets a list of their contents minus dotfiles and
#    anything with 'snapshots' in it, and issues and 'rm -rf' on them.
# Then it cds to the haes directory and for each file, directory, etc. that it
#    finds, if that item doesn't exist on the classic side, a link is put 
#    on the classic side.
# -----------------------------------------------------------------------
for ESDIR in /usr/es/sbin/cluster /usr/es/lpp/cluster /usr/es/include/cluster 
do
    LNDIR=$(echo ${ESDIR} | sed "s:/es/:/:")
    if [[ -d ${LNDIR} && -x ${LNDIR} ]]
    then
        cd ${LNDIR}
        for file in $(ls -d * | egrep -v "^\.|snapshots")
        do
            rm -rf ${file} 2>/dev/null
        done
    else
	mkdir -p ${LNDIR} 2>/dev/null
    fi

    cd ${ESDIR}
    for i in `ls -d *`
    do
       if [[ ! -d ${LNDIR}/${i} ]]; then
          ln -s ${ESDIR}/${i} ${LNDIR} 2>/dev/null
       fi
    done
done

# ****************************************************
#  Begin adding sysck/TCB entry for clinfo link
# ****************************************************
INVFILE='/tmp/cluster.es.client.rte.inventory'
cat > ${INVFILE} <<EOF
/usr/sbin/cluster/clinfo:
          owner = root
          group = system
          mode = TCB,SUID,755
          type = SYMLINK
          target = /usr/es/sbin/cluster/clinfo
          class = apply,inventory,cluster.es.client.rte
EOF

sysck -i -f ${INVFILE} cluster.es.client.rte

TCBCHECK=$(odmget -q attribute=TCB_STATE PdAt | grep -w deflt | cut -d"\"" -f2)
if [[ $TCBCHECK = "tcb_enabled" ]]
then
  tcbck -a -f ${INVFILE}
fi
rm -f ${INVFILE} 

# ****************************************************
#  End adding sysck/TCB entry for clinfo link
# ****************************************************


# Setup symbolic link for Xclstat in default location.
# ------------------------------------------------------
ESFILE='/usr/es/sbin/cluster/samples/clstat/Xclstat'
LNFILE='/usr/sbin/cluster/samples/clstat/Xclstat'
LNDIR="${LNFILE%/*}"

if [[ ! -d ${LNDIR} ]]
then
	mkdir -p ${LNDIR}
	if (( $? != $success ))
 	then
		cl_log 5119 "\n${PROGNAME}: WARNING: Unable to create directory ${LNDIR}.\n" ${PROGNAME} ${LNDIR}
	fi
fi

if [[ ! -f ${LNFILE} ]]
then
	if [[ -f ${ESFILE} ]]
	then
		ln -s ${ESFILE} ${LNFILE}
		if (( $? != $success ))
		then
			cl_log 5114 "\n${PROGNAME}: WARNING: Unable to create symbolic link ${LNFILE}.\n" ${PROGNAME} ${LNFILE}
		fi
	fi
fi

# Handle the libs a little more preservingly.
# ---------------------------------------------
for ESDIR in /usr/es/lib
do
	LNDIR=$(echo ${ESDIR} | sed "s:/es/:/:")
	if [[ -d ${ESDIR} ]]
	then
		cd ${ESDIR}
		for file in $(ls | grep -v '^\.')
		do
			ESFILE="${ESDIR}/${file}"
			LNFILE="${LNDIR}/${file}"
			[ -L "${LNFILE}" ] && rm -f ${LNFILE}
			if [[ ! -f "${LNFILE}" ]]
			then
				ln -s ${ESFILE} ${LNFILE}
				if (( $? != $success )) 
				then
					cl_log 5114 "\n${PROGNAME}: WARNING: Unable to create symbolic link ${LNFILE}.\n" ${PROGNAME} ${LNFILE}
				fi
			fi
		done
	fi
done

# Link msg catalogs.
# --------------------
for msgset in en_US En_US ja_JP Ja_JP
do
	ESDIR="/usr/es/lib/nls/msg/${msgset}"
	LNDIR="/usr/lib/nls/msg/${msgset}"
	if [[ -d ${ESDIR} ]]
	then
		cd ${ESDIR}
		for file in $(ls | grep -v '^\.')
		do
			ESFILE="${ESDIR}/${file}"
			LNFILE="${LNDIR}/${file}"
			[ -L "${LNFILE}" ] && rm -f ${LNFILE}
			if [[ ! -f "${LNFILE}" ]]
			then
				ln -s ${ESFILE} ${LNFILE}
				if (( $? != $success )) 
				then
					cl_log 5114 "\n${PROGNAME}: WARNING: Unable to create symbolic link ${LNFILE}.\n" ${PROGNAME} ${LNFILE}
				fi
			fi
		done
	fi
done

# Remove inittab entries that could be left from HAS 4.5.0.0
# ----------------------------------------------------------
chsubserver -d -v clver -p tcp -t stream -w nowait -u root \
-g /usr/sbin/cluster/diag/clver clver -d > /dev/null 2>&1

# Now for the odms.
# -------------------
ESDIR='/etc/es/objrepos'
LNDIR=$(echo ${ESDIR} | sed "s:/es/:/:")

if [[ -d ${ESDIR} ]]
then
	cd ${LNDIR}
	rm -f ${LNDIR}/HACMP* 2>/dev/null
	cd ${ESDIR}
	for file in $(ls -d HACMP*)
	do
		ESFILE="${ESDIR}/${file}"
		LNFILE="${LNDIR}/${file}"
		ln -s ${ESFILE} ${LNFILE}
		if (( $? != $success )) 
		then
			cl_log 5114 "\n${PROGNAME}: WARNING: Unable to create symbolic link ${LNFILE}.\n" ${PROGNAME} ${LNFILE}
		fi
	done
else
	cl_log 5120 "\n${PROGNAME}: WARNING: Directory ${ESDIR} does not exist.\n" ${PROGNAME} ${ESDIR}
fi

#
# add the link for Xclstat 
#
ln -sf /usr/es/sbin/cluster/samples/clstat/Xclstat /usr/lib/X11/app-defaults/Xclstat

# add link for /etc/cluster

ln -sf /usr/es/sbin/cluster/etc/hacmp.clean /etc/cluster/hacmp.clean
ln -sf /usr/es/sbin/cluster/etc/hacmp.term /etc/cluster/hacmp.term

# Call cllog to migrate the entries in /etc/syslog.conf.
# We change both the comment from "HACMP for AIX" to "HACMP/ES for AIX"
# and rewrite the cluster.log entries in syslog.conf with the values
# from the HAES HACMPlogs ODM.  If the directory for cluster.log was
# like "/usr/adm" then it is converted to "/usr/es/adm" during
# the running of the clconvert scripts.
ODMDIR="/etc/${HA_DIR}/objrepos" cllog -m

# Get rid of /etc/firstboot and the .mig flag.
# ----------------------------------------------
for file in /etc/firstboot /usr/sbin/cluster/.mig
do
	rm -f $file
done

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

exit 0

