#!/bin/sh

#
# perforce-to-cvs sync script
# Stephan Uhlmann <su@su2.info>
# Version 0.3, 12.01.2002
#

###########################################################################
#                                                                         #
# Copyright (C) 2001, 2002 by Stephan Uhlmann                             #
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
###########################################################################


#
# Adjust the following to your needs
#

#
# Perforce environment variables
#
export P4CLIENT=p42cvs
export P4PORT=perforce.whatever.org:1666
export P4USER=user
export P4PASSWD=password

#
# the CVSROOT for cvs
#
export CVSROOT=:pserver:perforce@cvs.whatever.org:/home/cvs/whatever

#
# Base directory containing all this stuff, in particular
# the subdirectories (see below) of the Perforce files
# ("client view") and a cvs checkout.
#
BASEDIR=/home/foobar/whatever

#
# The following values for the subdirectories must not contain slashes!
#
PERFSUBDIR=Whatever
CVSSUBDIR=Whatever-CVS

#
# location of the p4 tool
#
P4=$BASEDIR/p4



###########################################################################


cd $BASEDIR

echo

MESSAGE="perforce sync `date`"
echo $MESSAGE
echo

$P4 sync

echo

cd $PERFSUBDIR
find > ../perforce-filelist
cd ..

cd $CVSSUBDIR
find -path '*CVS*' -prune -o -print > ../cvs-filelist
cd ..

diff -r -q $PERFSUBDIR $CVSSUBDIR | grep "Files .* and .* differ" | sed s/^Files\ $PERFSUBDIR\\///g | sed s/\ and.*$//g > modifiedfiles
diff perforce-filelist cvs-filelist | grep "^<" | sed s/\<\ \.\\///g > addedfiles
diff perforce-filelist cvs-filelist | grep "^>" | sed s/\>\ \.\\///g > removedfiles

MODIFIEDFILES=`cat modifiedfiles`
ADDEDFILES=`cat addedfiles`
REMOVEDFILES=`cat removedfiles`

cd $CVSSUBDIR

DOCOMMIT=""

echo "---"

if (test -n "$MODIFIEDFILES");
then
 echo "files modified"
 for i in $MODIFIEDFILES;
 do
  cp -afv ../$PERFSUBDIR/$i $i
 done
 DOCOMMIT="yes"
else
 echo "no modified files."
fi

echo "---"

if (test -n "$ADDEDFILES");
then
 echo "files added"
 FILESTOADD=""
 for i in $ADDEDFILES;
 do
  if (test -d "../$PERFSUBDIR/$i");
  then
   mkdir --verbose $i
   cvs add -ko $i
  else
   cp -afv ../$PERFSUBDIR/$i $i
   FILESTOADD="$FILESTOADD $i"
  fi
 done
 if (test -n "$FILESTOADD");
 then
  cvs add -ko $FILESTOADD
 fi
 DOCOMMIT="yes"
else
 echo "no added files."
fi

echo "---"

if (test -n "$REMOVEDFILES");
then
 echo "files removed"
 DIRSTOREMOVE=""
 FILESTOREMOVE=""
 for i in $REMOVEDFILES;
 do
  if (test -d $i);
  then
   DIRSTOREMOVE="$i $DIRSTOREMOVE"
  else
   rm -fv $i
   FILESTOREMOVE="$FILESTOREMOVE $i"
  fi
 done
 if (test -n "$FILESTOREMOVE");
 then
  cvs remove $FILESTOREMOVE
 fi
 DOCOMMIT="yes"
else
 echo "no removed files."
fi

echo "---"


if (test -n "$DOCOMMIT");
then
 cvs commit -m "$MESSAGE"
else
 echo "no commit necessary"
fi

echo "---"

echo "cleanup"

if (test -n "$DIRSTOREMOVE");
then
 for i in $DIRSTOREMOVE;
 do
  rm -rfv $i
 done
fi

cd ..
rm -f perforce-filelist
rm -f cvs-filelist
rm -f modifiedfiles
rm -f addedfiles
rm -f removedfiles

echo
echo
echo

