#!/bin/sh
#
# Written by Daniel Stenberg <Daniel.Stenberg@sth.frontec.se>
# Version 1.0 - 18 July 1997.
#
# This script is called by the bot whenever a file is requested. It will then
# maintain a list of usage that will be used as base for erasing old files
# when the cache fills up.
#

if test "$#" -lt 2; then
 echo "Usage: filecount <remote file> <local file>"
 exit
fi

rname="$1"
lname="$2"

. ./filesetup


# Extract a previous line for this file (if there is any)
line=`grep -s "^$lname" "$CACHEFILE"`

# NOTE: The -s above is to suppress error messages about nonexistent file.
# You may need to alter this.
#

if test -n "$line"; then
  # We got a line
  fetches=`echo $line | cut "-d " -f2`
  fetches=`expr $fetches + 1`

  # Now remove the previous line
  grep -v "$line" $CACHEFILE >$CACHEFILE.$$
  mv $CACHEFILE.$$ $CACHEFILE

else
  # well, it has been requsted this time ;)
  fetches=1
fi

# year+month+day+hour+minute+second
datestring=`date "+%y%m%d%H%M%S"`  

# Now append this line fresh and new in the bottom of the file
echo "$lname $fetches $datestring" >>$CACHEFILE

# sizeofcache=`du -k $CACHEDIR | awk '{print $1}'`

while test `du -k $CACHEDIR | awk '{print $1}'` -gt "$MAXSIZE"; do
  # remove the oldes files until the condition gets false:

  rmfile=`sort -r +3 $CACHEFILE | head -1 | awk '{ print $1}'`

  # now whipe that file
  rm -f "$CACHEDIR/$rmfile"

  # remove the line from the file too:
  sort -r +3 $CACHEFILE | tail +2 >$CACHEFILE.$$
  mv "$CACHEFILE.$$" "$CACHEFILE"

#  sizeofcache=`du -k $CACHEDIR | awk '{print $1}'`

done


syntax highlighted by Code2HTML, v. 0.9.1