dirs=`find . -type d ! -name CVS`
removeddirs=" "
for dir in $dirs; do 
   if test -f $dir/Makefile || test -f $dir/Makefile.in; then
	echo "$dir contains files that should have been removed by cvs-clean"
	exit 1
   fi
   files=`cd $dir && ls -1`
   other=0
   for file in $files; do
	case $file in
		CVS|Makefile.am|.cvsignore)
			;;
		*) other=1;
			;;
	esac
   done
   if test "$other" = 0; then
	echo "echo removing $dir"
	echo "cvs remove -f -l $dir"
	removeddirs="$removeddirs$dir "
   fi
done

dirs=`find */docs -type d ! -name CVS | grep -v templates`
for dir in $dirs; do 
   if echo $removeddirs | grep -q " ./$dir "; then
	continue
   fi
   if test ! -f $dir/Makefile.am; then
       echo "echo copying Makefile.am into $dir"
       cp de/docs/Makefile.am $dir
       cvs add $dir/Makefile.am
   fi
   lang=`echo $dir | sed -e "s#^./##; s#/.*##"`
   if ! egrep -q "KDE_LANG *= *$lang\$" $dir/Makefile.am; then
      echo "# wrong lang in $dir/Makefile.am"
      chmod u+w $dir/Makefile.am
      sed -e "s#^KDE_LANG.*#KDE_LANG = $lang#" $dir/Makefile.am > $dir/Makefile.am.new && mv $dir/Makefile.am.new $dir/Makefile.am
   fi
   if test ! -f $dir/.cvsignore; then
       cp de/docs/.cvsignore $dir 
       cvs add $dir/.cvsignore
   fi
done

dirs=`find */messages -type d ! -name CVS | grep -v messages/docs | grep -v others | grep -v kdeextra | grep -v www.kde.org`
xdirs=`find xx/messages -type d ! -name CVS`
for dir in $dirs $xdirs; do
   if echo $removeddirs | grep -q " ./$dir "; then
	continue
   fi
   if test ! -f $dir/Makefile.am; then
       echo "echo copying Makefile.am into $dir"
       cp de/messages/Makefile.am $dir
       cvs add $dir/Makefile.am
   fi
   lang=`echo $dir | sed -e "s#^./##; s#/.*##"`
   if ! egrep -q "KDE_LANG *= *$lang\$" $dir/Makefile.am; then
      echo "echo wrong lang in $dir/Makefile.am"
      sed -e "s#^KDE_LANG.*#KDE_LANG = $lang#" $dir/Makefile.am > $dir/Makefile.am.new && mv $dir/Makefile.am.new $dir/Makefile.am
   fi
   if test ! -f $dir/.cvsignore; then
       cp de/messages/.cvsignore $dir
       cvs add $dir/.cvsignore
   fi
done

list=`find . -name Makefile.am  | grep '\./[^/]*/[^/]*/[^/]*/[^/]*/[^/]*/Makefile.am' `
for i in $list; do
  subdir=`echo $i | sed -e "s#.*docs/[^/]*/##; s#/Makefile.am##"`
  if egrep -q  'KDE_DOCS *= *AUTO' $i; then
    echo "# patching $i"
    sed -e "s#KDE_DOCS *= *AUTO#KDE_DOCS = $subdir#" $i > $i.new && mv $i.new $i
  fi
done

list=`ls -1 */docs/kdelibs/api/Makefile.am 2>/dev/null`
for i in $list; do 
   if ! grep -q index.cache.bz2 $i; then
      echo "# $i is not using rules"
   fi
done

list=`ls -1 */messages/others/Makefile.am */messages/kdeextragear*/Makefile.am */messages/www.kde.org/Makefile.am 2>/dev/null`
for i in $list; do
   if echo $i | grep -q xx/; then
	continue
   fi
   echo "cvs remove -f $i"
done
