#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/inittab_util.sh 1.1 
#  
# Licensed Materials - Property of IBM 
#  
# COPYRIGHT International Business Machines Corp. 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 
# @(#)53        1.1 src/43haes/usr/sbin/cluster/utilities/inittab_util.sh, hacmp.utils, 61haes_r714 12/4/07 09:48:09

#########################################################################
#									#
#  Name : inittab_common						#
#									#
#  The script contains common routines called from mkitab/chitab/rmitab/#
#  lsitab wrappers for creating,modifying, removing and listing         #
#  inittab entries, respecitively, on Linux.				#
#########################################################################


#########################################################################
#                                                                       #
#  Name      : init_os_spec_const					#
#              Helper routine to initialize variables for boundary      #
#              checks.					                #
#                                                                       #
#  Arguments : none							#
#                                                                       #
#  Returns   : nothing                                                  #
#                                                                       #
#########################################################################

init_os_spec_const() {
        [[ "$VERBOSE_LOGGING" = "high" ]] && set -x
	if [[ ${OSNAME} = *AIX* ]]
	then	
        	MAX_ID_LEN=14
	        MIN_ID_LEN=1
		MAX_RUNLEVEL_LEN=20
		VALID_ACTIONS=( respawn wait once boot bootwait powerfail powerwait off hold ondemand initdefault sysinit )
	else
		MAX_ID_LEN=14 #relaxing this criteria intentionally
		MIN_ID_LEN=1
		MAX_RUNLEVEL_LEN=20
		VALID_ACTIONS=( respawn wait once boot bootwait off ondemand initdefault sysinit powerwait powerfail powerokwait powerfailnow ctrlaltdel kebrequest )

	fi
}

#########################################################################
#									#
#  Name      : usage							#
#	       Helper routine to display usage messages			#
#  									#
#  Arguments : $1 : command for which usage msg is to be displayed	#
#									#
#  Returns   : nothing							#
#									#
#########################################################################
usage() {
	[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
	prog=$1
	case $prog in
	mkitab)
		echo "$PROGNAME:mkitab [ -i Identifier ] { [ Identifier ]:[ RunLevel ]:[ Action ]:[ Command ] }"
		;;
	lsitab)
		echo "$PROGNAME:lsitab { -a | Identifier }"
		;;
	rmitab)
		echo "$PROGNAME:rmitab Identifier"
		;;
	chitab)
		echo "$PROGNAME:chitab { [ Identifier ]:[ RunLevel ]:[ Action ]:[ Command ] }"
		;;
	*)
		echo "$PROGNAME:Invalid prgram name"
		;;
	esac
	exit 1
}
#########################################################################
#									#
#  Name     : dump_entry						#
#	      Helper routine to dump a colon seperated entry. eg:	#
#	      [identifier]:[run level]:[action]:[command]		#
#									#
#  Arguments: $1 : id							#
#             $2 : run level                                   		#	
#             $3 : action                                      		#
#             $4 : command 	                                	#
#	      $5 : file name where the record is to dumped		#
#									#
#########################################################################

dump_entry () {
	[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
        id=$1
        run_level=$2
        action=$3
        cmd=$4
	file=$5

	echo $id":"$run_level":"$action":"$cmd >> $file
	
}

#########################################################################
#                                                                       #
#  Name     : validate_id                                               #
#             Helper routine to validate the 'identifier' argument.     #
#             Presently the routine performs following checks:          #
#             1. ID can not be null					#
#             2. It can not be a duplicate id 				#
#	      3. Valid lenght check					#
#                							#
#                                                                       #
#  Arguments: $1 - identifier						#
#                                                                       #
#  Returns  : 0 - If 'identifier' is valid                              #
#             E_INVALID_ID -if action argument is missing 		#
#	      E_DUP_ID - Duplicate Id					#
#	      E_ID_TOO_LONG - Id too long				#
#                                                                       #
#########################################################################

validate_id() {
	[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
	id=$1

	#check 1: id can not be null
        if [[ -z $id ]]
        then
		echo $E_INVALID_ID
                return $E_INVALID_ID
        fi
	
	#check 2: Is this a dup id
 	if [[ -n `awk -F':' -v id=$id '/^[a-zA-Z0-9]/ &&  $1==id {print $1}' $INITTAB_FILE` ]]
	then
		echo $E_DUP_ID
		return $E_DUP_ID
	fi
	
	#check 3: valid length check for id
	if [[ (${#id} -gt $MAX_ID_LEN) || (${#id} -lt $MIN_ID_LEN) ]]
	then
		echo $E_ID_TOO_LONG
		return $E_ID_TOO_LONG
	fi

	echo $E_SUCCESS
	return $E_SUCCESS
}

#########################################################################
#                                                                       #
#  Name     : validate_runlevel                                         #
#             Helper routine to validate the 'run level' argument.      #
#             Presently the routine does not perform any check and acts #
# 	      and acts as a place-holder to insert future checks        #
#                                                                       #
#  Arguments: $1 - run level 						#
#                                                                       #
#  Returns  : 0 - If 'run level' is valid                               #
#             non-zero - otherwise                                      #
#                                                                       #
#########################################################################

validate_runlevel() {
	[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
	run_level=$1
	#todo: following checks
	#1. Valid length check for runlevel
	#2. A,B,C are valid runlevel entries for ondemand "$action" entries on Linux

	echo $E_SUCCESS
	return $E_SUCCESS
}

#########################################################################
#                                                                       #
#  Name     : validate_action 						#
#             Helper routine to validate the 'action' argument.         #
#             Presently the routine performs following checks:		#
#	      1. 'action' must be subset of VALID_ACTIONS(platform	#
#		 dependent set of valid actions)			#
#                                                                       #
#  Arguments: $1 - action						#
#                                                                       #
#  Returns  : 0 - If 'action' is valid                                  #
# 	      E_INVALID_ACTION -if action argument is missing or invalid#
#                                                                       #
#########################################################################

validate_action() {
 	[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
	action=$1

	#check 1: is this a valid action
	err_code=$E_INVALID_ACTION
	for (( i=0; i <= ${#VALID_ACTIONS}; i++))
	do
		if [[ "$action" =  ${VALID_ACTIONS[$i]} ]]
		then
			err_code=$E_SUCCESS
			break;
		fi
	done
	if [[ $err_code != $E_SUCCESS ]]
	then
		echo $E_INVALID_ACTION
		return $E_INVALID_ACTION
	fi 
	echo $err_code
	return $err_code
}

#########################################################################
#									#
#  Name     : validate_cmd						#
#	      Helper routine to validate the 'command' argument.	#
#  									#
#  Arguments: $1 - command						#
#									#
#  Returns  : 0 - If 'command' is valid					#
#             non-zero - otherwise					#
#									#
#########################################################################
validate_cmd() {
	[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
	cmd=$1
	#todo:some validations if required
	err_code=$E_SUCCESS
	echo $err_code
	return $err_code
}

#main
[[ "$VERBOSE_LOGGING" = "high" ]] && set -x
[[ "$VERBOSE_LOGGING" = "high" ]] && version='1.1'

#Error codes
E_SUCCESS=0
E_ERROR=1
E_DUP_ID=2
E_ID_TOO_LONG=3
E_INVALID_ID=4
E_INVALID_RUNLEVEL=5
E_INVALID_ACTION=6

INITTAB_FILE="/etc/inittab"
init_os_spec_const
