#! /bin/sh
#
# Copyright (c) 2000-2002 Silicon Graphics, Inc and LANL  All Rights Reserved.
# Copyright (c) 2004-2005 Silicon Graphics, Inc All Rights Reserved.
# 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, or:
#
# http://www.cray.com
#
# csa:		Start CSA job accounting
#
# /etc/init.d/csa
#
# description:	CSA provides job and per-process accounting.  CSA \
#		collects per-process resource usage data, monitors \
#		disk usage and charges fees to specific login \
#		accounts.  This accounting information is combined \
#		by job identifier within system uptime periods for \
#		daily and periodic reports.
# config: /etc/csa.conf
#
### BEGIN INIT INFO
# Provides: csa
# Required-Start: $local_fs job
# Required-Stop: $local_fs job
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Starts CSA job accounting
### END INIT INFO

#
# If on a service node, must run bash to get the boot info and to determine if
# this is a boot node or an sdb node.  We do not want to run accounting on
# either the boot node or the sdb node.
# To check for boot or service node, execute the bash script, /etc/init.d/csa-ss
#
if [ -r /etc/xt.servicenode ]; then
    /etc/init.d/csa-ss
    [[ $? == 0 ]] && exit 0  # on boot node or sdb node
fi

. /etc/rc.status
case $1 in

'start')
	echo -n "Starting CSA job accounting"
	# Make sure /var/csa directories exist
	[[ ! -d /var/csa ]] && mkdir -p /var/csa
	[[ ! -d /var/csa/day ]] && mkdir -p /var/csa/day
	[[ ! -d /var/csa/fiscal ]] && mkdir -p /var/csa/fiscal
	[[ ! -d /var/csa/nite ]] && mkdir -p /var/csa/nite
	[[ ! -w /var/csa/nite/statefile ]] && \
		cat /dev/null > /var/csa/nite/statefile
	[[ ! -d /var/csa/sum ]] && mkdir -p /var/csa/sum
	[[ ! -d /var/csa/work ]] && mkdir -p /var/csa/work
	# make sure /var/lock/subsys directory exists
	[[ ! -d /var/lock/subsys ]] && mkdir -p /var/lock/subsys
	# rc_failed does not exist on compute nodes
	if [ -r /etc/xt.computenode ]; then
		/usr/sbin/csaswitch -c on 2> /dev/null
	else
		/usr/sbin/csaswitch -c on 2> /dev/null || rc_failed
	fi
	RETVAL=$?
	# touch does not exist on compute nodes, use echo
	[[ $RETVAL -eq 0 ]] && echo "$$" >  /var/lock/subsys/csa
	# clean up old accounting files
	rm -f /var/csa/nite/lock* 2> /dev/null
	rc_status -v
	;;

'stop')
	echo -n "Stopping CSA job accounting"
	# rc_failed does not exist on compute nodes
	if [ -r /etc/xt.computenode ]; then
		/usr/sbin/csaswitch -c halt 2> /dev/null
		/sbin/rmmod csa 2> /dev/null
	else
		/usr/sbin/csaswitch -c halt 2> /dev/null || rc_failed
		/sbin/rmmod csa 2> /dev/null || rc_failed
	fi
	RETVAL=$?
	rm -f /var/lock/subsys/csa
	rc_status -v
	;;
'restart')
	$0 stop
	$0 start
	;;
*)
	echo "usage: $0 {start|stop}"
	exit 1
	;;

esac

exit $RETVAL
