#!/bin/ksh
#
# $Revision: 1.1 $ $Date: 2011-03-12 18:11:43-05 $
# $Source: /doc/sitelog/fs005/perflog/RCS/120.perf-compress,v $
# $Host: fs005.com $
# $UUID: 6d996d69-1ffc-4723-9e2f-82e777d92418 $
#
#<120.perf-compress: runs xz on yesterday's logfile.
# 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 -d yesterday '+%Y %m%d')
case "$#" in
    3) yr="$2"; day="$3" ;;
    *) die "date failed? [$@]" ;;
esac

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

# If today is 9 Mar 2011, the file $top/2011.n/0308 should exist.
# Compress if found.

if test -f "$day"; then
    logmsg "compressing $yr.n/$day"
    xz $day
elif test -f "$day.xz"; then
    logmsg "$yr.n/$day already done"
else
    logmsg "FAILED: $day: file not found"
fi

exit 0
