#!/bin/ksh
#
# $Revision: 1.1 $ $Date: 2011-03-12 18:11:11-05 $
# $UUID: 3aaa505c-da19-4e3e-bb81-32226e4a38ea $
#
#<110.perf-clean: removes old perflog directory if "reduce" worked.
# PERFLOG env variable: top-level performance log directory.

export PATH=/usr/local/bin:/bin:/sbin:/usr/sbin:/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 9 Mar 2011, perf-reduce should have created a file
# called $top/2011.n/0309 holding at least 500K.  If we find it,
# we can remove the $top/2011/0309 directory.

set X $(ls -l $dst/$day)
case "$#" in
    10) size="$6" ;;
     *) die "ls failed? [$@]" ;;
esac

if test "$size" -ge 500000; then
    if test -d "$day"; then
        logmsg "removing $yr/$day"
        rm -rf $day || logmsg "rm failed"
    else
        logmsg "FAILED: $day: dir not found"
    fi
else
    logmsg "FAILED: $day too small"
fi

exit 0
