#!/bin/sh
#/* 
#  Copyright (c) 2000 QoSient, LLC
#  All rights reserved.
# 
#  Permission to use, copy, modify, and distribute this software and
#  its documentation for any purpose and without fee is hereby granted, 
#  provided that the above copyright notice appear in all copies and
#  that both that copyright notice and this permission notice appear
#  in supporting documentation, and that the name of QoSient not be
#  used in advertising or publicity pertaining to distribution of the
#  software without specific, written prior permission.  
#  
#  QOSIENT, LLC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
#  SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
#  FITNESS, IN NO EVENT SHALL QOSIENT, LLC BE LIABLE FOR ANY
#  SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
#  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
#  CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
#  CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 
#*/

PATH=/bin:/usr/bin

#
# Try to use $ARGUSDATA and $ARGUSARCHIVE where possible.
# If these are available, the only thing that we need to
# know is what is the name of the argus output file.
#
# If ARGUSDATA set then don't need to define below.  For
# cron scripts however, $ARGUSDATA may not be defined, so
# lets do that here.

ARGUSBIN=/usr/local/bin
ARGUSDATA=/var/log/argus
ARGUSARCHIVE=/usr/local/argus/archive

RAGATOR=$ARGUSBIN/ragator
RASORT=$ARGUSBIN/rasort

COMPRESSOR=gzip
COMPRESSFILEEXT=gz

#COMPRESSOR=bzip2
#COMPRESSFILEEXT=bz2

#COMPRESSOR=compress
#COMPRESSFILEEXT=Z

DATAFILE=argus.out

ARCHIVE=argus.`date '+%Y.%m.%d.%H.%M.%S'`
ARCHIVEZIP=$ARCHIVE.$COMPRESSFILEEXT

YEAR=`echo $ARCHIVE | awk 'BEGIN {FS="."}{print $2}'`
MONTH=`echo $ARCHIVE | awk 'BEGIN {FS="."}{print $3}'`
DAY=`echo $ARCHIVE | awk 'BEGIN {FS="."}{print $4}'`

if [ -d $ARGUSDATA ] ; then
   cd $ARGUSDATA
else
   echo "argus data directory $ARGUSDATA not found"
   exit
fi

if [ ! -d $ARGUSARCHIVE ] ; then
   mkdir $ARGUSARCHIVE
   if [ ! -d $ARGUSARCHIVE ] ; then
      echo "could not create archive directory $ARGUSARCHIVE"
      exit
   fi
fi

ARGUSARCHIVE=$ARGUSARCHIVE/$YEAR

if [ ! -d $ARGUSARCHIVE ]; then
   mkdir $ARGUSARCHIVE
   if [ ! -d $ARGUSARCHIVE ]; then
      echo "could not create archive directory structure."
      exit
   fi
fi

ARGUSARCHIVE=$ARGUSARCHIVE/$MONTH

if [ ! -d $ARGUSARCHIVE ]; then
   mkdir $ARGUSARCHIVE
   if [ ! -d $ARGUSARCHIVE ]; then
      echo "could not create archive directory structure."
      exit
   fi
fi

ARGUSARCHIVE=$ARGUSARCHIVE/$DAY

if [ ! -d $ARGUSARCHIVE ]; then
   mkdir $ARGUSARCHIVE
   if [ ! -d $ARGUSARCHIVE ]; then
      echo "could not create archive directory structure."
      exit
   fi
fi

if [ -f $DATAFILE ] ; then
   mv $DATAFILE $ARCHIVE.tmp 2>/dev/null
else
   echo "argus data file $ARGUSDATA/$DATAFILE not found"
   exit
fi

if [ -f $ARCHIVE.tmp ]; then
#  $RAGATOR -VRr $ARCHIVE.tmp -w - | $RASORT -w $ARCHIVE
   $RASORT -r $ARCHIVE.tmp -w $ARCHIVE
else
   echo "argus data file not moved."
   exit
fi

if [ -f $ARCHIVE ]; then
   rm -f $ARCHIVE.tmp
   $COMPRESSOR $ARCHIVE
else
   echo "argus data file not moved."
   exit
fi

if [ -f $ARCHIVEZIP ]; then
   ARCHIVESTR=$ARCHIVEZIP
else
   ARCHIVESTR=$ARCHIVE
fi

mv $ARCHIVESTR $ARGUSARCHIVE
if [ ! -f  $ARGUSARCHIVE/$ARCHIVESTR ]; then
   echo "storing compressed argus data file failed"
   echo "leaving as $ARCHIVESTR"
fi

