#!/usr/bin/env bash
# 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 -x -F -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
	addedsomething=0
	if test -f CMakeLists.txt; then
		if test $recurse -eq 1; then
			echo "Entering $1"
		fi
		addignore Makefile
        addignore CMakeFiles
        addignore cmake_install.cmake
        addignore DartTestfile.txt
        addignore install_icons.cmake
        addignore progress.make
		grep -q kde4_automoc CMakeLists.txt && addignore "*.moc" 
		grep -q kde4_add_dcop_skels CMakeLists.txt && addignore "*.kidl"
		grep -q kde4_add_dcop_skels CMakeLists.txt && addignore "*_skel.c*"
		grep -q kde4_add_dcop_stubs CMakeLists.txt && addignore "*_stub.cpp"
		#TODO add binary

	elif 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 | grep _PROGRAMS | 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"
		
		grep -q "^include.*/Doxyfile.am$" Makefile.am && addignore "Doxyfile"
	else
		qmakefiles=`ls -1 *.pro 2>/dev/null`
		if test -n "$qmakefiles"; then
			if test $recurse -eq 1; then
        			echo "Entering $1"
        		fi
        		addignore Makefile
        		addignore .obj
        		addignore .moc
        		addignore "*.moc"
        		addignore "moc_*.cpp"
        		addignore .ui
        		for f in `ls -1 *.pro`; do
        		   if fgrep -q debug_and_release $f; then
        		    addignore "Makefile.Debug"
        		    addignore "Makefile.Release"
        		   fi
        		   template=`grep ^TEMPLATE $f | sed -e 's/.*=[ \t]*//'`
        		   if test -z "$template" -o "$template" = "app"; then
        		        target=`grep ^TARGET $f | sed -e 's/.*=[ \t]*//'`
                                test -z "$target" && target=`echo $f | sed -e 's/\.pro$//'`
        			addignore $target
        			addignore $target.exe
        			# addignore $target.app # Mac OSX, but only gui apps (grep for CONFIG?)
        		   fi
        		   # RESOURCES = foo.qrc -> addignore qrc_foo.cpp
        		   for resource in `perl -p -e 's/\\\s*\n/ /g' $f | grep ^RESOURCES | sed -e 's/.*=\s*//;s/#.*//;s/\$([^)]*)//'`; do
        		      addignore qrc_`echo $resource | sed -e 's/\.qrc$/\.cpp/'`
        		   done
        		   # FORMS = foo.ui -> addignore ui_foo.h
        		   for form in `perl -p -e 's/\\\s*\n/ /g' $f | grep ^FORMS | sed -e 's/.*=\s*//;s/#.*//;s/\$([^)]*)//'`; do
        		      addignore ui_`echo $form | sed -e 's/\.ui$/\.h/'`
        		   done
        		done
		else
			echo "Skipping $1"
		fi
	fi
	if test "$addedsomething" = 1; then
	    svn propset svn:ignore -F svnignore.tmp .
	fi
	rm -f svnignore.tmp
	)
}

qmakefiles=`ls -1 *.pro 2>/dev/null`
if test -f Makefile.am -o -n "$qmakefiles" -o -f CMakeLists.txt; 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

