#!/bin/bash
# Daniel Engel (dane@zero.org)
#
# Creates a patch from any version of Anope to another within the
# same CVS module (in this case, anope-rel). Usage:
#
# ./anope-mkpatch 1.5.1 1.5.5
#
# The earliest version you can use is 1.5.1 (the first CVS tag)
#
# Feel free to improve this script at will.
#
CVSROOT=":pserver:pubcvs@zero.org:/home/cvs/anope"
MODULE="anope-dev"
OLD="$1"
NEW="$2"
NAME="$1-$2.diff"

echo "*** Note: This script is able to create patches from one version of 1.5.x"
echo "*** to anoter. The earliest valid version is 1.5.1, and you can only make"
echo "*** a patch between two released versions (i.e. no -rnum versions)"
echo ""

if [[ $OLD == "" ]]; then
	echo "*** Usage: $0 [OLD] [NEW]"
	exit 0
fi


if [[ $NEW == "" ]]; then
	echo "*** Usage: $0 [OLD] [NEW]"
	exit 0
fi

OLD="anope-$OLD"
NEW="anope-$NEW"

if [[ -d $OLD ]]; then
	echo "*** Directory $OLD already exists... aborting."
	exit 0
fi

if [[ -d $NEW ]]; then
	echo "*** Directory $OLD already exists... aborting."
	exit 0
fi

echo "*** Issuing the CVS login, when prompted for a password, just press RETURN..."
cvs -d $CVSROOT login

if [[ $? -ne 0 ]]; then
	echo "*** ERROR: CVS login failed... aborting."
	exit 0
fi

TOLD="$(echo $OLD | sed 's/\./_/g')"
TNEW="$(echo $NEW | sed 's/\./_/g')"

echo "*** Cheking out $MODULE ($TOLD)"
cvs -d $CVSROOT checkout -r $TOLD -d $OLD $MODULE > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
	echo "*** ERROR: Unable to checkout $MODULE ($TNEW)... aborting."
	exit 0
fi

echo "*** Cheking out $MODULE ($TNEW)"
cvs -d $CVSROOT checkout -r $TNEW -d $NEW $MODULE > /dev/null 2>&1
if [[ $? -ne 0 ]]; then
	echo "*** ERROR: Unable to checkout $MODULE ($TNEW)... aborting."
	exit 0
fi

# I wish i could use cvsrmadm here, but not everybody has it :(
echo "*** Removing CVS control files"
find $OLD -name CVS -exec rm -rf {} \; > /dev/null 2>&1
find $NEW -name CVS -exec rm -rf {} \; > /dev/null 2>&1

echo "*** Creating the patch..."
diff -urN $OLD $NEW > $NAME

echo "*** Done! The patch is $NAME";
echo "*** You might want to clean things up with:"
echo "*** rm -rf $OLD $NEW"
