#!/bin/sh

# check options
if [ $# -lt 1 ]; then
    echo "usage: pc_cvs_chroot [-d work_directory] old_cvsroot new_cvsroot" >&2
    exit 1
fi

# set workspace
if [ "$1" = "-d" ]; then
    tree=`echo $2 | sed -e 's@/$@@'`
    [ -h "$tree" ] && tree="$tree/"
    if [ ! -d "$tree" ]; then
        echo "$0: $tree: not found directory" >&2
        exit 1
    fi
    shift; shift
else
    tree=.
fi

oldroot=$1
newroot=$2

count_files="0"
echo "Replacing $oldroot with $newroot"
find $tree -name Root -print0 -follow |  xargs -0 grep -l "$oldroot" | while read f; do
#find $tree -name Root |  while read f; do
    case $f in
    */CVS/Root)
        count_files=`expr $count_files + 1`
        echo $newroot > $f
        ;;
    esac
done

echo "Updated $count_files CVS/Root files"
exit 0