#
# Copyright (c) 2007 Cray, Inc  All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it would be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
#
# Contact information:  Silicon Graphics, Inc., 1500 Crittenden Lane,
# Mountain View, CA  94043, or:
#
# http://www.sgi.com
#
# Contact information:  Cray Inc., 1340 Mendota Heights Road,
# Mendota Heights, MN 55120
#
# http://www.cray.com
#
# This script runs on a service node on Cray XT and Cray BW systems.
# Its purpose is to collect the accounting data from each of the local
# node accounting files that reside on the common file system, and
# combine this data onto a single process accounting (pacct) file that
# can be processed by the accounting utility programs.
#
# Put /usr/sbin in the front of the path
PATH=/usr/sbin:${PATH}
# Absolute path for rca-helper and xtgetconfig
RCA_PATH=/opt/xt-boot/default/bin/snos64
RCA_ALT_PATH=/sbin
ACCT_FS_MOUNT="ACCT_FILE_SYSTEM_"
DO_PROCESS_ACCT="NODE_PROCESS_ACCOUNT"
CSA="csa"
NODE_TYPE=""
PROCESS=""
XT="XT"
X2="X2"
# Define the global lock file used by csanodeacct and csanodemerg.
# This lock file is used to make sure the common file system pacct
# file for a given compute node is not being read by csanodemerg at
# the same time that csanodeacct is writing it.
CFSLOCKFILE=csanodeacct.lock
# Lockfile to make sure only one copy of csanodemerg is running
LOCKFILE=/tmp/csanodemerg.lock

# define global working variables
typeset -i j
typeset -i SIO_NODES
typeset -i SIO_NODES_MAX=99
typeset -RZ2 i

# Global variables for cname components on Cray XT systems
typeset -i CAB
typeset -i ROW
typeset -i CAGE
typeset -i XT_SLOT
typeset -i MCOMP

# Global variables for rname components on Cray XT systems
typeset -i X
typeset -i Y
typeset -i CHASSIS
typeset -i X2_SLOT
typeset -i NODE

#
# Set some variables retrieved from csa...
#
# what group to chgrp to
CHGRP=`/usr/sbin/csagetconfig CHGRP`
# chmod mask value
CHMOD=`/usr/sbin/csagetconfig CHMOD`
#
# function cfpath2path	# _config_file_path
#	Converts a pathname obtained from the config file to a real
#	path name.  The config file pathname has "_" instead of "/"
#	so that the config file parser can correctly handle it.
#
#	input:		pathname of form _config_file_path
#	output:		pathname of form /config/file/path
#
function cfpath2path	# _config_file_path
{
	typeset i
	typeset cf_path
	typeset path
	
	cf_path=${1#_}
	IFS=_
	set $cf_path
	for i
	do
	    path=$path"/$i"
	done
	print "$path"
	return
}

#
#  cname2comps	- break cname apart into each of its component
#			parts and place each component in a global
#			variable.
#	inputs	- $1 	cname of form: cCC-RRcCCsSSnNN
#			each component can be up 1 or more digits
#	return	- None
#

function cname2comps	# cname
{
	typeset temp
	typeset cname
	
	cname=$1			# cCC-RRcCCsSSnNN
	temp=${cname%-*}		# cCC
	CAB=${temp#c}			# CC
	temp=${cname#*-}		# RRcCCsSSnNN
	ROW=${temp%c*}			# RR
	temp=${temp#*c}			# CCsSSnNN
	CAGE=${temp%s*}			# CC
	temp=${temp#*s}			# SSnNN
	XT_SLOT=${temp%n*}			# SS
	MCOMP=${temp#*n}		# NN
	return
}

#
# cname2path	- convert cname into a pathname, using the cname
#		  components
#	calls	- cname2comps
#	input	- cname of form:  cCC-RRcCCsSSnNN
#	output	- pathname of form: cabCC/rowRR/cageCC/slotSS/mcompNN
#

function cname2path	# cname
{
	cname2comps $1
	echo "cab$CAB/row$ROW/cage$CAGE/slot$XT_SLOT/mcomp$MCOMP"
	return
}

#
# cname_in_range  - compares each cname component to the minimum and maximum
#		    allowed values for the component as defined by an array
#		    index.  The index is used to reference arrays of minimum
#		    and maximum values respectively for each cname component.
#	inputs	  - cname of form cCC-RRcCCsSSnNN
#		  - index for component maximum and minimum value arrays.
#	output    - RANGE_OK = 0 cname is in range
#		  - RANGE_OK = 1 cname is not in range
#

function cname_in_range # cname indx
{
	RANGE_OK=0
	cname2comps $1

	[[ $CAB < ${CAB_MIN[$2]} || $CAB > ${CAB_MAX[$2]} ]] && \
		RANGE_OK=1
	[[ $ROW < ${ROW_MIN[$2]} || $ROW > ${ROW_MAX[$2]} ]] && \
		RANGE_OK=1
	[[ $CAGE < ${CAGE_MIN[$2]} || $CAGE > ${CAGE_MAX[$2]} ]] && \
		RANGE_OK=1
	[[ $XT_SLOT < ${XT_SLOT_MIN[$2]} || $XT_SLOT > ${XT_SLOT_MAX[$2]} ]] \
		&& RANGE_OK=1
	[[ $MCOMP < ${MCOMP_MIN[$2]} || $MCOMP > ${MCOMP_MAX[$2]} ]] && \
		RANGE_OK=1
	return
}

#
#  Note: Nodes on Cray X2 systems are represented by an rname that is similar
#	 to the cname on XT systems.  The rname is broken into components
#	 having different names than cname components. This represents the
#	 difference in the physical hardware description.
#
#  rname2comps	- break rname apart into each of its component
#			parts and place each component in a global
#			variable.
#	inputs	- $1 	rname of form: r1XX-YYcCCsSSnNN
#			r1 = rank 1, always for compute cabinets
#			XX = X coord
#			YY = Y coord
#			CC = chassis
#			SS = slot
#			NN = node
#			each component can be 1 or more digits
#	return	- None
#

function rname2comps	# rname
{
	typeset temp
	typeset rname
	
	rname=$1			# r1XX-YYcCCsSSnNN
	temp=${rname%-*}		# r1XX
	X=${temp#r1}
	temp=${rname#*-}		# YYcCCsSSnNN
	Y=${temp%c*}			# YY
	temp=${temp#*c}			# CCsSSnNN
	CHASSIS=${temp%s*}		# CC
	temp=${temp#*s}			# SSnNN
	X2_SLOT=${temp%n*}			# SS
	NODE=${temp#*n}			# NN
	return
}

#
# rname2path	- convert rname into a pathname, using the rname
#		  components
#	calls	- rname2comps
#	input	- cname of form:  r1XX-YYcCCsSSnNN
#	output	- pathname of form: rank1/x/y/chassis/slotSS/nodeNN
#	Note:	- all X2 compute nodes have a rank value of 1, so this
#		  is hard coded into the pathname.
#

function rname2path	# rname
{
	rname2comps $1
	echo "rank1/x${X}/y${Y}/chassis${CHASSIS}/slot${X2_SLOT}/node${NODE}"
	return
}

#
# rname_in_range  - compares each cname component to the minimum and maximum
#		    allowed values for the component as defined by an array
#		    index.  The index is used to reference arrays of minimum
#		    and maximum values respectively for each rname component.
#	inputs	  - rname of form r1XX-YYcCCsSSnNN
#		  - index for component maximum and minimum value arrays.
#	output    - RANGE_OK = 0 rname is in range
#		  - RANGE_OK = 1 rname is not in range
#

function rname_in_range # rname indx
{
	RANGE_OK=0
	rname2comps $1

	[[ $X < ${X_MIN[$2]} || $X > ${X_MAX[$2]} ]] && \
		RANGE_OK=1
	[[ $Y < ${Y_MIN[$2]} || $Y > ${Y_MAX[$2]} ]] && \
		RANGE_OK=1
	[[ $CHASSIS < ${CHASS_MIN[$2]} || $CHASSIS > ${CHASS_MAX[$2]} ]] \
		&& RANGE_OK=1
	[[ $X2_SLOT < ${X2_SLOT_MIN[$2]} || $X2_SLOT > ${X2_SLOT_MAX[$2]} ]] \
		&& RANGE_OK=1
	[[ $NODE < ${NODE_MIN[$2]} || $NODE > ${NODE_MAX[$2]} ]] && \
		 RANGE_OK=1
	return
}

#
# Functions: cleanup
# Used to cleanup the lock file if we get a signal we want to trap
#
function cleanup {
	[[ -r $LOCKFILE ]] && rm -f $LOCKFILE
	exit
}
