#! /bin/sh

# $Id: driver.sh,v 1.2 2012/11/28 00:30:32 svnexp Exp svnexp $

HOME=/local/svnexp; export HOME
CVSROOT=$HOME/Fake-CVSROOT; export CVSROOT

TDIR=/local/svnexp/tmp

commitcount=0

svnpath="$1"
cvspath="$2"
cvsbranch="$3"

if [ ! -f $HOME/$cvspath.RUN ]; then
 echo "  $cvspath is not scheduled to run"
 exit 0
fi

echo "svnpath $svnpath  cvspath $cvspath  cvsbranch $cvsbranch"

myrev=$(svn info svnco/$svnpath | awk -F: '$1 == "Revision" {print $2}')
testrev=$(expr $myrev + 1)

cvs=/local/svnexp/exportcvs/$cvspath
svn=/local/svnexp/svnco/$svnpath

while true
do
 cd $HOME
 if [ $commitcount -gt 10 ] ; then
   echo "commit count 10; finishing"
   exit 0
 fi
 lastrev=$(svn info svn://svn.freebsd.org/$svnpath | awk -F: '$1 == "Last Changed Rev" {print $2}')
 if [ "$lastrev" = "" ]; then
   echo "Invalid lastrev, aborting"
   exit 0
 fi
 echo "=== considering" $testrev of $lastrev
 if [ $testrev -gt $lastrev -a ! -f $cvs/force-commit ]; then
   break
 fi
 nextrev=$(svn info -r $testrev svn://svn.freebsd.org/$svnpath | awk -F: '$1 == "Last Changed Rev" {print $2}')
 if [ "$nextrev" = "" ]; then
   echo "Invalid nextrev, aborting"
   exit 0
 fi
 if [ $nextrev = $testrev -o -f $cvs/force-commit ]; then
   cd $HOME
   commitcount=$(expr $commitcount + 1)
   echo "=== commit" $nextrev
   svn log -c $nextrev -v svn://svn.freebsd.org/$svnpath

   #===

   svnrev=$(echo $nextrev)

   rm -f $cvs/force-commit

   cd $cvs || exit 1
   cvs -Rq up $cvsbranch -d -C
   find . -name '.#*' -delete

   cd $svn || exit 1
   #svn revert . || exit 1
   svn up -r $svnrev || exit 1

   cd $cvs || exit 1
   find . -name CVS -prune -o -name LASTCOMMIT.txt -prune -o -type f -print | sed -e 's@^\./@@' | sort -n > $TDIR/cvsfiles
   find . -name CVS -prune -o -type d -print | sed -e 's@^\./@@' | sort -n > $TDIR/cvsdirs

   cd $svn || exit 1
   cp /local/svnexp/deprecated.src etc/rc.d/deprecated || exit 1
   chmod +x etc/rc.d/deprecated || exit 1
   find . -name .svn -prune -o -type f -print | sed -e 's@^\./@@' | sort -n > $TDIR/svnfiles
   find . -name .svn -prune -o -type d -print | sed -e 's@^\./@@' | sort -n > $TDIR/svndirs

   cd $cvs || exit 1
   comm -2 -3 $TDIR/svndirs $TDIR/cvsdirs > $TDIR/dirsadd
   while read d; do
     echo Adding directory $d
     mkdir -p $d
     cvs -q add $d 2>&1
   done <  $TDIR/dirsadd

   cd $cvs || exit 1
   comm -2 -3 $TDIR/svnfiles $TDIR/cvsfiles > $TDIR/filesadd
   while read f; do
     echo Adding file $f
     cp $svn/$f $f
     cvs -q add $f 2>&1
   done < $TDIR/filesadd

   cd $cvs || exit 1
   comm -1 -3 $TDIR/svnfiles $TDIR/cvsfiles > $TDIR/filesremove
   while read f; do
     echo Deleting file $f
     cvs -q rm -f $f 2>&1
   done < $TDIR/filesremove

   comm -1 -2 $TDIR/svnfiles $TDIR/cvsfiles > $TDIR/commonfiles

   cd $cvs || exit 1
   xargs md5 < $TDIR/commonfiles > $TDIR/cvssum

   cd $svn || exit 1
   xargs md5 < $TDIR/commonfiles > $TDIR/svnsum

   cd $cvs || exit 1
   diff -u $TDIR/svnsum $TDIR/cvssum | grep '^-MD5' | sed -e 's/.*(\(.*\)).*/\1/' > $TDIR/changed
   while read f; do
     echo Updating file: $f
     cp $svn/$f $f
   done < $TDIR/changed

   cd $svn || exit 1
   echo "## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/$svnrev" > $TDIR/commitmsg
   echo "## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated" >> $TDIR/commitmsg
#    case "$svnpath" in
#      base/head)
#        ;;
#      *)
#        echo "## SVN ##" >> $TDIR/commitmsg
#        svn log -c $svnrev | sed -e 's/^/## SVN ## /'  >> $TDIR/commitmsg
#        echo "## SVN ##" >> $TDIR/commitmsg
#        ;;
#    esac

   cd $svn || exit 1
   echo "## SVN ## Exported commit - http://svnweb.freebsd.org/changeset/base/$svnrev" > $cvs/LASTCOMMIT.txt
   echo "## SVN ## CVS IS DEPRECATED: http://wiki.freebsd.org/CvsIsDeprecated" >> $cvs/LASTCOMMIT.txt
   svn log -c $svnrev -v | sed -e 's/^/## SVN ## /' >> $cvs/LASTCOMMIT.txt

   cd $cvs || exit 1
   cvs add LASTCOMMIT.txt >/dev/null 2>&1

   cvs -q commit -F $TDIR/commitmsg
   #===
   echo "xxx sleep 5"
   sleep 5
 else
   cd $HOME
   echo "=== not my tree" $testrev
 fi
 testrev=$(expr $testrev + 1)
done
rm -f $HOME/$cvspath.RUN