#!/bin/bash

POUBELLE="${HOME}/tmp/POUBELLE"
mkdir -p "${POUBELLE}"

usage () {
    echo `basename "$0"` " [-] [-h] [-help] [-clean] [-wipe] [-n] [directory ...]"
    echo "    remove temporaries files"
    echo "    -     Treat the following arguments as filenames \`-\' so  that"
    echo "          you can specify filenames starting with a minus."
    echo "    -h"
    echo "    -help Display this help."
    echo "    -n    Simulate the remove (juste print files)."
    echo "    directories are the roots where the purge had to be done. If no"
    echo "          roots are given, the root is the home directory."
}

DETRUIT=""
ANT_OPT=""
ANT_CMD=""
case "$1" in
    '-' )
	shift;;
    '-n' )
	DETRUIT="echo"
	ANT_OPT="-p"
	shift;;
    '-clean' )
	ANT_CMD="clean"
	shift;;
    '-wipe' )
	ANT_CMD="wipe"
	shift;;
    '-h' | '-help' )
	usage
	shift
	exit;;
esac

DIRS=$*
if test "$#" -le 1
then
    DIRS="$*"
    if test -z "$1" -o -d "$1"
    then
	cd $1 || exit
	DIRS=.
    fi
fi

if test "${ANT_CMD}" != ""
then
    find $DIRS -type f -name build.xml -execdir ant -f {} "${ANT_CMD}" \;
    find $DIRS -type f -name Makefile\* -execdir make -f {} "${ANT_CMD}" \;
    exit
fi

find $DIRS -type d -name .xvpics -exec $DETRUIT rm -r {} \; -prune

find $DIRS '(' \
    -type d -name POUBELLE -prune \
    -o \
    -type f '(' \
    -name core -o -name '*.BAK' -o -name '*.bak' -o -name '*.CKP' \
    -o -name '.*.BAK' -o -name '.*.bak' -o -name '.*.CKP' \
    -o -name '.*.back' -o -name '*.back' \
    -o -name '*.backup' -o -name '*.backup ' \
    -o -name '.*.backup' -o -name '.*.backup ' \
    -o -name .make.state \
    -o -name 'untitled*' -o -name 'Sansnom' \
    -o -name '.emacs_*' -o -name '.wi_*' \
    -o -name 'ws_ftp.log' -o -name 'hs_err*.log' \
    -o -name '#*' -o -name '*~' -o -name '.*~' -o -name junk \
    -o -name '.~lock.*#' \
    -o -name '*%' -o -name '.*%' \
    ')'\
    -print -exec $DETRUIT mv -f '{}' "${POUBELLE}" \; \
    ')'

#		-o -name '*.ps' -o -name '.*.ps' \
#    -o -name '*.i' -o -name '*.ixx' \
#    -o -name '.*.sav' -o -name '*.sav' \

