:
##########################################################################
# Title      :	expirefiles - expire files older than a number of days
# Author     :	Heiner Steven <heiner.steven@odn.de>
# Date       :	2007-06-28
# Category   :	File Utilities
# Requires   :	find
# SCCS-Id.   :	@(#) expirefiles	1.3 18/01/05
##########################################################################
# Description
#
##########################################################################

PN=`basename "$0"`			# Program name
VER='1.3'

DEFAULT_CONFIG=$PN.conf
FINDARGS=-L
: ${root:=$HOME}

usage () {
    echo >&2 "$PN - expire files older than a number of days
usage: $PN [-hn] [-c config]
   -c:  use the specified configuration file (default: $DEFAULT_CONFIG)
   -n:  only print what would be done, do not remove anything
   -h:  print this usage summary

The configuration file is searched in the current directory and
in $HOME/etc ."
    exit 1
}

msg () { echo >&2 "$PN:" "$@";  }

fatal () { msg "$@"; exit 1; }

##########################################################################
# initialconfig - write initial configuration file
##########################################################################

initialconfig () {
    cat <<EOT
# $DEFAULT_CONFIG - expire files older than a certain number of days
#
# The program "$PN" reads this file periodically and removes
# files that are older than the specified number of days.
#
#  o  Relative path names (not starting with a '/') are relative to the
#     HOME directory.
#  o  Lines beginning with '#' are comments
#  o  TAB characters (not space characters) must be used for separating
#     columns (consequence: file names cannot contain TABs).
#  o  multiple TABs are collapsed to one (this makes aligning columns
#     easier)
#
# First column:
# 	Directory where old files should be expired. Relative path names
# 	(not starting with a '/') are relative to the HOME directory
# Second column:
#	File name pattern (not regular expression), for use with find(1)
#	"-name".
# Third column:
#	Number of days after which a file should be deleted. The number
#	is used in conjunction with find(1) "-mtime":
#		"+180"	180 days old or older
#		"-180"	180 days old or younger
#		"180"	exactly 180 days old
#
# Directory			Pattern			Days
#log				*.log			+180
EOT

}

##########################################################################

Config=
DryRun=false
while getopts :c:nh opt
do
    case "$opt" in
    	c)	Config=$OPTARG;;
	h)	usage;;
	n)	DryRun=true;;
	?)	usage;;
    esac
done
shift `expr ${OPTIND-1} - 1`

config=$Config
if [ -z "$config" ]
then
    for dir in `pwd` $HOME/etc
    do
	if [ -s "$dir/$DEFAULT_CONFIG" ]
	then
	    config=$dir/$DEFAULT_CONFIG
	    break;
	fi
    done
fi

if [ -z "$config" ]
then
    msg "ERROR: could not find configuration file"
    initialconfig > "$DEFAULT_CONFIG" ||
    	fatal "ERROR: could not write configuration file: $DEFAULT_CONFIG"

    fatal "INFO: wrote example configuration file: $DEFAULT_CONFIG"
fi

[ -r "$config" ] || fatal "ERROR: cannot read configuration file: $config"
[ -s "$config" ] || fatal "ERROR: cannot read configuration file: $config"

if [ "$DryRun" = true ]
then
    findaction="-print"
else
    # Remove matching files. If your local find(1) does not support the
    # POSIX internal xargs operator ("{} +") replace the '+' with ";".
    findaction='-print -exec rm -f -- {} +'
fi

# Remove comment lines and empty lines. Collapse multiple TAB (ASCII 9)
# characters to one TAB.

sed -e '/^[ 	]*#/d' \
	-e '/^[ 	]*$/d' \
	-e 's/		*/	/g' "$config" |
    {
	OIFS=$IFS; newIFS="	"
	IFS=$newIFS
	while read dir pattern time
	do
	    IFS=$OIFS

	    if [ -z "$dir" ] || [ -z "$pattern" ] || [ -z "$time" ]
	    then
	    	msg "ERROR: invalid line: $config"
		msg "lines need to have the format" \
			"DIRECTORY<TAB>PATTERH<TAB>DAYS:"
		fatal "$dir	$pattern	$time"
	    fi

	    #echo >&2 "DEBUG: dir=<$dir> pattern=<$pattern> time=<$time>"

	    # Relative paths (not starting with '/') are always relative to the
	    # HOME directory

	    case "$dir" in
		/*)	absdir=$dir;;
		*)	absdir=$root/$dir;;
	    esac

	    [ -d "$absdir" ] || fatal "ERROR: directory does not exist: $dir"

	    case "$time" in
		[+-]*)	;;
		*)
		    msg "WARNING: number of days should start with '+'/'-'";;
	    esac

	    # Run the find(1) command to delete files. Standard input of
	    # find is redirected because it should not run interactively

	    echo "### expiring $dir ###"
	    find $FINDARGS "$absdir" -name "$pattern" -mtime "$time" \
		$findaction < /dev/null

	    IFS=$newIFS
	done
    }

exit 0
