#!/bin/sh
## --------------------------------------------------------------------
## $HeadURL$
## --------------------------------------------------------------------
##!
## --------------------------------------------------------------------
##  COPYRIGHT (C) 1999: Rehinemtall Electronics GMBH, D-28305 BREMEN
## --------------------------------------------------------------------
##
## HIER/ITEM : CSU: RP 5182 Q 100 SRC 00102
## BRIEF DESC: remove corefiles from '/tmp/coredumps' to fall below x%
## AUTHOR    : Norbert Thoden
## RELEASE   :
## MODIF/STAT: 27.10.20
## ORIGIN REF:
##
## Revision INFO:
## --------------
##    $Revision$
##    $Date$
##    $Id$
##
## LANGUAGE: (TCL/TK-Version)
##
## PURPOSE:
##
## ERROR HANDLING:
## keine
##
##!


# /tmp/coredumps is most likely a symlink to /coredumps

dir=/tmp/coredumps
max_usage=70



usage()
{
  echo "usage: `basename "$0"` y  -  (y = yes)   -  to remove corefiles from '${dir}' to fall below $max_usage%"
  exit 1
}

if [ $# -ne 1 ] || [ $1 != y ]; then
  usage
  exit 1
fi


if [ ! -d "$dir" ]; then
  echo "'${dir}' does not exist... exit"
  exit
fi


usep=$(df -H | grep coredumps | awk '{print $5}' | cut -d '%' -f1)
if [ $usep -ge $max_usage ]; then
  echo "Dir '$dir' exceeds limit, $usep % used..."
else
  echo "Dir '$dir' is used by $usep% -> exit"
  exit
fi


echo "Remove oldest files files from '$dir' to fall below $max_usage%..."


remove_older()
{
  days=$1
  echo " remove files older '${days}' ..."
#  find /coredumps -mtime +${days} -exec ls -lisa {} \;
  find /coredumps -mtime +${days} -exec rm -vf {} \;
}

## loop as long as usage is high ; max 10 times
age=100
while [ $usep -ge $max_usage ] && [ "$age" -gt 0 ];
do
  age=$((age-10))
  echo " age'${age}' ..."
  remove_older $age
  usep=$(df -H | grep coredumps | awk '{print $5}' | cut -d '%' -f1)
done


# Finally:
usep=$(df -H | grep coredumps | awk '{print $5}' | cut -d '%' -f1)
echo "Dir '$dir' is used by $usep %"

