#!/bin/sh

# Make-Bzr-Patch
# Creates a universal patch file from a Bazaar branch
#
# Copyright (C) 2007-2014 ABINIT Group (Yann Pouillon).
# Originally written by Yann Pouillon.
#

#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

#
# TUNE AT YOUR OWN RISKS!
#

# Check arguments
if test "${#}" -lt "1"; then
  echo "Usage: `basename $0` patch_file [starting_revision] [ending_revision]"
  echo ""
  echo "Important: if you specify a starting revision, the diff will"
  echo "           start AFTER the commit of this revision. Idem for"
  echo "           the ending revision."
  echo ""
  exit 0
fi

# Init
patch_file="${1}"
srev="${2}"
erev="${3}"

# Check starting revision
if test "${srev}" = ""; then
  srev=1
fi

# Check patch existence
if test -e "${patch_file}"; then
  echo "Cowardly refusing to overwrite an existing patch!"
  exit 1
fi

# Create patch
bzr diff -r ${srev}..${erev} --prefix "old/:new/" --diff-options="-urN" > ${patch_file}

# Increment starting revision
srev=`echo ${srev} | awk '{print $1+1}'`

# Report
echo "Revisions included in the patch"
echo "-------------------------------"
echo ""
bzr log --line --forward -r ${srev}..${erev}
echo ""
