#!/bin/ksh
#
# $Revision: 1.1 $ $Date: 2011-03-12 18:10:55-05 $
# $UUID: 1865d353-8b6c-469a-876d-83f72e8ae5c1 $
#
#<100.perf-reduce: merges separate perflog files to save space.
# PERFLOG env variable: top-level performance log directory.

export PATH=/usr/local/bin:/bin:/usr/bin
tag=${0##*/}

# General logging and error-handling.
logmsg () {
    logger -t $tag "$@"
}

die () {
    logmsg "FATAL: $@"
    exit 1
}

# Sanity checks.
case "$PERFLOG" in
    "") die "need top-level log directory" ;;
    *)  top="$PERFLOG" ;;
esac

set X $(date '+%Y %m%d')
case "$#" in
    3) yr="$2"; day="$3" ;;
    *) die "date failed? [$@]" ;;
esac

src="$top/$yr"
test -d "$src" || die "$src: no source dir"
cd "$src"      || die "$src: cannot cd"

dst="$top/$yr.n"
test -d "$dst" || mkdir -p "$dst" || die "$dst: no dest dir"

# If today is 31 Dec 2009, one sub-directory is 2009/1231/1441
# with these files:
#   cache  df  ifconfig  netstat  ping  ps  smbstatus  swap  uname  uptime
#
# We want a single 2009.n/1231 file holding sections like:
#   ....
#   ==> 1231/1441/cache <==
#   ....
#   ==> 1231/1441/df <==
#   ....
#   ==> 1231/1441/uptime <==
#   ....
#
# We could use tar, but this way we can also use grep, or put
# the sections back into their own files.

if test -d "$day"
then
    logmsg "wrote $dst/$day"
    head -9999 $day/*/* > $dst/$day
else
    logmsg "FAILED: $day: dir not found"
fi

exit 0
