#! /bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004,2007 
# 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 
#*===========================================================================*/
#*                                                                           */
#* Module Name:  ctrestore                                                   */
#*                                                                           */
#* Description:                                                              */
#*      Script to restore RSCT data from a backup directory                  */
#*                                                                           */
#* Procedure:                                                                */
#*   1) Ensure backup directory /var/ct.backup exists and contains           */
#*      critical data                                                        */
#*                                                                           */
#*   2) Run uncfgct to clean up existing data                                */
#*                                                                           */
#*   3) Copy cluster independent files to /var/ct                            */
#*                                                                           */
#*   4) For each defined cluster in backup, copy cluster dependent files     */
#*                                                                           */
#*===========================================================================*/
#*   @(#)55   1.3   src/rsct/utils/cmds/ctrestore.sh, common_utils, rsct_relgh, relghs001a 10/4/04 20:02:04

# include common definitions for backup/restore

. /usr/sbin/rsct/bin/ctbrdefs

# define a clean-up function. Remove all data under /var/ct and then
# execute cfgct to provide a "sane" RMC subsystem. This function is only
# called after uncfgct has been called.

function clean_up_and_exit {
	for name in $(ls $CTROOTDIR)
	do
		if [[ $name != "cfg" && $name != "lck" ]]
		then
			if [[ -d ${CTROOTDIR}/$name ]]
			then
				rm -rf ${CTROOTDIR}/$name
			else
				rm -f ${CTROOTDIR}/$name
			fi
		fi
	done

	rm -rf ${CTROOTDIR}/cfg/*
	rm -rf ${CTROOTDIR}/lck/*

	rm -f /etc/ct_node_id

	cfgct

	exit 1
}


#==================================================================
#==================================================================
#                     Main procedure
#==================================================================
#==================================================================

# Need to know which OS on which this script is running

OSname=$(uname -s)
if [[ -z $OSname ]]
then
    OSname=unknown_OS
fi

# verify that SRC is running on Linux

if [[ $OSname = "Linux" ]]
then
	# set limit on retries

	(( i = 5 ))

	while (( i > 0 ))
	do
		lssrc -a > /dev/null 2>&1
		rc=$?
		if [[ $rc -eq 0 ]]
		then
			break
		fi

		(( i = i - 1 ))
		if (( i == 0 ))
		then
			break		# retry limit exceeded
		fi

		sleep 2
	done
	if (( i == 0 ))
	then
		print "SRC subsystem is not running"
		exit 1		# retry limit exceeded
	fi
fi


#==================================================================
# Check backup directory
#==================================================================

# Validate that the backup directory exists

if [[ ! -d $CTBACKUPDIR ]]
then
	print "Directory $CTBACKUPDIR does not exist"
	exit 1
fi

# check for required files

for name in $CTBACKUPDIR/rsct.version $CLUSTER_LIST
do
	if [[ ! -s $name ]]
	then
		print "File $name is missing from backup directory"
		exit 1
	fi
done


#==================================================================
# Remove current content of /var/ct, including the node ID
#==================================================================

if ! uncfgct -n
then
	exit 1
fi


#==================================================================
# restore files in "root" directory 
#==================================================================

for name in $VAR_CT_FILES
do
	if ! copy_file_to_dir ${CTBACKUPDIR}/$name $CTROOTDIR $FUNC_RETURN
	then
		clean_up_and_exit
	fi
done

#==================================================================
# restore files in cluster independent cfg directory
#==================================================================

if ! cp -rp $BCFGDIR $CTROOTDIR
then
	print "Cannot restore files from $BCFGDIR"
	clean_up_and_exit
fi

# make copy of node ID in /etc

if ! cp -pf ${CFGDIR}/ct_node_id /etc/ct_node_id
then
	print "Cannot make /etc/ct_node_id"
	clean_up_and_exit
fi

#==================================================================
# restore files in cluster dependent directories
#==================================================================

# do for each cluster

while read CLUSTER_NAME CLUSTER_ID
do

	# restore the cluster directory

	if ! cp -rp ${CTBACKUPDIR}/${CLUSTER_ID} $CTROOTDIR
	then
		print "Cannot restore files from $BCFGDIR"
		clean_up_and_exit
	fi

	# restore the cluster symlink

	ln -s ${CTROOTDIR}/${CLUSTER_ID} ${CTROOTDIR}/${CLUSTER_NAME}

	# make necessary directories

	for name in							\
		${CTROOTDIR}/${CLUSTER_ID}/lck	\
		${CTROOTDIR}/${CLUSTER_ID}/log	\
		${CTROOTDIR}/${CLUSTER_ID}/run	\
		${CTROOTDIR}/${CLUSTER_ID}/soc
	do
		if ! mkdir_and_set_ogp $name $FUNC_RETURN
		then
			clean_up_and_exit
		fi
	done

	# if not the IW "cluster", make additional directories

	if [[ $CLUSTER_NAME != "IW" ]]
	then
		for name in									\
			${CTROOTDIR}/${CLUSTER_ID}/lck			\
			${CTROOTDIR}/${CLUSTER_ID}/lck/mc		\
			${CTROOTDIR}/${CLUSTER_ID}/log			\
			${CTROOTDIR}/${CLUSTER_ID}/log/mc		\
			${CTROOTDIR}/${CLUSTER_ID}/log/cthats	\
			${CTROOTDIR}/${CLUSTER_ID}/log/cthags	\
			${CTROOTDIR}/${CLUSTER_ID}/cfg/rmdefs	\
			${CTROOTDIR}/${CLUSTER_ID}/run			\
			${CTROOTDIR}/${CLUSTER_ID}/run/mc		\
			${CTROOTDIR}/${CLUSTER_ID}/run/cthats	\
			${CTROOTDIR}/${CLUSTER_ID}/run/cthags	\
			${CTROOTDIR}/${CLUSTER_ID}/soc			\
			${CTROOTDIR}/${CLUSTER_ID}/soc/mc
		do
			if ! mkdir_and_set_ogp $name $FUNC_RETURN
			then
				clean_up_and_exit
			fi
		done
	fi


done < $CLUSTER_LIST


#==================================================================
# start RSCT
#==================================================================

# add security subsystem to SRC first

ctcasctrl -a

# start RMC, which will bring up resource managers and result in the
# cluster coming up

if ! rmcctrl -A
then
	exit 1
fi

exit 0
