#!/bin/sh
# --------------------------------------------------------------------------
# Copyright 1992 by Forschungszentrum Informatik (FZI)
#
# You can use and distribute this software under the terms of the license
# version 1 you should have received along with this software.
# If not or if you want additional information, write to
# Forschungszentrum Informatik, "STONE", Haid-und-Neu-Strasse 10-14,
# D-76131 Karlsruhe, Germany.
# --------------------------------------------------------------------------
# 'obst-cct - 27:02:91 - Dietmar Theobald'
#
# obst-cct [-l [<delay>]]
#
# Clean container directory: remove empty containers and squeeze nonempty ones
# in $OBSTCONTAINER.
# If the option '-l' is enabled the removal/squeeze is repeated every <delay>
# seconds. The default value for <delay> is 300s, i.e. 5min.
#
 self='obst-cct'
usage='[-l [<delay>]]'
[ -n "$OBSTdir" ] || { OBSTdir="`cd \`dirname $0\`;pwd`/"; export OBSTdir; }

sync_container=2

if [ -n "$OBSTCONTAINER" ] ; then
  cd $OBSTCONTAINER
elif [ -n "$SOSCONTAINER" ] ; then
  cd $SOSCONTAINER
else
  echo >&2 "*** $self: environment variable OBSTCONTAINER not defined"
  exit 1
fi

[ "$1" = '-l' ] && { loop='+'; shift
                     if [ $# -gt 0 ] ; then
                        delay="$1"; shift
                     else
                        delay='3600'
                     fi
                   }

[ $# -gt 0    ] && { echo >&2 "*** usage: $self $usage"; exit 1 ;}

while true
do
   for cnt in *[0-9]
   do
      [ -f "$cnt" -a "$cnt" != "$sync_container" ] && {
         ${OBSTdir}cnt -t -s $cnt 2> /dev/null
         ${OBSTdir}cnt -t -d $cnt 2> /dev/null
      }
   done

   [ "$loop" ] || break

   sleep $delay
done
