#!/bin/sh
#
#   basically a client-side "cvs import"
#
#   it has to be client-side so w3 mirroring works right.
#
#   does not work with changes to the tree structure!
#

if [ $# != 3 ]; then
   echo "usage: $0 <directory to publish> <cvs path> <comment>"
   exit
fi

start_dir=`pwd`
pubdir_arg=$1
cvs_path=$2
comment=$3
tmp_dir=/tmp/cvs.$$
t0=$tmp_dir/.files-in-repository
t1=$tmp_dir/.files-to-publish

if ! cd $pubdir_arg; then
    echo "Can't cd to $pubdir_arg"
    exit 1
fi
pubdir=`pwd`


# make a copy of what CVS currently has
rm -rf $tmp_dir
mkdir $tmp_dir
cd $tmp_dir
echo Checking out old version of documentation for comparison
sleep 1
cvs checkout $cvs_path
cd $cvs_path
find * -type f | egrep -v '(^|/)CVS($|/)' | sort > $t0

# find out what is to be published
cd $pubdir
find * -type f | egrep -v '(^|/)CVS($|/)' | sort > $t1

# add the new files and modified files
cp -a * $tmp_dir/$cvs_path

cd $tmp_dir/$cvs_path

# doesn't deal with directory changes, I'm afraid
files_to_add=`diff $t0 $t1 | grep '^>' | cut -c3-`
files_to_remove=`diff $t0 $t1 | grep '^<' | cut -c3-`

if [ -n "$files_to_add" ]; then
  echo add $files_to_add
  cvs add $files_to_add
else 
  echo no files to add
fi

if [ -n "$files_to_remove" ]; then
  echo remove $files_to_remove
  rm -f $files_to_remove
  cvs remove $files_to_remove
else
  echo no files to remove
fi

# cvs commit -m"$comment"
cvs commit

exit 0