#!/bin/sh
#
# This script will clean up a directory from old files.
#
# Options:
# -d <directory>
# -t <time in days to keep files>
# -p <pattern to search>
# -v ==> VERBOSE
#
usage()
{
	echo "Usage: `basename $0`: -d <dir> -t <time> -p <pattern> -v"
	echo "Options:"
	echo "-d <directory to search>"
	echo "-t <files older than x days>"
	echo "-p <search pattern>"
	echo "-v ==> Verbose flag"
	echo ""
	echo "A script to cleanup a directory"
	exit 0
}

WORKFILE=/tmp/`basename $0`.$$
VERBOSE=0
#
while getopts d:t:p:v OPT
do
	case ${OPT} in
		d) 
			DIR=${OPTARG}
		;;
		p)
			PATTERN=${OPTARG}
		;;
		t)
			TIME_KEEP=${OPTARG}
		;;
		v)
			VERBOSE=1
		;;
		\?)
			usage
		;;
	esac
done

if [ -z "${DIR}" ] ; then
	echo "Must specify a directory, aborting..."
	usage
fi

if [ ! -d "${DIR}" ] ; then
	echo "Directory ${DIR} does not exist, aborting ..."
	exit 1
fi

if [ -z "${PATTERN}" ] ; then
	echo "Must specify a pattern to search, aborting..."
	usage
fi

if [ ! -z "${TIME_KEEP}" ] ; then
	MTIME="-mtime +${TIME_KEEP}"
fi

if [ ${VERBOSE} -eq 0 ] ; then
	PRINT=""
else
	PRINT="-print"
fi

find ${DIR} -name "${PATTERN}" -type f ${MTIME} ${PRINT} -exec rm -f {} \;

