#!/bin/sh
#
# track log 0.2
###
# Copyright (C) 1999-2004, R-fx Networks <proj@r-fx.org>
# Copyright (C) 2004, Ryan MacDonald <ryan@r-fx.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
###
#
# Just a global PATH so we can find common binaries
PATH=/sbin:/usr/sbin:/usr/bin:/usr/local/bin:/usr/local/sbin:$PATH ; export PATH


# Base run path; no trailing slash
BASERUN="/etc/apf/"

if [ "$1" == "" ] && [ "$2" == "" ]; then
	echo "$0 usage: [file] [tlog]"
	echo "file   :   path to file for tracking"
	echo "tlog   :   data file name to store tracked sized"
	exit 1
else
	FILE="$1"
	TLOG="$2"
fi

if [ ! -f "$FILE" ]; then
	echo "$FILE is not a valid file, aborting"
	exit 1
elif [ ! -d "$BASERUN" ]; then
	echo "$BASERUN is not a valid operating path, aborting."
	exit 1
fi

if [ ! -f "$BASERUN/$TLOG" ]; then
	echo "0" > $BASERUN/$TLOG
fi

getsize() {
if [ ! -f "$BASERUN/$TLOG" ]; then
	SIZE=`ls -al $FILE | awk '{print$5}'`
	echo $SIZE > $BASERUN/$TLOG
else
	SIZE=`cat $BASERUN/$TLOG`
	NEWSIZE=`ls -al $FILE | awk '{print$5}'`
fi
}

getsize
getsize

if [ ! "$NEWSIZE" -lt "$SIZE" ] && [ "$NEWSIZE" -ne "$SIZE" ]; then
	tail -c `expr $NEWSIZE - $SIZE` $FILE
	echo $NEWSIZE > $BASERUN/$TLOG
elif [ "$NEWSIZE" -lt "$SIZE" ]; then 
	echo "$NEWSIZE" > $BASERUN/$TLOG
	getsize
	tail -c `expr $NEWSIZE - $SIZE` $FVAL
        echo $NEWSIZE > $BASERUN/$TLOG
fi
