#
# This HOWTO try to define how to import sourceforge package in savannah CVS
# Based on http://www.debian.org/devel/cvs_packages
#
# WARNING WARNING WARNING WARNING
#
# I'm not yet sure of what is written there, 
# I should check with a fake local CVS
# and i will document this...
#
ORIGTGZ="`pwd`/sourceforge_2.5.orig.tar.gz"
PKGDSC="sourceforge_2.5-8.dsc"
PKGDIR=`pwd`

# Don't change this
PKGCVSDIR="`pwd`/savannah"
ORIGDIR="`pwd`/orig"
TESTCVSDIR="`pwd`/testcvsroot/debian-sf"
TESTCVSROOT=":ext:`logname`@`hostname`:$TESTCVSDIR"
SAVANNAHCVSROOT=":ext:cbayle@subversions.gnu.org:/cvsroot/debian-sf/"
CVSROOT=$TESTCVSROOT
echo "Doing with CVSROOT=$CVSROOT"
read bid
#=====
#INIT
#=====
#To checkout the CVS made by savannah the command is 
#for the source tree:
#cd  $PKGCVSDIR (=/where/your/source/tree/will/reside)
#CVS_RSH=ssh cvs -d:ext:cbayle@subversions.gnu.org:/cvsroot/debian-sf/ co debian-sf
#for the html tree:
#CVS_RSH=ssh cvs -d:ext:<username>@subversions.gnu.org:/webcvs/ co non-gnu/debian-sf/
# Init of TEST CVS
if [ "$CVSROOT" = "$TESTCVSROOT" ]
then
	[ ! -d $TESTCVSDIR ] && mkdir -p $TESTCVSDIR/debian-sf
	CVS_RSH=ssh cvs -d$TESTCVSROOT init 
fi

[ ! -d $PKGCVSDIR ] && mkdir $PKGCVSDIR 
cd $PKGCVSDIR
[ ! -d CVSROOT ] && CVS_RSH=ssh cvs -d$CVSROOT co .

echo "Empty CVS Checkout Done"
read bid
#======
#Modules
#======
cd $PKGCVSDIR
if ! grep -q "sourceforge sourceforge-2.5" CVSROOT/modules
then
	cvs checkout CVSROOT
	cd CVSROOT
## edit modules
#In the following, <package> should be the package name as in debian/changelog file. 
#Add the following lines, the first is essential, the second can be duplicated and is self-explanatory: 

	cat >> modules <<-FIN
modules   CVSROOT modules
sourceforge sourceforge-2.5
FIN
	cvs commit -m 'Define first modules'

fi
echo "Module created and commited"
read bid

#=====
#Prepare
#=====
#lets create a local temporary directory
if [ ! -d $ORIGDIR ]
then
	mkdir $ORIGDIR
	cd $ORIGDIR

	tar zvfx  $ORIGTGZ
	# Removing useless stuffs???????
	cd SF2.5 #(The doc seems to say that it should be sourceforge_2.5.orig)
	cvs -d$CVSROOT import -ko -m 'Import of bare source' sourceforge-2.5 source-dist upstream_version_2_5
fi

echo "Original archive unpacking  and import Done"
read bid
#=====
#checkout original tree
#=====
cd $PKGCVSDIR
[ ! -f sourceforge-2.5/README ] && cvs update -d
#
cd $PKGDIR
#Package is a place containing current release (like in pool)
[ ! -d $PKGDIR/sourceforge-2.5 ] && dpkg-source -x $PKGDSC
#(dpkg-source: extracting sourceforge in sourceforge-2.5)
cd $PKGDIR/sourceforge-2.5

echo "Specific Cleaning"
find . -name "\.#*" | xargs rm
rm www/pm/admin/index.php.orig
rm www/pm/admin/index.php.rej

echo "Comparing $PKGDIR/sourceforge-2.5 and $PKGCVSDIR/sourceforge-2.5"
# WARNING WARNING for an unknown reason this doesn't work
#diff -qrBbw . $PKGCVSDIR/sourceforge-2.5 | grep -v CVS
# has to be replaced by
#diff -qrBbw . ../savannah/sourceforge-2.5 | grep -v CVS
#FILES_CHANGED_LOCALLY="<output from above>"
#tar cf - $FILES_CHANGED_LOCALLY | tar -C ~/CVS/sf/debian/savannah/debian-sf -xvpf -
#
# I do this for a in one time transfer of updated files
diff -qrBbw . ../savannah/sourceforge-2.5 | grep -v CVS | grep Files | cut -d" " -f2 | cpio -pdumvB ../savannah/sourceforge-2.5

# This is for new dir
diff -qrBbw . ../savannah/sourceforge-2.5 | grep -v CVS | grep -v Files | cut -d: -f2 | while read dir
do
	cd $PKGDIR/sourceforge-2.5
	echo "====="
	echo "copy $dir"
	echo "====="
	cp -r $dir ../savannah/sourceforge-2.5
	echo "cvs add for $dir"
	cd ../savannah/sourceforge-2.5
	find $dir | while read file
	do
		cvs add $file
	done
done

echo "Cleanup and Update Done"
read bid
#Please make sure that the file debian/rules is executable, since this way it shall be executable when exported, and there will be no problems running dpkg-buildpackage on the exported sources. In general, make sure *all* files have the right permission before you add them to the CVS repository. 
#
#cd ~/CVS/sf/debian/savannah/sourceforge-2.5
#cvs add debian <any other files added as well>
#
#A nice thing is that running cvs update will show you the status of all files in the directory 
#cd ~/CVS/sf/debian/savannah/sourceforge-2.5
#cvs update
#
#The output looks like: 
#
#            cvs update: Updating .
#	    M Makefile
#            cvs update: Updating debian
#            A rules
#            ? example1
#
#M means modified (has to be committed), A means Added (has to be committed), ? means CVS does not know about the file (needs to be added, maybe?). When you are satisfied that nothing has been missed, and all files have the required permissions, you are ready to commit. 
#
cd $PKGCVSDIR/sourceforge-2.5
cvs commit -m 'Made all debian changes'
#cvs tag debian_version_sourceforge_2_5-8 .
cvs tag debian_version_`basename $PKGDSC '.dsc'|tr . _` .
#
#
echo "CVS Commit Done"
read bid

#cd $PKGDIR
#rm -rf $PKGDIR/sourceforge-2.5
#mv $PKGCVSDIR/sourceforge-2.5 $PKGDIR
echo "After modifying your /etc/cvsdeb.conf"
echo "You should be able to cvs-buildpackage -d -n -rfakeroot"
echo "for testing"
echo "and cvs-buildpackage -rfakeroot"
echo "for real build"

