#!/bin/sh
#
QUIET=0
WEEKS=0
MODE="zero"
while getopts qcmzw: opts
do
case $opts in
q) QUIET=1;;
c) MODE="copy";;
m) MODE="move";;
z) MODE="zero";;
w) WEEKS=$OPTARG;;
esac
done
shift `expr $OPTIND - 1`
if [ "$1" = "" ]
then
echo "usage: $0 [-q] [-w numweeks] [-c|-m|-z] logdir "
echo "ERROR: missing argument logdir "
exit 1
fi
LOGDIR=$1
LOGHOME=${LOGDIR}.archive
if [ ! -d ${LOGDIR} ]
then
if [ $QUIET = 1 ]
then
exit 0
else
echo "usage: logmaint.sh logdir "
echo "ERROR: '${LOGDIR}' is not a directory "
exit 1
fi
fi
if [ ! -d ${LOGHOME} ]
then
/bin/mkdir ${LOGHOME}
if [ $? -ne 0 ]
then
echo "ERROR: failed to create ${LOGHOME}"
exit 1
fi
fi
if [ $WEEKS = 0 ]
then
DAYOFWEEK=`date | cut -f1 -d" "`
DSTDIR=${LOGHOME}/$DAYOFWEEK
if [ ! -d ${DSTDIR} ]
then
/bin/mkdir ${DSTDIR}
if [ $? -ne 0 ]
then
echo "ERROR: failed to create ${DSTDIR}"
exit 1
fi
fi
/bin/rm -r ${DSTDIR}/* > /dev/null 2>&1
/bin/cp -rp ${LOGDIR}/* ${DSTDIR}
if [ $? -ne 0 ]
then
echo "ERROR: failed to copy '${LOGDIR}' to '${DSTDIR}'"
exit 1
fi
else
DSTDIR=${LOGHOME}/week1
/bin/rm -rf ${LOGHOME}/week${WEEKS}
while [ $WEEKS -gt 1 ]
do
NEXT=$WEEKS
WEEKS=`expr $WEEKS - 1`
if [ -d ${LOGHOME}/week${WEEKS} ]
then
/bin/mv -f ${LOGHOME}/week${WEEKS} ${LOGHOME}/week${NEXT}
fi
done
/bin/mkdir ${LOGHOME}/week1
/bin/cp -rp ${LOGDIR}/* ${LOGHOME}/week1
if [ $? -ne 0 ]
then
echo "ERROR: failed to copy '${LOGDIR}' to '${DSTDIR}'"
exit 1
fi
fi
if [ $MODE = zero ]
then
find ${LOGDIR} -name "*" -print > /tmp/cleanloglist.${$}
for name in `cat /tmp/cleanloglist.${$}`
do
if [ -f $name ]
then
/bin/cat /dev/null > $name
fi
done
/bin/rm -f /tmp/cleanloglist.${$}
elif [ $MODE = move ]
then
/bin/rm -rf ${LOGDIR}/*
fi
exit 0