#!/bin/sh
#
# sysstat      Reset the system activity logs
#
# chkconfig: 12345 01 99
# description: Reset the system activity logs
# /etc/init.d/sysstat
# 2000/01/22 - Sebastien Godard <sebastien.godard@wanadoo.fr>
#
# Description: Insert a dummy record in current daily data file.
#	       This indicates that the counters have restarted from 0.
#
# 2004/10/10 - Sebastien Godard (sysstat <at> wanadoo.fr)
#	Now returns real exit code.
# 2006/07/05 - Sebastien Godard (sysstat <at> wanadoo.fr)
#	Added support for chkconfig.
# 2007/01/21 - Sebastien Godard (sysstat <at> wanadoo.fr)
#	Added support for multiple months history.
#

RETVAL=0
HISTORY=0

# See how we were called.
case "$1" in
  start)
	exitCodeIndicator="$(mktemp /tmp/sysstat-XXXXXX)" || exit 1
	[ -r /etc/sysconfig/sysstat ] && . /etc/sysconfig/sysstat
	if [ ${HISTORY} -gt 28 ]
	then
		CURRENTDIR=`date +%Y%m`
		CURRENTFILE=sa`date +%d`
		DDIR=/var/log/sa
		cd ${DDIR} || exit 1
		[ -d ${CURRENTDIR} ] || mkdir -p ${CURRENTDIR}
		touch ${CURRENTDIR}/${CURRENTFILE}
	        # Remove the "compatibility" link and recreate it to
	        # point to the (new) current file
	        rm -f ${CURRENTFILE}
	        ln -s ${CURRENTDIR}/${CURRENTFILE} ${CURRENTFILE}
		# CHOWN ${CURRENTDIR} ${CURRENTFILE} ${CURRENTDIR}/${CURRENTFILE}
	fi
        echo -n "Calling the system activity data collector (sadc): "
        /usr/lib/sa/sadc -F -L - || rm -f ${exitCodeIndicator}

	# Try to guess if sadc was successfully launched. The difficulty
	# here is that the exit code is lost when the above command is
	# run via "su foo -c ..."
	if [ -f "${exitCodeIndicator}" ]; then
		rm -f ${exitCodeIndicator}
	else
		RETVAL=1
	fi
        echo
        ;;
  stop|status|restart|reload)
        ;;
  *)
        echo "Usage: sysstat {start|stop|status|restart|reload}"
        exit 1
esac
exit ${RETVAL}

