#! /usr/bin/ksh

#
# @(#)05        1.1  src/bos/usr/sbin/drmgr/sample_scripts/IBM_template.sh.S, cmdcfg, bos720 8/22/02 11:44:49
#
# COMPONENT_NAME: (CMDCFG)
#
# FUNCTIONS: 
#
# ORIGINS: 27
#
# (C) COPYRIGHT International Business Machines Corp. 2000, 2002
# All Rights Reserved
# Licensed Materials - Property of IBM
# 
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#
# DLPAR aware Application developers will modify this script to
# develop a DLPAR script to suit their application's needs of control
# vis-a-vis Dynamic Reconfiguration(DLPAR) Operations.
#


# FILE NAME: IBM_template.sh
#
# FILE DESCRIPTION: 
#   This is an example template shell DLPAR script file for 
#   Dynamic Reconfiguration Application Framework of AIX.
#   This template file just prints the various inputs 
#   from drmgr.
#
#   Note that DLPAR script file should adher to the guildelines
#   related to AIX Dynamic Reconfiguration. Some of the 
#   issues to be considered while chaagng this script file
#   are:
#   1. Output name=value pairs only to stdout
#      as per the DRAF guidelines. Refer to
#      Manuals related to DLPAR for more details.
#   2. Return 0 upon success, 10 if the command
#      is not implemented, else return any other
#      return code (1 to 9, 11 to 255)
#   3. Use DRAF defined environment variables and
#      input parameters for processing the
#      command.
#   4. To debug the script file, one can use
#      the method shown in this template file.
#
# RETURN VALUE DESCRIPTION:
#      0         Successful
#      10        Command not implemented
#      Else      Error
#
#
#


################################# dbg #######################################
#
# NAME: dbg()
#
# DESCRIPTION: Write the debug message to debug file
#
# INPUT:
#	Message to write to debug file
#
# OUTPUT:
#	Message echoed to the debug file.
#
# RETURN VALUE DESCRIPTION:
#	None
#
##############################################################################

dbg()
{
	echo $1 >> ${DBG_FILE_NAME}
}

############################## process_scriptinfo ############################
#
# NAME: process_scriptinfo()
#
# DESCRIPTION: Process 'scriptinfo' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#
# OUTPUT:
#	Output name=value pairs to stdout
#	Various pieces of information about the DLPAR script.
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	Else failure.
#
##############################################################################

process_scriptinfo()
{
	echo "DR_SCRIPTINFO=AIX DR ksh example script"
	echo "DR_VERSION=1"
	echo "DR_DATE=10182002"
	echo "DR_VENDOR=IBM"
	echo "DR_TIMEOUT=10"
	return 0
}

############################## process_register   ############################
#
# NAME: process_register()
#
# DESCRIPTION: Process 'register' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#
# OUTPUT:
#	Output name=value pairs to stdout
#	List of all the resources supported by this DLPAR script.
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	Else failure.
#
##############################################################################

process_register()
{
	echo "DR_RESOURCE=cpu"
	echo "DR_RESOURCE=mem"
	return 0
}

############################## process_usage      ############################
#
# NAME: process_usage()
#
# DESCRIPTION: Process 'usage' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Output name=value pairs to stdout
#	Writes the how this resource is being used by the application
#	associated with this DLPAR script.
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	Else failure.
#
##############################################################################

process_usage()
{
	case	"$1" in
		"cpu")
			echo "DR_USAGE=cpu binding for performance"
			;;
		"mem")
			echo "DR_USAGE=Shared(Pinned) memory for app XYZ"
			;;
		*)
			echo "DR_ERROR=Script does not use Resource $1"
			;;
	esac
	return 0
}

############################## process_checkrelease ##########################
#
# NAME: process_checkrelease()
#
# DESCRIPTION: Process 'checkrelease' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR operatin is not ok with the DLPAR script/associated app.
#
##############################################################################

process_checkrelease()
{
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			# Do all the cpu related checks here and determine
			# whether DLPAR remove can proceed. 
			;;
		"mem")
			dbg "Resource : mem"
			# Do all the memory  related checks here and determine
			# whether DLPAR remove can proceed. 
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac

	return 0
}

############################## process_prerelease ############################
#
# NAME: process_prerelease()
#
# DESCRIPTION: Process 'prerelease' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application could not release the resource
#	for DLPAR operation.
#
##############################################################################
process_prerelease()
{
	# Do any pre release actions here. One could send a signal
	# from here and wait for application do the necessary.
	# Return from here only after the desired actions have
	# taken place.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			# Release any cpu bindings etc here if the
			# resource being released is used by the app.
			;;
		"mem")
			dbg "Resource : mem"
			# Release application hold over any memory 
			# that is being removed.
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac

	return 0
}

############################## process_postrelease ############################
#
# NAME: process_postrelease()
#
# DESCRIPTION: Process 'postrelease' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application could not post DLPAR operations.
#
##############################################################################
process_postrelease()
{
	# Reacquire any resource release during prerelease.
	# activate any apps quieced during prerelease.

	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac

	return 0
}

############################## process_undoprerelease #########################
#
# NAME: process_undoprerelease()
#
# DESCRIPTION: Process 'process_undoprerelease' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application failed undorelease
#
##############################################################################
process_undoprerelease()
{
	# DLPAR operation was aborted/failed. Hence undo any
	# changes done during prerelease for this resource
	# and the application associated with the DLPAR script.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}

############################## process_checkacquire   #########################
#
# NAME: process_checkacquire()
#
# DESCRIPTION: Process 'process_checkacquire' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application does want this resource.
#
##############################################################################
process_checkacquire()
{
	# Do any checks prior to resource addition.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}
############################## process_preacquire     #########################
#
# NAME: process_preacquire()
#
# DESCRIPTION: Process 'process_preacquire' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application preacquire failed.
#
##############################################################################
process_preacquire()
{
	# Do all the necessary work prior to resource addition.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}

############################## process_undopreacquire #########################
#
# NAME: process_undopreacquire()
#
# DESCRIPTION: Process 'process_undopreacquire' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application undopreacquire failed.
#
##############################################################################
process_undopreacquire()
{
	# DLPAR operation has failed. So undo any activities done during
	# preacquire
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}
############################## process_postacquire  #########################
#
# NAME: process_postacquire()
#
# DESCRIPTION: Process 'process_postacquire' command from drmgr
#
# INPUT:
#	The various environment variables set by drmgr
#	resource name input variable
#
# OUTPUT:
#	Any debug information using DR debug name=value pairs
#	such as DR_LOG_DEBUG="..."
#	Any error message in the form of DR_ERROR name=value pair
#
#
# RETURN VALUE DESCRIPTION:
#	0 : success
#	10 : Command not implemented for this resource.
#	Else DLPAR script/associated application postacquire failed.
#
##############################################################################
process_postacquire()
{
	# execute any actions required after the DLPAR add operation.
	# Egs: Increase the number of threads for the application
	#      Increase memory areas reserved for application etc.
	case	"$1" in
		"cpu")
			dbg "Resource : cpu"
			;;
		"mem")
			dbg "Resource : mem"
			;;
		*)
			echo "DR_ERROR=Script does not support Resource $1"
			;;
	esac
	return 0
}

###############################################
#            MAIN SCRIPT STARTS HERE
###############################################

script_file_name=`basename $0`
DBG_FILE_NAME=/tmp/${script_file_name}.dbg

date_and_time=`date`
dbg "------ DLPAR Script start at $date_and_time -------"

if [ $# -eq 0 ]
then
	# Atleast the command must be part of the invocation
	dbg "No command passed to the DLPAR script"
	echo "DR_ERROR=Script Usage Error"
	exit 1
fi

# Note down the command
command=$1
ret_code=0

dbg "command issued: $1"
case	"$1" in
	scriptinfo)
		process_scriptinfo
		ret_code=$?
		;;
	register)
		process_register
		ret_code=$?
		;;
	usage)
		process_usage $2 
		ret_code=$?
		;;
	checkrelease)
		process_checkrelease $2
		ret_code=$?
		;;
	prerelease)
		process_prerelease $2
		ret_code=$?
		;;
	postrelease)
		process_postrelease $2
		ret_code=$?
		;;
	undoprerelease)
		process_undoprerelease $2
		ret_code=$?
		;;
	checkacquire)
		process_checkacquire $2
		ret_code=$?
		;;
	preacquire)
		process_preacquire $2
		ret_code=$?
		;;
	undopreacquire)
		process_undopreacquire $2
		ret_code=$?
		;;
	postacquire)
		process_postacquire $2
		ret_code=$?
		;;
	*)
		dbg "unknown command: $1 issued"
		ret_code=10
		;;
esac

dbg "SCRIPT exiting with return code : $ret_code"

dbg "................DLPAR Script end  ................"

return $ret_code