#!/bin/sh
# /usr/lib/sysstat/sa2
# (C) 1999-2015 Sebastien Godard (sysstat <at> orange.fr)
#
#@(#) sysstat-11.0.4
#@(#) sa2: Write a daily report
#
S_TIME_FORMAT=ISO ; export S_TIME_FORMAT
umask 0022
prefix=/usr
exec_prefix=/usr
SA_DIR=/var/log/sysstat
SYSCONFIG_DIR=/etc/sysstat
HISTORY=7
COMPRESSAFTER=10
ZIP="xz"
YESTERDAY=
DATE=`date ${YESTERDAY} +%d`

[ -r ${SYSCONFIG_DIR}/sysstat ] && . ${SYSCONFIG_DIR}/sysstat
[ -d ${SA_DIR} ] || SA_DIR=/var/log/sysstat

if [ ${HISTORY} -gt 28 ]
then
	DATE=`date ${YESTERDAY} +%Y%m%d`
fi
CURRENTFILE=sa${DATE}
CURRENTRPT=sar${DATE}

RPT=${SA_DIR}/${CURRENTRPT}
DFILE=${SA_DIR}/${CURRENTFILE}
ENDIR=/usr/bin

[ -f "$DFILE" ] || exit 0
cd ${ENDIR}
${ENDIR}/sar $* -f ${DFILE} > ${RPT}

find ${SA_DIR} \( -name 'sar??' -o -name 'sa??' -o -name 'sar??.xz' -o -name 'sa??.xz' -o -name 'sar??.gz' -o -name 'sa??.gz' -o -name 'sar??.bz2' -o -name 'sa??.bz2' \) \
	-mtime +"${HISTORY}" -exec rm -f {} \;
find ${SA_DIR} \( -name 'sar????????' -o -name 'sa????????' -o -name 'sar????????.xz' -o -name 'sa????????.xz' -o -name 'sar????????.gz' -o -name 'sa????????.gz' -o -name 'sar????????.bz2' -o -name 'sa????????.bz2' \) \
	-mtime +"${HISTORY}" -exec rm -f {} \;
find ${SA_DIR} \( -name 'sar??' -o -name 'sa??' -o -name 'sar????????' -o -name 'sa????????' \) -type f -mtime +"${COMPRESSAFTER}" \
	-exec ${ZIP} {} \; > /dev/null 2>&1
exit 0

