#!/bin/sh
#
# Author: Shuhei Ohkado <shuhei@is.s.u-tokyo.ac.jp>
# Version: 0.3

# This shell script will remove 'gomi' files.
#
# You can add your own "gomi" files in the rmfun section.
# The extensible and powerful feature is enabled by the use-specified "gomi" filename way.
# Previous approach you might adapt in removing 'gomi' files is to select carefully which file
# could be removed and even worry with prohibitive concentration about which couldn't,
# including important files like C source files you had much trouble to write up.
# It will cause disastrous predicament if you would mistakenly type 'rm -rf '.
# Also it may facilitate the choice of which file can be removed, you are only to type rmv!
# Note that you can even express pattern matching by '*', so the powerful expressive ability should readily exploited.
#

# Supported OSs are Linux (2.0.36), Solaris (5.6) and IRIX 6.5:
# versions are those I made certain to work well
# I suppose this is feasible on almost all platforms.

# <Copyright>
# You can modify, use and redistribute this script without fee under GPL.

# Options
# -e : 'print only, do not actually remove'
# -f : 'force to remove files, it pass -f argument in the call of rm'
#

# Debug options:
# set -e
# set -v -n
# set -v

commandname=`basename $0`
count=0
echoflag=0
forceflag=0

if [ $# -gt 0 ]
then
    if [ $1 = "-e" ]
    then
	echoflag=1
    elif [ $1 = "-f" ]
    then
	forceflag=1
    else
    	printf "Usage: $0 [-e|-f]\n"
	exit 1
    fi
fi

rmfun ()
{
    file_list="";
    for file in $*
    do
	if [ -f $file ]
	then
	    file_list="$file_list $file"
	fi
    done

    if [ "X$file_list" = "X" ]
    then
        return
    fi
       
    for file in $file_list
    do
	if [ $echoflag -eq 1 ]
	then
	    printf "$commandname will 'rm $file'\n"
	    count=`expr $count + 1`
	elif [ $echoflag -eq 0 ]
	then
	    if [ $forceflag -eq 1 ]
	    then
		printf "%-20s ===> " "trying to rm -f $file"
		rm -f $file
	    elif [ $forceflag -eq 0 ]
	    then
		printf "%-20s ===> " "trying to rm $file"
		rm $file
	    fi

	    if [ $? -eq 0 ];
	    then
		count=`expr $count + 1`
		printf "successful\n"
	    else
		printf "failed\n"
	    fi
	fi
    done
}

# 
# rmfun section
# 
# specify your own 'gomi' filenames as much as you wish
#

rmfun *~
rmfun .*~
rmfun \#*
rmfun a.out
rmfun core
rmfun *.o
rmfun .gdb_history
rmfun .saves-*
rmfun .\#*
rmfun *.s

if [ $count -eq 0 ];
then
    if [ $echoflag -eq 1 ]
    then
	printf "No match, no file will be removed.\n"
    elif [ $echoflag -eq 0 ]
    then
	printf "No match, no file was removed.\n"
    fi
elif [ $count -eq 1 ];
then
    if [ $echoflag -eq 1 ]
    then
	printf "1 file will be removed\n"
    elif [ $echoflag -eq 0 ]
    then
	printf "1 file was removed\n"
    fi
else
    if [ $echoflag -eq 1 ]
    then
	printf "$count files will be removed\n"
    elif [ $echoflag -eq 0 ]
    then
	printf "$count files were removed\n"
    fi
fi
exit 0
