#! /bin/sh
#	$Id: maintainerclobber,v 1.5 2009/09/28 06:56:52 fang Exp $

# emulates a "make maintainerclean" and more
# run this script from the top_srcdir (root)
# "maintainer-clean" should invoke "make distclean"
# Any .tar.* distribution tarballs will not be deleted by this script.  

# even though we've migrated to git, we still keep around .cvsignore files
# and copy them over to .gitignore files.  

usage () {
cat << EOF
$0: script to remove all files not in CVS, like a maintainerclean.
usage: $0 [options]
options:
	-h : print usage and exit
	-k : keep .cvsignore files, even if they are generated
	-n : dry-run print files that would be removed without removing
	-v : verbose mode
EOF
}

args=`getopt "hknv" $*`
test $? = 0 || { usage; exit 2; }
set -- $args

keep_cvsignore=0
verbose=0
dry_run=0

for i
do
case "$i"
in
	-h) usage; exit ;;
	-k) keep_cvsignore=1 ; shift ;;
	-n) dry_run=1 ; shift ;;
	-v) verbose=1 ; shift ;;
	--) shift; break ;;
esac
done

# Nah, allow this script to be run from anywhere
# test -f configure.ac || { echo "You must run this script from the top source directory." ; exit 1 ;}

if test $keep_cvsignore = 1
then
	filt_cvsignore="grep -v -e ^\.cvsignore -e \.gitignore"
else
	filt_cvsignore=cat
fi

# read the .cvsignore to determine what to delete
cvs_clobber () {
# traverse flat list if subdirs
# cvsdirs=`find . -type d -name CVS`
cvsdirs=`find . -type d`
for d in $cvsdirs
do
case $d in
./.git*) ;;
*/CVS*) ;;
*)
#	p=`echo $d | sed 's|/CVS$||'`
	p=$d
	echo "-------- clobbering in $p"
	pushd $p > /dev/null 2>&1
	if test -f .cvsignore
	then
		# only remove things that belong to this directory
		clean=`grep . .cvsignore | sed 's|^\./||' | grep -v '/' | $filt_cvsignore`
		# in case command line is too long
		for f in $clean
		do
			test "$verbose" != 1 || echo "rm -rf $f"
			test "$dry_run" = 1 || rm -rf $f
		done
	fi
	popd > /dev/null 2>&1 ;;
esac
done
echo "-------- done clobbering `pwd`"
test $keep_cvsignore != 1 || echo "All .cvsignore and .gitignore files were kept."
}

cvs_clobber

# to see what's left:
# ls -R

echo "Run \"bootstrap\" to regenerate maintainer files."

