#! /bin/sh
# Make a dump file for a project that can't be done by a single
# svndumpfilter command as stuff has been copied from other projects.

# How to call several svn commands
SVNADMIN="sudo svnadmin"
SVNADMIN_LOAD="$SVNADMIN load --quiet"
SVNDUMPFILTER="svndumpfilter --drop-empty-revs --renumber-revs --quiet"
SVNEXPORT="svn export --ignore-externals"
SVNCHECKOUT="svn checkout --ignore-externals"

# Project we want to dump
PROJECT="eXtremeManagement"

# Main repository
ORI_REPOS="/var/lib/svn/zest"
# Temporary repository used for creating a good dump file
TMP_REPOS="/var/lib/svn/temp"
# Final repository where we check that it actually works
FIN_REPOS="/var/lib/svn/fin"

# Dump file for the complete repository
# (numbers may be added after this)
DUMPFILE_ALL="zest.dump"

# Dump file for the project we want:
# (numbers may be added after this)
DUMPFILE_PROJECT="xm.dump"


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

# Give the user some fair warnings.
cat <<EOF
WARNING!!!
This will remove the following subversion repositories if they exist:

$TMP_REPOS
$FIN_REPOS

and in this directory it removes the files and directories:

$DUMPFILE_ALL*
$DUMPFILE_PROJECT*
$PROJECT*
diff*

Are you 100 percent sure you want this?
Enter y to continue.  Enter n (or any other key) to stop.
EOF

read ANSWER
if test x$ANSWER != 'xy'; then
    exit 0
fi

# Cleanup previous attempts
sudo rm -rf $TMP_REPOS $FIN_REPOS
rm -rf $DUMPFILE_ALL* $DUMPFILE_PROJECT* $PROJECT* diff*

echo "Creating temporary repository at $TMP_REPOS"
$SVNADMIN create $TMP_REPOS
sudo chmod -R a+w $TMP_REPOS

# Dump revisions 0 to 2844 as these give no trouble.
GOOD_REV=2844
TROUBLE_REV=$(($GOOD_REV + 1))
NEXT_REV=$(($TROUBLE_REV + 1))

echo "Dumping revisions 0 to $GOOD_REV"
$SVNADMIN dump --quiet --revision 0:$GOOD_REV $ORI_REPOS > ${DUMPFILE_ALL}.1

echo "Filtering our Project into ${DUMPFILE_PROJECT}.1 ..."
cat ${DUMPFILE_ALL}.1 | $SVNDUMPFILTER include $PROJECT > ${DUMPFILE_PROJECT}.1

echo "Load this dump file into the temporary repository..."
$SVNADMIN_LOAD $TMP_REPOS < ${DUMPFILE_PROJECT}.1

echo "Checking out till revision $GOOD_REV"
$SVNCHECKOUT --quiet file://$TMP_REPOS/$PROJECT

echo "Exporting the troubled revision from the original repository."
$SVNEXPORT --force --quiet -r $TROUBLE_REV file://$ORI_REPOS/$PROJECT

cd $PROJECT
# Now do something by hand, depending on where exactly your problem is
svn add integration
svn commit --quiet --message "Added $PROJECT/integration."
cd ..


echo "Dumping revisions $NEXT_REV to HEAD"
$SVNADMIN dump -q -r $NEXT_REV:HEAD --incremental $ORI_REPOS  > ${DUMPFILE_ALL}.2

echo "Filtering our Project into ${DUMPFILE_PROJECT}.2 ..."
cat ${DUMPFILE_ALL}.2 | $SVNDUMPFILTER include $PROJECT > ${DUMPFILE_PROJECT}.2

echo "Load this dump file into the temporary repository..."
$SVNADMIN_LOAD $TMP_REPOS < ${DUMPFILE_PROJECT}.2

echo "Now we must check if everything works."
echo "Dumping all revisions from $TMP_REPOS"
$SVNADMIN dump --quiet $TMP_REPOS > ${DUMPFILE_PROJECT}.all

# For eXtremeManagement we still want to exclude two paths:
# eXtremeManagement/integration and
# eXtremeManagement/temp
echo "Filtering our Project into ${DUMPFILE_PROJECT}.2 ..."
cat ${DUMPFILE_PROJECT}.all | $SVNDUMPFILTER exclude $PROJECT/integration $PROJECT/temp > ${DUMPFILE_PROJECT}.noint

echo "Creating temporary project repository at $FIN_REPOS"
$SVNADMIN create $FIN_REPOS
sudo chmod -R a+w $FIN_REPOS

echo "Load this dump file into the project repository..."
$SVNADMIN_LOAD $FIN_REPOS < ${DUMPFILE_PROJECT}.noint

echo "Now an export of our project from all three"
echo "repositories should be the same.  Exporting..."

$SVNEXPORT --quiet file://$ORI_REPOS/$PROJECT ${PROJECT}.ORI
$SVNEXPORT --quiet file://$TMP_REPOS/$PROJECT ${PROJECT}.TMP
$SVNEXPORT --quiet file://$FIN_REPOS/$PROJECT ${PROJECT}.FIN

echo "Done exporting.  Starting comparison now..."

echo "############################################################"
echo "##### BEGIN of differences between original and final. #####"
diff -q -r ${PROJECT}.ORI ${PROJECT}.FIN
echo "##### END   of differences between original and final. #####"
echo "############################################################"

echo "Listing details, if any..."
diff -r ${PROJECT}.ORI ${PROJECT}.TMP > diff-ori-tmp
diff -r ${PROJECT}.TMP ${PROJECT}.FIN > diff-tmp-fin
diff -r ${PROJECT}.ORI ${PROJECT}.FIN > diff-ori-fin
echo "Details are in the diff* files."

