#!/bin/sh
#
# recursive CVS add/commit with comment
#
# written by Markus Neteler 3/2000
# based on useful hints by Bernhard Reiter
# $Id: cvs.recursadd,v 1.5 2003/02/08 21:30:18 markus Exp $

# what to do in case of user break:
function exitprocedure()
{
 echo "User break!"
 exit 1
}
# shell check for user break (signal list: trap -l)
trap "exitprocedure" 2 3 9 15


# add files recursivly to CVS
if [ $# = 0 -o "$1" = "" -o "$1" = "help" -o "$1" = "-h" -o "$1" = "-help" ]
    then
     echo "USAGE: cvs.r.add comment"
     echo "  This command runs recursively within a directory tree"
     echo "  'cvs add' and 'cvs commit' are called within the script."
    else
     comment=$1
fi

if [ ! "$comment" ]
then
   exit
fi

echo "Walking through the tree..., ignore CVS dirs and OBJ dirs"

if test -d CVS
then
 echo "CVS dir is already present. Please check that."
 exit 1
fi

#add the current directory
current=`pwd |tr / ' '|wc -w`         # count the words in path
current=`expr $current + 1`
local_dir=`pwd| cut -d'/' -f$current` # get the name
cd ..
#-------------------------------------
# insert the job here:
 currpwd=`pwd`
 echo "cvs add $local_dir [$currpwd]"
 cvs add $local_dir
#-------------------------------------
cd $local_dir

echo "Adding the files in current directory to CVS (if any)..."
files=`find . -type f -maxdepth 1`
count=`echo $files |wc -w`
if [ $count -gt 0 ]; then
      cvs add $files
      cvs ci -m"$comment" $files
fi

echo "Proceeding..."
dirnames=`find . -type d |grep -v CVS |grep -v OBJ`
for i in $dirnames ; do
   pushd . >/dev/null
   cd $i
   # get the last directory name:
   words=`echo $i |tr / ' '|wc -w`         # count the words
   local_dir=`echo $i| cut -d'/' -f$words` # get the name
   if [ $local_dir != "." ]; then          # don't work on current dir
#-------------------------------------
# insert the job here:
   currpwd=`pwd`

   echo "cvs add $local_dir [$currpwd]"
   cd ..                                   # we were already inside
   cvs add $local_dir
   cd $local_dir                           # o.k. now jump into it

   echo "Add the files in current directory to CVS (if any)..."
   files=`find . -type f -maxdepth 1`
   count=`echo $files |wc -w`
   if [ $count -gt 0 ]; then
    echo files found
      cvs add $files
      cvs ci -m"$comment" $files
   fi
   cd ..
#-------------------------------------
   popd >/dev/null
   fi
done

echo "The End. You may check with cvs update"
