#!/bin/sh
# reports any files deemed temporary as by the filter below

. fslver

Usage() {
	ProgName=`basename "$0"`
	echo "find Temporary Files.
Usage: $ProgName [-c[days]] [[-r] [-f] paths(s) ...]

If -c specified then a more thorough search is done for
(and only for) core files, and more information is printed about them.

If --age=days specified then the temporary files must be over that
number of \"days\" old before being reported.
For e.g. findtf -c --age=4 only reports core files over 4 days old.

If no path(s) specified then the currrent directory is assumed."
	exit
}

MIN_AGE=0 #days

for arg
do
	case "$arg" in
	-h|--help|-help)
		Usage ;;
	-v|--version)
		Version ;;
        --age=*)
		days=`echo $arg | cut -b7-`
		[ "$days" ] && MIN_AGE=$days ;;
	-c)
		core_mode="yes" ;;
	*)
		argsToPassOn="$argsToPassOn $arg" ;;
	esac
done

if [ "$MIN_AGE" != "0" ]; then
	FIND_AGE="-mtime +$MIN_AGE"
fi

set -f #no globbing is required by getfpf
if [ "$force_full" == "yes" ]; then
    . getfpf -f "$argsToPassOn"
else
    . getfpf "$argsToPassOn"
fi

if [ "$core_mode" == "yes" ]; then
	find $findArgs -type f -name "*core*" $FIND_AGE -printf "%p\n" |
	grep -E "/core$|\.core$|/core\.[0-9]+$" |
	xargs -r file |
	grep -F "core file " |
        if [ -p /proc/self/fd/1 ]; then
            cut -f1 -d: #only leave filename
        else
            cat
        fi
	exit
fi

# .v are rcs files
# .swp are vi backup files are shouldn't be removed really?
# Hrm well vi doesn't delete after restore?
# dead.letter generated by pine and sendmail I think
# #*# & *~ are EMACS
# *.pure is purify remains

tmpFiles="core dead.letter '*~' ',*' '*.v' tmp junk '\#*' \
'.emacs_[0-9]*' '*.[Bb][Aa][Kk]' '*.swp' '*.pure'"

# build "find" command line
Names=
for name in $tmpFiles
do
    [ -n "$Names" ] && Names="$Names -o "
    Names="${Names}-name $name"
done
Names="\( $Names \)"

eval find '$findArgs' -type f $Names $FIND_AGE -printf "$FPF'\n'"
