#!/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-cgc - 27:02:91 - Dietmar Theobald'
#
# obst_cgc
#
# OBST garbage collection on container level.
#
# The command must be run in 'single user mode', i.e. no containers must be
# accessible during the run.
# Unfortunately, container files must always be writable, even if they are only
# opened for reading.
#
          self='obst-cgc'
sync_container=2

[ -n "$OBSTdir" ] || { OBSTdir="`dirname $0`"; export OBSTdir; }

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

used="`$OBSTdir/obst-dmp -c`"

for ct in ${cnt}/*[0-9] # Don't test the lock files
do
   bn="`basename $ct`"
   if [ "$bn" != "$sync_container" ] ; then
      echo "$used" | grep -s "\<$bn\>" || {
         echo "$self: removing $ct"
         rm -f $ct $ct.lck
      }
   fi
done
