#!/bin/sh
# This script makes a preliminary svn:ignore in the current dir by
# adding some standard stuff according to Makefile.am.
# License: GPL

addignore() {
	if ! test -f svnignore.tmp; then
	   svn pg svn:ignore . | sed -e "s, *,," | grep -v '^$' > svnignore.tmp
	   addedsomething=0
	fi
	if ! grep -q "^$1\$" svnignore.tmp; then
	  echo "$1" >> svnignore.tmp && echo "added $1 to svn:ignore"
	  sort -u -o svnignore.tmp svnignore.tmp
	  addedsomething=1
	fi
}

recurse=0
if test $# -eq 1; then
	if test "$1" = "-r"; then
		recurse=1
	fi
fi

handledir() {
	(
	cd $1
	trap "rm svnignore.tmp" 1 2 15
	if test -f Makefile.am; then
		if test $recurse -eq 1; then
			echo "Entering $1"
		fi
		addignore Makefile
		addignore Makefile.in

		bins=`perl -p -e 's/\\\s*\n/ /g' Makefile.am | egrep '_PROGRAMS|_LTLIBRARIES|_LIBRARIES' | sed -e 's/.*=\s*//;s/#.*//;s/\$([^)]*)//'`
		if test -n "$bins"; then
			addignore ".libs"
			addignore ".deps"
			for prog in $bins; do
				addignore "$prog"
			done
		fi
		grep -q LIBRARIES Makefile.am && addignore ".libs"
		grep -q METASOURCES Makefile.am && addignore "*.moc"
		fgrep -q .skel Makefile.am && addignore "*.kidl"
		fgrep -q .skel Makefile.am && addignore "*_skel.c*"
		fgrep -q .stub Makefile.am && addignore "*_stub.cpp"
		if fgrep -q .ui Makefile.am; then
			uis=`ls -1 *.ui 2>/dev/null`
			for ui in $uis; do
				addignore ${ui/.ui/.h}
				addignore ${ui/.ui/.cpp}
			done
		fi
		
		grep -q "^include.*/Doxyfile.am$" Makefile.am && addignore "Doxyfile"

		if test "$addedsomething" = 1; then
		    svn propset svn:ignore -F svnignore.tmp .
		fi
		rm svnignore.tmp
	else
		echo "Skipping $1"
	fi
	)
}


if test -f Makefile.am; then
	if test $recurse -eq 1; then
		find . -type d | egrep -v 'CVS|.svn' | sed -e 's,/$,,' | \
		while read dir; do
			handledir $dir
		done
	else
		handledir .
	fi
else
	echo "No Makefile.am found!"
fi

